/// <summary> /// The method based on ReadOnlySpan /// </summary> /// <param name="str"></param> public static void ParseStringSpan(ReadOnlySpan <char> str) { var url = str.Slice(13, 19); var method = str.Slice(58, 3); PrimitiveParser.TryParseInt32(str.Slice(84, 3).AsBytes(), out int statusCode, out int bytesConsumed); var remoteAddr = str.Slice(116, 14); var referrerPolicy = str.Slice(157, 26); var accept = str.Slice(201, 85); var acceptEncoding = str.Slice(313, 13); var acceptLanguage = str.Slice(353, 44); var connection = str.Slice(419, 10); var cookie = str.Slice(447, 118); var host = str.Slice(581, 11); PrimitiveParser.TryParseInt32(str.Slice(629, 1).AsBytes(), out int updgradeInsecureRequest, out int bytesConsumed2); var userAgent = str.Slice(652, 115); Debug.WriteLine(url); Debug.WriteLine(method); Debug.WriteLine(statusCode); Debug.WriteLine(remoteAddr); Debug.WriteLine(accept); Debug.WriteLine(acceptEncoding); Debug.WriteLine(acceptLanguage); Debug.WriteLine(connection); Debug.WriteLine(cookie); Debug.WriteLine(host); Debug.WriteLine(updgradeInsecureRequest); Debug.WriteLine(userAgent); }
public unsafe void ParseInt32Thai(string text) { ReadOnlySpan <byte> utf8Span = TestHelper.UtfEncode(text, false); foreach (var iteration in Benchmark.Iterations) { using (iteration.StartMeasurement()) { for (int i = 0; i < Benchmark.InnerIterationCount; i++) { PrimitiveParser.TryParseInt32(utf8Span, out int value, out int bytesConsumed, 'G', TestHelper.ThaiTable); TestHelper.DoNotIgnore(value, bytesConsumed); } } } }
public unsafe void Int32PositiveHexTests(string text, int length, int expectedValue, int expectedConsumed) { byte[] byteBuffer = new Utf8String(text).CopyBytes(); ReadOnlySpan <byte> byteSpan = new ReadOnlySpan <byte>(byteBuffer); char[] charBuffer = text.ToCharArray(); ReadOnlySpan <char> charSpan = new ReadOnlySpan <char>(charBuffer); bool result; int actualValue; int actualConsumed; result = PrimitiveParser.TryParseInt32(byteSpan, out actualValue, out actualConsumed, EncodingData.InvariantUtf8, 'X'); Assert.True(result); Assert.Equal(expectedValue, actualValue); Assert.Equal(expectedConsumed, actualConsumed); fixed(byte *bytePointer = byteBuffer) { result = PrimitiveParser.InvariantUtf8.Hex.TryParseInt32(bytePointer, length, out actualValue); Assert.True(result); Assert.Equal(expectedValue, actualValue); result = PrimitiveParser.InvariantUtf8.Hex.TryParseInt32(bytePointer, length, out actualValue, out actualConsumed); Assert.True(result); Assert.Equal(expectedValue, actualValue); Assert.Equal(expectedConsumed, actualConsumed); } result = PrimitiveParser.InvariantUtf8.Hex.TryParseInt32(byteSpan, out actualValue); Assert.True(result); Assert.Equal(expectedValue, actualValue); result = PrimitiveParser.InvariantUtf8.Hex.TryParseInt32(byteSpan, out actualValue, out actualConsumed); Assert.True(result); Assert.Equal(expectedValue, actualValue); Assert.Equal(expectedConsumed, actualConsumed); ReadOnlySpan <byte> utf16ByteSpan = charSpan.Cast <char, byte>(); result = PrimitiveParser.TryParseInt32(utf16ByteSpan, out actualValue, out actualConsumed, EncodingData.InvariantUtf16, 'X'); Assert.True(result); Assert.Equal(expectedValue, actualValue); Assert.Equal(expectedConsumed, actualConsumed / 2); fixed(char *charPointer = charBuffer) { result = PrimitiveParser.InvariantUtf16.Hex.TryParseInt32(charPointer, length, out actualValue); Assert.True(result); Assert.Equal(expectedValue, actualValue); result = PrimitiveParser.InvariantUtf16.Hex.TryParseInt32(charPointer, length, out actualValue, out actualConsumed); Assert.True(result); Assert.Equal(expectedValue, actualValue); Assert.Equal(expectedConsumed, actualConsumed); } result = PrimitiveParser.InvariantUtf16.Hex.TryParseInt32(charSpan, out actualValue); Assert.True(result); Assert.Equal(expectedValue, actualValue); result = PrimitiveParser.InvariantUtf16.Hex.TryParseInt32(charSpan, out actualValue, out actualConsumed); Assert.True(result); Assert.Equal(expectedValue, actualValue); Assert.Equal(expectedConsumed, actualConsumed); }