using System.Collections.Generic; namespace BitStrap { public static class StringHelper { private class IndexComparer : IEqualityComparer { 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 stringTable = new Dictionary(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); } } }