static public int CalculateSpecificity(StringSlice inMatchRule, bool inbCaseSensitive, char inWildcard = '*') { if (inMatchRule.IsEmpty) { return(0); } int specificity = (int.MaxValue / 2) - inMatchRule.Length; bool bWildcardStart = inMatchRule.StartsWith(inWildcard); bool bWildcardEnd = inMatchRule.EndsWith(inWildcard); if (bWildcardStart && bWildcardEnd) { specificity = inMatchRule.Length - 2; } else if (bWildcardStart || bWildcardEnd) { specificity = inMatchRule.Length - 1; } if (specificity < 0) { specificity = 0; } if (inbCaseSensitive) { specificity *= 2; } return(specificity); }
/// <summary> /// Attempts to parse the string slice into a color. /// </summary> static public bool TryParseColor(StringSlice inSlice, out Color outColor) { StringSlice colorData = inSlice; StringSlice alphaData = StringSlice.Empty; int dotIdx = inSlice.IndexOf('.'); if (dotIdx >= 0) { colorData = inSlice.Substring(0, dotIdx); alphaData = inSlice.Substring(dotIdx + 1); } Color color = default(Color); bool bParsed = false; if (colorData.StartsWith('#')) { ulong hex; StringSlice hexString = colorData.Substring(1); if (hexString.Length <= 6 && TryParseHex(colorData, 6, out hex)) { color = Colors.RGBA((uint)hex << 8); bParsed = true; } else if (TryParseHex(colorData, 8, out hex)) { color = Colors.RGBA((uint)hex); bParsed = true; } } if (!bParsed) { bParsed = ColorUtility.TryParseHtmlString(colorData.ToString(), out color); if (!bParsed) { outColor = default(Color); return(false); } } if (!alphaData.IsEmpty) { float alphaMult; if (!TryParseFloat(alphaData, out alphaMult)) { outColor = default(Color); return(false); } color.a *= alphaMult / 100f; } outColor = color; return(bParsed); }
/// <summary> /// Attempts to parse a string slice into a ulong. /// </summary> static public bool TryParseULong(StringSlice inSlice, out ulong outULong) { inSlice = inSlice.Trim(); if (inSlice.StartsWith("0x")) { return(TryParseHex(inSlice.Substring(2), 16, out outULong)); } if (inSlice.Length == 0 || TooLong(inSlice, MaxDigits64)) { outULong = 0; return(false); } decimal accum = 0; char c; for (int i = 0; i < inSlice.Length; ++i) { c = inSlice[i]; if (c == Negative) { outULong = 0; return(false); } if (c == Positive) { if (i > 0) { outULong = 0; return(false); } continue; } if (!IsDigit(c)) { outULong = 0; return(false); } accum = (accum * 10) + (ulong)(c - '0'); } if (accum > ulong.MaxValue) { outULong = 0; return(false); } outULong = (uint)accum; return(true); }
/// <summary> /// Determines if a string matches the given filter. /// Wildcard characters are supported at the start and end of the filter. /// </summary> /// <remarks> /// This does not yet support wildcards in the middle of the filter. /// </remarks> static public bool WildcardMatch(StringSlice inString, string inFilter, char inWildcard = '*', bool inbIgnoreCase = false) { if (string.IsNullOrEmpty(inFilter)) { return(inString.IsEmpty); } int filterLength = inFilter.Length; if (filterLength == 1 && inFilter[0] == inWildcard) { return(true); } if (filterLength == 2 && inFilter[0] == inWildcard && inFilter[1] == inWildcard) { return(true); } bool bStart = inFilter[0] == inWildcard; bool bEnd = inFilter[filterLength - 1] == inWildcard; if (bStart || bEnd) { string filterStr = inFilter; int startIdx = 0; if (bStart) { ++startIdx; --filterLength; } if (bEnd) { --filterLength; } filterStr = filterStr.Substring(startIdx, filterLength); if (bStart && bEnd) { return(inString.Contains(filterStr, inbIgnoreCase)); } if (bStart) { return(inString.EndsWith(filterStr, inbIgnoreCase)); } return(inString.StartsWith(filterStr, inbIgnoreCase)); } return(inString.Equals(inFilter, inbIgnoreCase)); }
static public bool TryParse(StringSlice inSlice, out StringHash64 outHash) { if (inSlice.StartsWith(StringHashing.CustomHashPrefix)) { ulong hexVal; if (StringParser.TryParseHex(inSlice.Substring(1), 16, out hexVal)) { outHash = new StringHash64(hexVal); return(true); } } else if (inSlice.StartsWith("0x")) { ulong hexVal; if (StringParser.TryParseHex(inSlice.Substring(2), 16, out hexVal)) { outHash = new StringHash64(hexVal); return(true); } outHash = default(StringHash64); return(false); } else if (inSlice.StartsWith(StringHashing.StringPrefix)) { outHash = inSlice.Substring(1).Hash64(); return(true); } else if (inSlice.StartsWith('"') && inSlice.EndsWith('"')) { outHash = inSlice.Substring(1, inSlice.Length - 2).Hash64(); return(true); } outHash = inSlice.Hash64(); return(true); }
public StringSlice Process(StringSlice inSlice) { StringSlice slice = inSlice.Trim(); if (slice.Length >= 2 && slice.StartsWith('"') && slice.EndsWith('"')) { slice = slice.Substring(1, slice.Length - 2); } // if this contains escaped CSV sequences, unescape it here if (m_Unescape && (slice.Contains("\\") || slice.Contains("\\\""))) { return(slice.Unescape(Escaper.Instance)); } return(slice); }
/// <summary> /// Attempts to parse a string slice into a byte. /// </summary> static public bool TryParseByte(StringSlice inSlice, out byte outByte) { inSlice = inSlice.Trim(); if (inSlice.StartsWith("0x")) { ulong hex; if (TryParseHex(inSlice.Substring(2), 2, out hex)) { outByte = (byte)hex; return(true); } outByte = 0; return(false); } if (inSlice.Length == 0 || TooLong(inSlice, MaxDigits8)) { outByte = 0; return(false); } int accum = 0; char c; for (int i = 0; i < inSlice.Length; ++i) { c = inSlice[i]; if (c == Negative) { outByte = 0; return(false); } if (c == Positive) { if (i > 0) { outByte = 0; return(false); } continue; } if (!IsDigit(c)) { outByte = 0; return(false); } accum = (accum * 10) + (c - '0'); } if (accum > byte.MaxValue) { outByte = 0; return(false); } outByte = (byte)accum; return(true); }
static private bool TryParseLongInternal(StringSlice inSlice, bool inbCheckHex, out long outLong) { inSlice = inSlice.Trim(); if (inbCheckHex && inSlice.StartsWith("0x")) { ulong hex; if (TryParseHex(inSlice.Substring(2), 16, out hex)) { outLong = (long)hex; return(true); } outLong = 0; return(false); } if (inSlice.Length == 0 || TooLong(inSlice, MaxDigits64)) { outLong = 0; return(false); } decimal accum = 0; char c; int sign = 1; for (int i = 0; i < inSlice.Length; ++i) { c = inSlice[i]; if (c == Negative) { if (i > 0) { outLong = 0; return(false); } sign = -1; continue; } else if (c == Positive) { if (i > 0) { outLong = 0; return(false); } continue; } if (!IsDigit(c)) { outLong = 0; return(false); } accum = (accum * 10) + (c - '0'); } accum *= sign; if (accum > long.MaxValue || accum < long.MinValue) { outLong = 0; return(false); } outLong = (long)accum; return(true); }
/// <summary> /// Attempts to parse a string slice into an int. /// </summary> static public bool TryParseInt(StringSlice inSlice, out int outInt) { inSlice = inSlice.Trim(); if (inSlice.StartsWith("0x")) { ulong hex; if (TryParseHex(inSlice.Substring(2), 8, out hex)) { outInt = (int)hex; return(true); } outInt = 0; return(false); } if (inSlice.Length == 0 || TooLong(inSlice, MaxDigits32)) { outInt = 0; return(false); } long accum = 0; char c; int sign = 1; for (int i = 0; i < inSlice.Length; ++i) { c = inSlice[i]; if (c == Negative) { if (i > 0) { outInt = 0; return(false); } sign = -1; continue; } else if (c == Positive) { if (i > 0) { outInt = 0; return(false); } continue; } if (!IsDigit(c)) { outInt = 0; return(false); } accum = (accum * 10) + (c - '0'); } accum *= sign; if (accum > int.MaxValue || accum < int.MinValue) { outInt = 0; return(false); } outInt = (int)accum; return(true); }
static private int EvaluateFloatMode(StringSlice inSlice) { if (inSlice.StartsWith("0x")) { return(ReadAsInteger); } bool bHasDot = false; bool bHasE = false; for (int i = 0; i < inSlice.Length; ++i) { char c = inSlice[i]; if (c == '.') { if (bHasDot) { return(DoNotRead); } bHasDot = true; } else if (c == 'e' || c == 'E') { if (bHasE) { return(DoNotRead); } bHasE = true; } else { if (c == Positive || c == Negative) { if (i > 0) { return(DoNotRead); } continue; } if (!IsDigit(c)) { return(DoNotRead); } } if (bHasDot && bHasE) { break; } } if (bHasE) { return(ReadAsSystemFloat); } if (TooLong(inSlice, MaxDigits32)) { return(ReadAsSystemFloat); } return(bHasDot ? ReadAsDecimalPlace : ReadAsInteger); }