public static bool TryParseInt32(Utf8Span str, out int val) { if (!Utf8Parser.TryParseInt32(str, out val, out var consumed) || consumed != str.Length()) { return(false); } return(true); }
public static int ParseInt32(Utf8Span str) { if (!Utf8Parser.TryParseInt32(str, out var value, out var consumed)) { throw new FormatException(); } if (consumed != str.Length()) { throw new FormatException(); } return(value); }
private static void PrimitiveParserByteSpanToInt32_BytesConsumed(string text) { byte[] utf8ByteArray = Text.Encoding.UTF8.GetBytes(text); var utf8ByteSpan = new ReadOnlySpan <byte>(utf8ByteArray); foreach (var iteration in Benchmark.Iterations) { using (iteration.StartMeasurement()) { for (int i = 0; i < Benchmark.InnerIterationCount; i++) { Utf8Parser.TryParseInt32(utf8ByteSpan, out int value, out int bytesConsumed); TestHelper.DoNotIgnore(value, bytesConsumed); } } } }
private static void PrimitiveParserByteSpanToInt32_BytesConsumed_VariableLength() { int textLength = s_Int32TextArray.Length; byte[][] utf8ByteArray = (byte[][])Array.CreateInstance(typeof(byte[]), textLength); for (var i = 0; i < textLength; i++) { utf8ByteArray[i] = Text.Encoding.UTF8.GetBytes(s_Int32TextArray[i]); } foreach (var iteration in Benchmark.Iterations) { using (iteration.StartMeasurement()) { for (int i = 0; i < Benchmark.InnerIterationCount; i++) { ReadOnlySpan <byte> utf8ByteSpan = utf8ByteArray[i % textLength]; Utf8Parser.TryParseInt32(utf8ByteSpan, out int value, out int bytesConsumed); TestHelper.DoNotIgnore(value, bytesConsumed); } } } }