BizHawk/BizHawk.Client.Common/helpers/StringHelpers.cs

79 lines
1.3 KiB
C#
Raw Normal View History

using System.Linq;
2013-10-25 00:59:34 +00:00
namespace BizHawk.Client.Common
{
// TODO: these classes are worthless or need to be extensions, decide which
2013-10-25 00:59:34 +00:00
public static class StringHelpers
{
public static int HowMany(string str, char c)
{
return !string.IsNullOrEmpty(str) ? str.Count(t => t == c) : 0;
2013-10-25 00:59:34 +00:00
}
public static int HowMany(string str, string s)
{
var count = 0;
for (int i = 0; i < (str.Length - s.Length); i++)
2013-10-25 00:59:34 +00:00
{
if (str.Substring(i, s.Length) == s)
{
2013-10-25 00:59:34 +00:00
count++;
}
2013-10-25 00:59:34 +00:00
}
2013-10-25 00:59:34 +00:00
return count;
}
}
// TODO: put it in its own file
public static class IntHelpers // TODO: a less lame name
2013-10-25 00:59:34 +00:00
{
public static int GetNumDigits(int i)
2013-10-25 00:59:34 +00:00
{
if (i < 0x100)
{
return 2;
}
if (i < 0x10000)
{
return 4;
}
if (i < 0x1000000)
{
return 6;
}
return 8;
2013-10-25 00:59:34 +00:00
}
public static uint MaxHexValueFromMaxDigits(int i)
2013-10-25 00:59:34 +00:00
{
switch (i)
{
case 0:
return 0;
case 1:
return 0xF;
case 2:
return 0xFF;
case 3:
return 0xFFF;
case 4:
return 0xFFFF;
case 5:
return 0xFFFFF;
case 6:
return 0xFFFFFF;
case 7:
return 0xFFFFFFF;
case 8:
return 0xFFFFFFFF;
}
return int.MaxValue;
}
}
2013-10-25 00:59:34 +00:00
}