private void TryParse_Special <T>(ParseDelegate <T> parser, T positiveInfinity, T negativeInfinity, T nan) where T : unmanaged, IComparable <T> { ReadOnlySequence <byte> bytes = BufferFactory.CreateUtf8("Infinity"); BufferReader <byte> reader = new BufferReader <byte>(bytes); Assert.True(parser(ref reader, out T value)); Assert.Equal(positiveInfinity, value); Assert.Equal(8, reader.Consumed); bytes = BufferFactory.CreateUtf8("I", "n", "finity", "-"); reader = new BufferReader <byte>(bytes); Assert.True(parser(ref reader, out value)); Assert.Equal(positiveInfinity, value); Assert.Equal(8, reader.Consumed); bytes = BufferFactory.CreateUtf8("-Infinity"); reader = new BufferReader <byte>(bytes); Assert.True(parser(ref reader, out value)); Assert.Equal(negativeInfinity, value); Assert.Equal(9, reader.Consumed); bytes = BufferFactory.CreateUtf8("-", "Infinit", "y"); reader = new BufferReader <byte>(bytes); Assert.True(parser(ref reader, out value)); Assert.Equal(negativeInfinity, value); Assert.Equal(9, reader.Consumed); bytes = BufferFactory.CreateUtf8("NaN"); reader = new BufferReader <byte>(bytes); Assert.True(parser(ref reader, out value)); Assert.Equal(nan, value); Assert.Equal(3, reader.Consumed); }
public void Rewind_ByOne() { ReadOnlySequence <byte> bytes = BufferFactory.Create(new byte[][] { new byte[] { 0 }, new byte[] { 1, 2 }, new byte[] { 3, 4 }, new byte[] { 5, 6, 7, 8 } }); BufferReader <byte> reader = new BufferReader <byte>(bytes); reader.Advance(1); BufferReader <byte> copy = reader; for (int i = 1; i < bytes.Length; i++) { reader.Advance(i); for (int j = 0; j < i; j++) { reader.Rewind(1); Assert.False(reader.End); } Assert.Equal(copy.Position, reader.Position); Assert.Equal(copy.Consumed, reader.Consumed); Assert.Equal(copy.CurrentSpanIndex, reader.CurrentSpanIndex); Assert.Equal(copy.End, reader.End); Assert.True(copy.CurrentSpan.SequenceEqual(reader.CurrentSpan)); } }
public void IsNext_Span() { ReadOnlySequence <byte> bytes = BufferFactory.Create(new byte[][] { new byte[] { 0 }, new byte[] { 1, 2 }, new byte[] { 3, 4 }, new byte[] { 5, 6, 7, 8 } }); BufferReader <byte> reader = new BufferReader <byte>(bytes); Assert.True(reader.IsNext(ReadOnlySpan <byte> .Empty, advancePast: false)); Assert.True(reader.IsNext(ReadOnlySpan <byte> .Empty, advancePast: true)); Assert.True(reader.IsNext(new byte[] { 0 }, advancePast: false)); Assert.False(reader.IsNext(new byte[] { 0, 2 }, advancePast: false)); Assert.False(reader.IsNext(new byte[] { 0, 2 }, advancePast: true)); Assert.True(reader.IsNext(new byte[] { 0, 1 }, advancePast: false)); Assert.False(reader.IsNext(new byte[] { 0, 1, 3 }, advancePast: false)); Assert.True(reader.IsNext(new byte[] { 0, 1, 2 }, advancePast: false)); Assert.False(reader.IsNext(new byte[] { 0, 1, 2, 4 }, advancePast: false)); Assert.True(reader.IsNext(new byte[] { 0, 1, 2, 3 }, advancePast: false)); Assert.True(reader.IsNext(new byte[] { 0, 1, 2, 3, 4 }, advancePast: false)); Assert.True(reader.IsNext(new byte[] { 0, 1, 2, 3, 4, 5 }, advancePast: false)); Assert.True(reader.IsNext(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, advancePast: false)); Assert.False(reader.IsNext(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, advancePast: false)); Assert.False(reader.IsNext(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, advancePast: true)); Assert.Equal(0, reader.Consumed); Assert.True(reader.IsNext(new byte[] { 0, 1, 2, 3 }, advancePast: true)); Assert.True(reader.IsNext(new byte[] { 4, 5, 6 }, advancePast: true)); Assert.True(reader.TryPeek(out byte value)); Assert.Equal(7, value); }
public void TryReadToSpan_Sequence(bool advancePastDelimiter) { ReadOnlySequence <byte> bytes = BufferFactory.Create(new byte[][] { new byte[] { 0, 0 }, new byte[] { 1, 1, 2, 2 }, new byte[] { }, new byte[] { 3, 3, 4, 4, 5, 5, 6, 6 } }); BufferReader <byte> reader = new BufferReader <byte>(bytes); for (byte i = 0; i < bytes.Length / 2 - 1; i++) { byte[] expected = new byte[i * 2 + 1]; for (int j = 0; j < expected.Length - 1; j++) { expected[j] = (byte)(j / 2); } expected[i * 2] = i; ReadOnlySpan <byte> searchFor = new byte [] { i, (byte)(i + 1) }; BufferReader <byte> copy = reader; Assert.True(copy.TryReadTo(out ReadOnlySequence <byte> seq, searchFor, advancePastDelimiter)); Assert.True(seq.ToArray().AsSpan().SequenceEqual(expected)); } bytes = BufferFactory.Create(new byte[][] { new byte[] { 47, 42, 66, 32, 42, 32, 66, 42, 47 } // /*b * b*/ }); reader = new BufferReader <byte>(bytes); Assert.True(reader.TryReadTo(out ReadOnlySequence <byte> sequence, new byte[] { 42, 47 }, advancePastDelimiter)); // */ Assert.True(sequence.ToArray().AsSpan().SequenceEqual(new byte[] { 47, 42, 66, 32, 42, 32, 66 })); }
public void MultiSegmentBytesReaderNumbers() { var bytes = BufferFactory.Create(new byte[][] { new byte[] { 0 }, new byte[] { 1, 2 }, new byte[] { 3, 4 }, new byte[] { 5, 6, 7, 8 }, new byte[] { 8, 0 }, new byte[] { 1, }, new byte[] { 0, 2, }, new byte[] { 1, 2, 3, 4 }, new byte[] { 5, 6 }, new byte[] { 7, 8, 9, }, new byte[] { 0, 1, 2, 3 }, new byte[] { 4, 5 }, new byte[] { 6, 7, 8, 9 }, new byte[] { 0, 1, 2, 3 }, new byte[] { 4 }, }); var reader = new BufferReader <byte>(bytes); Assert.True(reader.TryReadTo(out ReadOnlySequence <byte> bytesValue, 2)); var span = bytesValue.ToSpan(); Assert.Equal(0, span[0]); Assert.Equal(1, span[1]); Assert.True(reader.TryReadTo(out bytesValue, 5)); span = bytesValue.ToSpan(); Assert.Equal(3, span[0]); Assert.Equal(4, span[1]); Assert.True(reader.TryReadTo(out bytesValue, new byte[] { 8, 8 })); span = bytesValue.ToSpan(); Assert.Equal(6, span[0]); Assert.Equal(7, span[1]); Assert.True(reader.TryRead(out int intValue)); Assert.Equal(BitConverter.ToInt32(new byte[] { 0, 1, 0, 2 }), intValue); Assert.True(reader.TryReadInt32BigEndian(out intValue)); Assert.Equal(BitConverter.ToInt32(new byte[] { 4, 3, 2, 1 }), intValue); Assert.True(reader.TryReadInt64LittleEndian(out long longValue)); Assert.Equal(BitConverter.ToInt64(new byte[] { 5, 6, 7, 8, 9, 0, 1, 2 }), longValue); Assert.True(reader.TryReadInt64BigEndian(out longValue)); Assert.Equal(BitConverter.ToInt64(new byte[] { 0, 9, 8, 7, 6, 5, 4, 3 }), longValue); Assert.True(reader.TryReadInt16LittleEndian(out short shortValue)); Assert.Equal(BitConverter.ToInt16(new byte[] { 1, 2 }), shortValue); Assert.True(reader.TryReadInt16BigEndian(out shortValue)); Assert.Equal(BitConverter.ToInt16(new byte[] { 4, 3 }), shortValue); }
private void TryParse <T>(ParseDelegate <T> parser, T expected, string formatString, char standardFormat) where T : unmanaged, IEquatable <T>, IFormattable, IConvertible { // Note that there is no support in Utf8Parser for localized separators string text = expected.ToString(formatString, CultureInfo.InvariantCulture); ReadOnlySequence <byte> bytes = BufferFactory.CreateUtf8(text); BufferReader <byte> reader = new BufferReader <byte>(bytes); Assert.True(parser(ref reader, out T value, standardFormat)); Assert.Equal(text, value.ToString(formatString, CultureInfo.InvariantCulture)); }
public void TryReadTo_NotFound_Sequence(bool advancePastDelimiter) { ReadOnlySequence <byte> bytes = BufferFactory.Create(new byte[][] { new byte[] { 1 }, new byte[] { 2, 3, 255 } }); BufferReader <byte> reader = new BufferReader <byte>(bytes); reader.Advance(4); Assert.False(reader.TryReadTo(out ReadOnlySequence <byte> span, 255, 0, advancePastDelimiter)); }
public void TryReadTo_Sequence(bool advancePastDelimiter, bool useEscapeOverload) { ReadOnlySequence <byte> bytes = BufferFactory.Create(new byte[][] { new byte[] { 0 }, new byte[] { 1, 2 }, new byte[] { }, new byte[] { 3, 4, 5, 6 } }); BufferReader <byte> reader = new BufferReader <byte>(bytes); // Read to 0-5 for (byte i = 0; i < bytes.Length - 1; i++) { BufferReader <byte> copy = reader; // Can read to the first integer (0-5) Assert.True( useEscapeOverload ? copy.TryReadTo(out ReadOnlySequence <byte> sequence, i, 255, advancePastDelimiter) : copy.TryReadTo(out sequence, i, advancePastDelimiter)); // Should never have a null Position object Assert.NotNull(copy.Position.GetObject()); var enumerator = sequence.GetEnumerator(); while (enumerator.MoveNext()) { ; } // Should be able to read to final 6 Assert.True( useEscapeOverload ? copy.TryReadTo(out sequence, 6, 255, advancePastDelimiter) : copy.TryReadTo(out sequence, 6, advancePastDelimiter)); Assert.NotNull(copy.Position.GetObject()); enumerator = sequence.GetEnumerator(); while (enumerator.MoveNext()) { ; } // If we didn't advance, we should still be able to read to 6 Assert.Equal(!advancePastDelimiter, useEscapeOverload ? copy.TryReadTo(out sequence, 6, 255, advancePastDelimiter) : copy.TryReadTo(out sequence, 6, advancePastDelimiter)); } }
public void TryParseGuid_MultiSegment() { Guid expected = new Guid("9f21bcb9-f5c2-4b54-9b1c-9e0869bf9c16"); ReadOnlySequence <byte> bytes = BufferFactory.CreateUtf8("9f21bcb9-f5c2-4b54-9b1c-9e0869bf9c16"); BufferReader <byte> reader = new BufferReader <byte>(bytes); Assert.True(reader.TryParse(out Guid value)); Assert.Equal(expected, value); Assert.Equal(36, reader.Consumed); // Leading zero fails bytes = BufferFactory.CreateUtf8("09f21bcb9-f5c2-4b54-9b1c-9e0869bf9c16"); reader = new BufferReader <byte>(bytes); Assert.False(reader.TryParse(out value)); Assert.Equal(default, value);
public void SequenceIndexOfMultiSegment() { ReadOnlySequence <byte> bytes = BufferFactory.Create( new byte[] { 1, 2 }, new byte[] { 3, 4 } ); Assert.Equal(4, bytes.Length); // Static method call to avoid calling ReadOnlyBytes.IndexOf Assert.Equal(-1, Sequence.IndexOf(bytes, 0)); for (int i = 0; i < bytes.Length; i++) { Assert.Equal(i, Sequence.IndexOf(bytes, (byte)(i + 1))); } }
public void TryReadTo_Sequence(bool advancePastDelimiter) { ReadOnlySequence <byte> bytes = BufferFactory.Create(new byte[][] { new byte[] { 0 }, new byte[] { 1, 2 }, new byte[] { }, new byte[] { 3, 4, 5, 6 } }); BufferReader <byte> reader = new BufferReader <byte>(bytes); for (byte i = 0; i < bytes.Length - 1; i++) { BufferReader <byte> copy = reader; Assert.True(copy.TryReadTo(out ReadOnlySequence <byte> span, i, 255, advancePastDelimiter)); Assert.True(copy.TryReadTo(out span, 6, 255, advancePastDelimiter)); Assert.Equal(!advancePastDelimiter, copy.TryReadTo(out span, 6, 255, advancePastDelimiter)); } }
public void AdvancePastEmptySegments() { ReadOnlySequence <byte> bytes = BufferFactory.Create(new byte[][] { new byte[] { 0 }, new byte[] { }, new byte[] { }, new byte[] { } }); var reader = new BufferReader <byte>(bytes); reader.Advance(1); Assert.Equal(0, reader.CurrentSpanIndex); Assert.Equal(0, reader.CurrentSpan.Length); Assert.False(reader.TryPeek(out byte value)); ReadOnlySequence <byte> sequence = reader.Sequence.Slice(reader.Position); Assert.Equal(0, sequence.Length); }
public void BytesReaderParse() { ReadOnlySequence <byte> bytes = BufferFactory.Parse("12|3Tr|ue|456Tr|ue7|89False|"); var reader = new BufferReader <byte>(bytes); Assert.True(reader.TryParse(out long l64)); Assert.Equal(123, l64); Assert.True(reader.TryParse(out bool b)); Assert.Equal(true, b); Assert.True(reader.TryParse(out l64)); Assert.Equal(456, l64); Assert.True(reader.TryParse(out b)); Assert.Equal(true, b); Assert.True(reader.TryParse(out l64)); Assert.Equal(789, l64); Assert.True(reader.TryParse(out b)); Assert.Equal(false, b); }
public void BytesReaderParse() { ReadOnlySequence <byte> bytes = BufferFactory.Parse("12|3Tr|ue|456Tr|ue7|89False|"); var reader = BufferReader.Create(bytes); Assert.True(BufferReaderExtensions.TryParse(ref reader, out ulong u64)); Assert.Equal(123ul, u64); Assert.True(BufferReaderExtensions.TryParse(ref reader, out bool b)); Assert.Equal(true, b); Assert.True(BufferReaderExtensions.TryParse(ref reader, out u64)); Assert.Equal(456ul, u64); Assert.True(BufferReaderExtensions.TryParse(ref reader, out b)); Assert.Equal(true, b); Assert.True(BufferReaderExtensions.TryParse(ref reader, out u64)); Assert.Equal(789ul, u64); Assert.True(BufferReaderExtensions.TryParse(ref reader, out b)); Assert.Equal(false, b); }
public void MultiSegmentBytesReaderNumbers() { var bytes = BufferFactory.Create(new byte[][] { new byte[] { 0 }, new byte[] { 1, 2 }, new byte[] { 3, 4 }, new byte[] { 5, 6, 7, 8 }, new byte[] { 8, 0 }, new byte[] { 1, }, new byte[] { 0, 2, }, new byte[] { 1, 2, 3, 4 }, }); var reader = BufferReader.Create(bytes); Assert.True(BufferReaderExtensions.TryReadUntill(ref reader, out var bytesValue, 2)); var span = bytesValue.ToSpan(); Assert.Equal(0, span[0]); Assert.Equal(1, span[1]); Assert.True(BufferReaderExtensions.TryReadUntill(ref reader, out bytesValue, 5)); span = bytesValue.ToSpan(); Assert.Equal(3, span[0]); Assert.Equal(4, span[1]); Assert.True(BufferReaderExtensions.TryReadUntill(ref reader, out bytesValue, new byte[] { 8, 8 })); span = bytesValue.ToSpan(); Assert.Equal(6, span[0]); Assert.Equal(7, span[1]); Assert.True(BufferReaderExtensions.TryRead(ref reader, out int value, true)); Assert.Equal(BitConverter.ToInt32(new byte[] { 0, 1, 0, 2 }), value); Assert.True(BufferReaderExtensions.TryRead(ref reader, out value)); Assert.Equal(BitConverter.ToInt32(new byte[] { 4, 3, 2, 1 }), value); }
public void TryReadTo_SkipDelimiter() { byte[] expected = Encoding.UTF8.GetBytes("This is our ^|understanding^|"); ReadOnlySequence <byte> bytes = BufferFactory.CreateUtf8("This is our ^|understanding^|| you see."); BufferReader <byte> reader = new BufferReader <byte>(bytes); Assert.True(reader.TryReadTo(out ReadOnlySpan <byte> span, (byte)'|', (byte)'^', advancePastDelimiter: true)); Assert.Equal(expected, span.ToArray()); Assert.True(reader.IsNext((byte)' ')); Assert.Equal(30, reader.Consumed); reader = new BufferReader <byte>(bytes); Assert.True(reader.TryReadTo(out span, (byte)'|', (byte)'^', advancePastDelimiter: false)); Assert.Equal(expected, span.ToArray()); Assert.True(reader.IsNext((byte)'|')); Assert.Equal(29, reader.Consumed); // Put the skip delimiter in another segment bytes = BufferFactory.CreateUtf8("This is our ^|understanding", "^|| you see."); reader = new BufferReader <byte>(bytes); Assert.True(reader.TryReadTo(out span, (byte)'|', (byte)'^', advancePastDelimiter: true)); Assert.Equal(expected, span.ToArray()); Assert.True(reader.IsNext((byte)' ')); Assert.Equal(30, reader.Consumed); reader = new BufferReader <byte>(bytes); Assert.True(reader.TryReadTo(out span, (byte)'|', (byte)'^', advancePastDelimiter: false)); Assert.Equal(expected, span.ToArray()); Assert.True(reader.IsNext((byte)'|')); Assert.Equal(29, reader.Consumed); // Put the skip delimiter at the end of the segment bytes = BufferFactory.CreateUtf8("This is our ^|understanding^", "|| you see."); reader = new BufferReader <byte>(bytes); Assert.True(reader.TryReadTo(out span, (byte)'|', (byte)'^', advancePastDelimiter: true)); Assert.Equal(expected, span.ToArray()); Assert.True(reader.IsNext((byte)' ')); Assert.Equal(30, reader.Consumed); reader = new BufferReader <byte>(bytes); Assert.True(reader.TryReadTo(out span, (byte)'|', (byte)'^', advancePastDelimiter: false)); Assert.Equal(expected, span.ToArray()); Assert.True(reader.IsNext((byte)'|')); Assert.Equal(29, reader.Consumed); // No trailing data bytes = BufferFactory.CreateUtf8("This is our ^|understanding^||"); reader = new BufferReader <byte>(bytes); Assert.True(reader.TryReadTo(out span, (byte)'|', (byte)'^', advancePastDelimiter: false)); Assert.Equal(expected, span.ToArray()); Assert.True(reader.IsNext((byte)'|')); Assert.Equal(29, reader.Consumed); reader = new BufferReader <byte>(bytes); Assert.True(reader.TryReadTo(out span, (byte)'|', (byte)'^', advancePastDelimiter: true)); Assert.Equal(expected, span.ToArray()); Assert.True(reader.End); Assert.Equal(30, reader.Consumed); // All delimiters skipped bytes = BufferFactory.CreateUtf8("This is our ^|understanding^|"); reader = new BufferReader <byte>(bytes); Assert.False(reader.TryReadTo(out span, (byte)'|', (byte)'^', advancePastDelimiter: false)); Assert.Equal(0, reader.Consumed); reader = new BufferReader <byte>(bytes); Assert.False(reader.TryReadTo(out span, (byte)'|', (byte)'^', advancePastDelimiter: true)); Assert.Equal(0, reader.Consumed); bytes = BufferFactory.CreateUtf8("abc^|de|"); reader = new BufferReader <byte>(bytes); Assert.True(reader.TryReadTo(out span, (byte)'|', (byte)'^', advancePastDelimiter: true)); Assert.Equal(Encoding.UTF8.GetBytes("abc^|de"), span.ToArray()); Assert.True(reader.End); Assert.Equal(8, reader.Consumed); // Escape leads bytes = BufferFactory.CreateUtf8("^|a|b"); reader = new BufferReader <byte>(bytes); Assert.True(reader.TryReadTo(out span, (byte)'|', (byte)'^', advancePastDelimiter: true)); Assert.Equal(Encoding.UTF8.GetBytes("^|a"), span.ToArray()); Assert.True(reader.IsNext((byte)'b')); Assert.Equal(4, reader.Consumed); }
public void TryReadTo_SkipDelimiter_Runs() { ReadOnlySequence <byte> bytes = BufferFactory.CreateUtf8("abc^^|def"); BufferReader <byte> reader = new BufferReader <byte>(bytes); Assert.True(reader.TryReadTo(out ReadOnlySpan <byte> span, (byte)'|', (byte)'^', advancePastDelimiter: false)); Assert.Equal(Encoding.UTF8.GetBytes("abc^^"), span.ToArray()); Assert.True(reader.IsNext((byte)'|')); Assert.Equal(5, reader.Consumed); // Split after escape char bytes = BufferFactory.CreateUtf8("abc^^", "|def"); reader = new BufferReader <byte>(bytes); Assert.True(reader.TryReadTo(out span, (byte)'|', (byte)'^', advancePastDelimiter: false)); Assert.Equal(Encoding.UTF8.GetBytes("abc^^"), span.ToArray()); Assert.True(reader.IsNext((byte)'|')); Assert.Equal(5, reader.Consumed); // Split before and after escape char bytes = BufferFactory.CreateUtf8("abc^", "^", "|def"); reader = new BufferReader <byte>(bytes); Assert.True(reader.TryReadTo(out span, (byte)'|', (byte)'^', advancePastDelimiter: false)); Assert.Equal(Encoding.UTF8.GetBytes("abc^^"), span.ToArray()); Assert.True(reader.IsNext((byte)'|')); Assert.Equal(5, reader.Consumed); // Check advance past delimiter reader = new BufferReader <byte>(bytes); Assert.True(reader.TryReadTo(out span, (byte)'|', (byte)'^', advancePastDelimiter: true)); Assert.Equal(Encoding.UTF8.GetBytes("abc^^"), span.ToArray()); Assert.True(reader.IsNext((byte)'d')); Assert.Equal(6, reader.Consumed); // Leading run of 2 bytes = BufferFactory.CreateUtf8("^^|abc"); reader = new BufferReader <byte>(bytes); Assert.True(reader.TryReadTo(out span, (byte)'|', (byte)'^', advancePastDelimiter: false)); Assert.Equal(Encoding.UTF8.GetBytes("^^"), span.ToArray()); Assert.True(reader.IsNext((byte)'|')); Assert.Equal(2, reader.Consumed); // Leading run of 3 bytes = BufferFactory.CreateUtf8("^^^|abc"); reader = new BufferReader <byte>(bytes); Assert.False(reader.TryReadTo(out span, (byte)'|', (byte)'^', advancePastDelimiter: false)); Assert.True(reader.IsNext((byte)'^')); Assert.Equal(0, reader.Consumed); // Trailing run of 3 bytes = BufferFactory.CreateUtf8("abc^^^|"); reader = new BufferReader <byte>(bytes); Assert.False(reader.TryReadTo(out span, (byte)'|', (byte)'^', advancePastDelimiter: false)); Assert.True(reader.IsNext((byte)'a')); Assert.Equal(0, reader.Consumed); // Trailing run of 3, split bytes = BufferFactory.CreateUtf8("abc^^^", "|"); reader = new BufferReader <byte>(bytes); Assert.False(reader.TryReadTo(out span, (byte)'|', (byte)'^', advancePastDelimiter: false)); Assert.True(reader.IsNext((byte)'a')); Assert.Equal(0, reader.Consumed); }