public static int IsLetter(Utf8Span str, int offset) { str = str.SubstringRaw(offset); var ch = str.CharAt(0); if (ch >= 'a' && ch <= 'z') { return(1); } if (ch >= 'A' && ch <= 'Z') { return(1); } if (ch < 128) { return(0); } var enu = str.GetEnumerator(); if (!enu.MoveNext()) { return(0); } var l = enu.Current; if (l > char.MaxValue) { return(0); } if (!char.IsLetter((char)l)) { return(0); } return(new Utf8String(l.ToString()).Length()); }
public static int IsLetterOrUnderscore(Utf8Span str, int offset) { if (str.CharAt(offset) == (byte)'_') { return(1); } return(IsLetter(str, offset)); }
public static Utf8Span TrimEndSimple(this Utf8Span str) { while (str.Length() != 0 && Utf8Utils.IsWhiteSpace(str.CharAt(str.Length() - 1))) { str = str.SubstringRaw(0, str.Length() - 1); } return(str); }
public static byte?TryCharAt(this Utf8Span str, int index) { if (index < 0 || index >= str.Length()) { return(null); } return(str.CharAt(index)); }
public static int IsLetterOrDigit(Utf8Span str, int offset) { var ch = str.CharAt(offset); if ((byte)ch >= '0' && ch <= (byte)'9') { return(1); } return(IsLetter(str, offset)); }
public static int IsLetterOrDigitOrUnderscore(Utf8Span str, int offset) { var ch = str.CharAt(offset); if (ch == (byte)'_' || ((byte)ch >= '0' && ch <= (byte)'9')) { return(1); } return(IsLetter(str, offset)); }
public static int LastIndexOfRaw(this Utf8Span str, byte value) { for (int i = str.Length() - 1; i >= 0; i--) { if (str.CharAt(i) == value) { return(i); } } return(-1); }
public static int IsDigit(Utf8Span str, int offset) { var ch = str.CharAt(offset); return(ch >= (byte)'0' && ch <= (byte)'9' ? 1 : 0); }
/* * internal static bool IsWhiteSpace(Utf8Span str) * { * str.Trim() * if (str.Length == 0) return true; * for (int i = 0; i < str.Length; i++) * { * if (Utf8Span.IsWhiteSpace(str[i])) return false; * } * return true; * } */ private unsafe int FindSeparator(Utf8Span haystack, Utf8String[] separator, out int foundSeparator) { foundSeparator = 0; if (separator.Length == 1) { var sep = separator[0]; if (sep.Length() == 1) { return(haystack.IndexOfRaw(sep.CharAt(0))); } else { return(haystack.IndexOfRaw(sep)); } } else { byte *charMap = stackalloc byte[32]; InitializeProbabilisticMap(charMap, separator); //ref byte pCh = ref haystack.Bytes.DangerousGetPinnableReference(); //var charthere = haystack.Bytes[0]; //var fond = pCh; for (int i = 0; i < haystack.Length(); i++) { //byte thisChar = Unsafe.Add<byte>(ref pCh, i); byte thisChar = haystack.CharAt(i); if (ProbablyContains(charMap, thisChar)) { var substr = new Utf8Span(haystack.Bytes.Slice(i)); for (int j = 0; j < separator.Length; j++) { if (substr.StartsWith(separator[j])) { foundSeparator = j; return(i); } } //if (ArrayContains(thisChar, anyOf) >= 0) //return i; } } /* * var maxlen = 0; * for (int i = 0; i < separator.Length; i++) * { * maxlen = Math.Max(maxlen, separator[i].Length); * } * * * int expected = -1; * for (int i = 0; i < haystack.Length; i++) * { * var substr = new Utf8Span(haystack.Bytes.Slice(i)); * for (int j = 0; j < separator.Length; j++) * { * if (substr.StartsWith(separator[j])) * { * expected = i; * break; * } * } * if (expected != -1) break; * } * * * var min = int.MaxValue; * var minsep = -1; * const int STEP_SIZE = 100; * for (int j = 0; j < haystack.Length; j += STEP_SIZE) * { * * var subhaystack = haystack.Substring(j, Math.Min(STEP_SIZE + maxlen, haystack.Length - j)); * for (int i = 0; i < separator.Length; i++) * { * var pos = subhaystack.IndexOf(separator[i]); * if (pos != -1) * { * if (pos < min) * { * min = pos; * minsep = i; * subhaystack = subhaystack.Substring(0, Math.Min(min + maxlen, subhaystack.Length)); * } * } * } * * //#if DEBUG * // if (minsep != -1) * // { * // foundSeparator = minsep; * // var v = j + min; * // Debug.Assert(expected == v); * // return v; * // } * //#endif * } * */ return(-1); } }
public static string ToStringCached(this Utf8Span utf8) { if (utf8.IsEmpty) { return(string.Empty); } var utf8length = utf8.Length(); int hash = CalculateHash(utf8.CharAt(0), utf8.CharAt(utf8length / 2), utf8.CharAt(utf8length - 1), utf8length); if (cache == null) { cache = new CacheSlot[6841]; } CacheSlot cacheSlot = cache[hash]; var list = cacheSlot.List; if (list == null) { cacheSlot.List = list = new CacheEntry[6]; } for (int i = 0; i < list.Length; i++) { var entry = list[i]; if (entry.String == null) { break; } if (entry.Span.SequenceEqual(utf8.Bytes)) { return(entry.String); } } lock (lockObj) { var entry = new CacheEntry(); entry.Length = utf8length; entry.String = utf8.ToString(); if (usedScratchpadBytes + utf8length <= scratchpad.Length) { utf8.Bytes.CopyTo(scratchpad.Slice(usedScratchpadBytes)); entry.Bytes = scratchpad; entry.Offset = usedScratchpadBytes; usedScratchpadBytes += utf8length; } else { scratchpad = new byte[Math.Max(scratchpad.Length, utf8length * 2)]; utf8.Bytes.CopyTo(scratchpad); entry.Bytes = scratchpad; entry.Offset = 0; usedScratchpadBytes = utf8length; } cacheSlot.List[cacheSlot.NextItemToReplace] = entry; cacheSlot.NextItemToReplace = (cacheSlot.NextItemToReplace + 1) % cacheSlot.List.Length; cache[hash] = cacheSlot; return(entry.String); } }