// TODO: how to we chose the lengths of the temp buffers? // TODO: these methods call the slow overloads of PrimitiveParser.TryParseXXX. Do we need fast path? // TODO: these methods hardcode the format. Do we need this to be something that can be specified? public bool TryParseBoolean(out bool value) { int consumed; var unread = Unread; if (PrimitiveParser.TryParseBoolean(unread, out value, out consumed, _encoder)) { if (unread.Length > consumed) { _currentSegmentIndex += consumed; return(true); } } unsafe { byte *temp = stackalloc byte[15]; var tempSpan = new Span <byte>(temp, 15); var copied = CopyTo(tempSpan); if (PrimitiveParser.TryParseBoolean(tempSpan.Slice(0, copied), out value, out consumed, _encoder)) { Advance(consumed); return(true); } } return(false); }
public unsafe void BooleanPositiveTests(string text, int length, bool 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; bool actualValue; int actualConsumed; result = PrimitiveParser.TryParseBoolean(byteSpan, out actualValue, out actualConsumed, EncodingData.InvariantUtf8); Assert.True(result); Assert.Equal(expectedValue, actualValue); Assert.Equal(expectedConsumed, actualConsumed); fixed(byte *bytePointer = byteBuffer) { result = PrimitiveParser.InvariantUtf8.TryParseBoolean(bytePointer, length, out actualValue); Assert.True(result); Assert.Equal(expectedValue, actualValue); result = PrimitiveParser.InvariantUtf8.TryParseBoolean(bytePointer, length, out actualValue, out actualConsumed); Assert.True(result); Assert.Equal(expectedValue, actualValue); Assert.Equal(expectedConsumed, actualConsumed); } result = PrimitiveParser.InvariantUtf8.TryParseBoolean(byteSpan, out actualValue); Assert.True(result); Assert.Equal(expectedValue, actualValue); result = PrimitiveParser.InvariantUtf8.TryParseBoolean(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.TryParseBoolean(utf16ByteSpan, out actualValue, out actualConsumed, EncodingData.InvariantUtf16); Assert.True(result); Assert.Equal(expectedValue, actualValue); Assert.Equal(expectedConsumed, actualConsumed / 2); fixed(char *charPointer = charBuffer) { result = PrimitiveParser.InvariantUtf16.TryParseBoolean(charPointer, length, out actualValue); Assert.True(result); Assert.Equal(expectedValue, actualValue); result = PrimitiveParser.InvariantUtf16.TryParseBoolean(charPointer, length, out actualValue, out actualConsumed); Assert.True(result); Assert.Equal(expectedValue, actualValue); Assert.Equal(expectedConsumed, actualConsumed); } result = PrimitiveParser.InvariantUtf16.TryParseBoolean(charSpan, out actualValue); Assert.True(result); Assert.Equal(expectedValue, actualValue); result = PrimitiveParser.InvariantUtf16.TryParseBoolean(charSpan, out actualValue, out actualConsumed); Assert.True(result); Assert.Equal(expectedValue, actualValue); Assert.Equal(expectedConsumed, actualConsumed); }