-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helpers.cs
74 lines (65 loc) · 1.8 KB
/
Helpers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace TuckBytesInCode
{
public static class Helpers
{
public static int Base(this ICharLookup map)
{
return (int)Math.Ceiling(Math.Pow(2.0, 8.0 * map.BytesIn / map.CharsOut));
}
public static bool FindBaseRatio(int @base, out int bytesIn, out int charsOut)
{
bytesIn = 0;
charsOut = 0;
double ratio = Math.Log(@base,2.0) / 8.0;
const double searchMax = 16383.0;
for(double den = 1; den <= searchMax; den++)
{
double num = Math.Floor(den * ratio);
double testBase = Math.Ceiling(Math.Pow(2.0,8.0 * num / den));
// Log.Debug("num = "+num+" den = "+den+" tb = "+testBase);
if ((int)testBase == @base) {
bytesIn = (int)num;
charsOut = (int)den;
return true;
}
}
return false;
}
public static long LongCeil(long num, long den)
{
return num / den + (num % den == 0 ? 0 : 1);
}
public static bool StartsWithIC(this string subj, string test)
{
if (subj == null || test == null) {
return false;
}
return 0 == subj.IndexOf(test,StringComparison.OrdinalIgnoreCase);
}
public static bool ContainsIC(this string subj, string test)
{
if (subj == null || test == null) {
return false;
}
return -1 != subj.IndexOf(test,StringComparison.OrdinalIgnoreCase);
}
public static bool EqualsIC(this string subj, string test)
{
if (subj == null || test == null) {
return false;
}
return subj.Equals(test,StringComparison.OrdinalIgnoreCase);
}
public static string PadTo(this string subj, int len, char padChar = ' ')
{
if (subj == null || subj.Length >= len) {
return subj;
}
return subj + new String(padChar,len - subj.Length);
}
}
}