69 lines
1.3 KiB
C#
69 lines
1.3 KiB
C#
using System.Collections.Generic;
|
|
|
|
namespace BitStrap
|
|
{
|
|
public static class StringHelper
|
|
{
|
|
private class IndexComparer : IEqualityComparer<Index>
|
|
{
|
|
public bool Equals(Index x, Index y)
|
|
{
|
|
return x.Equals(y);
|
|
}
|
|
|
|
public int GetHashCode(Index x)
|
|
{
|
|
return x.GetHashCode();
|
|
}
|
|
}
|
|
|
|
private struct Index
|
|
{
|
|
public readonly int number;
|
|
|
|
public readonly string format;
|
|
|
|
private readonly int hashCode;
|
|
|
|
public Index(string format, int number)
|
|
{
|
|
this.number = number;
|
|
this.format = format;
|
|
hashCode = number * format.GetHashCode();
|
|
}
|
|
|
|
public bool Equals(Index other)
|
|
{
|
|
return number == other.number && format == other.format;
|
|
}
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
return hashCode;
|
|
}
|
|
|
|
public string GetString()
|
|
{
|
|
return string.Format(format, number);
|
|
}
|
|
}
|
|
|
|
private static Dictionary<Index, string> stringTable = new Dictionary<Index, string>(new IndexComparer());
|
|
|
|
public static string Format(string format, int number)
|
|
{
|
|
Index key = new Index(format, number);
|
|
if (!stringTable.ContainsKey(key))
|
|
{
|
|
stringTable[key] = key.GetString();
|
|
}
|
|
return stringTable[key];
|
|
}
|
|
|
|
public static string ToStringSmart(this int n, string format = "{0}")
|
|
{
|
|
return Format(format, n);
|
|
}
|
|
}
|
|
}
|