// re-use string keys using lru cache string StringFromCache(StringSlice slice) { int length = _stringCache.Length; int lowestCounter = int.MaxValue; int lowestCounterIndex = 0; for (int i = 0; i < length; ++i) { if (slice.Equals(_stringCache[i].key)) { _stringCache[i].counter = ++_stringCacheCounter; return(_stringCache[i].result); } var counter = _stringCache[i].counter; if (counter < lowestCounter) { lowestCounter = counter; lowestCounterIndex = i; } } string result = slice.ToString(); _stringCache[lowestCounterIndex].key = slice; _stringCache[lowestCounterIndex].result = result; _stringCache[lowestCounterIndex].counter = ++_stringCacheCounter; return(result); }
/// <summary> /// Attempts to parse a string slice into a variant. /// </summary> static public bool TryParse(StringSlice inSlice, bool inbAllowImplicitHash, out Variant outValue) { inSlice = inSlice.Trim(); if (inSlice.Length <= 0 || inSlice.Equals("null", true)) { outValue = Variant.Null; return(true); } if (inSlice.StartsWith(StringHashing.CustomHashPrefix) || inSlice.StartsWith(StringHashing.StringPrefix) || (inSlice.Length >= 2 && inSlice.StartsWith('"') && inSlice.EndsWith('"'))) { StringHash32 hash; if (StringHash32.TryParse(inSlice, out hash)) { outValue = hash; return(true); } } bool bBoolVal; if (StringParser.TryParseBool(inSlice, out bBoolVal)) { outValue = new Variant(bBoolVal); return(true); } int intVal; if (StringParser.TryParseInt(inSlice, out intVal)) { outValue = new Variant(intVal); return(true); } uint uintVal; if (StringParser.TryParseUInt(inSlice, out uintVal)) { outValue = new Variant(uintVal); return(true); } float floatVal; if (StringParser.TryParseFloat(inSlice, out floatVal)) { outValue = new Variant(floatVal); return(true); } if (inbAllowImplicitHash) { StringHash32 hash; if (StringHash32.TryParse(inSlice, out hash)) { outValue = hash; return(true); } } outValue = default(Variant); return(false); }