internal static bool TryParseMethod(ReadOnlySpan <byte> buffer, out HttpMethod method, out int parsedBytes) { var bufferString = new Utf8Span(buffer); if (bufferString.StartsWith(s_Get)) { method = HttpMethod.Get; parsedBytes = s_Get.Bytes.Length; return(true); } if (bufferString.StartsWith(s_Post)) { method = HttpMethod.Post; parsedBytes = s_Post.Bytes.Length; return(true); } if (bufferString.StartsWith(s_Put)) { method = HttpMethod.Put; parsedBytes = s_Put.Bytes.Length; return(true); } if (bufferString.StartsWith(s_Delete)) { method = HttpMethod.Delete; parsedBytes = s_Delete.Bytes.Length; return(true); } method = HttpMethod.Unknown; parsedBytes = 0; return(false); }
public static void TryFind_Char_Ordinal(ustring source, char searchTerm, Range?expectedForwardMatch, Range?expectedBackwardMatch) { using BoundedUtf8Span boundedSpan = new BoundedUtf8Span(source.AsBytes()); Utf8Span searchSpan = boundedSpan.Span; source = null; // to avoid accidentally using this for the remainder of the test // First, search forward bool wasFound = searchSpan.TryFind(searchTerm, out Range actualForwardMatch); Assert.Equal(expectedForwardMatch.HasValue, wasFound); if (wasFound) { AssertRangesEqual(searchSpan.Length, expectedForwardMatch.Value, actualForwardMatch); } // Also check Contains / StartsWith / SplitOn Assert.Equal(wasFound, searchSpan.Contains(searchTerm)); Assert.Equal(wasFound && searchSpan.Bytes[..actualForwardMatch.Start].IsEmpty, searchSpan.StartsWith(searchTerm)); (var before, var after) = searchSpan.SplitOn(searchTerm); if (wasFound) { Assert.True(searchSpan.Bytes[..actualForwardMatch.Start] == before.Bytes); // check for referential equality
/* * 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); } }