public void IsNext_Empty(bool advancePast) { var reader = new ByteBufferReader(ReadOnlySequence <byte> .Empty); Assert.False(reader.IsNext((byte)'Z', advancePast)); Assert.Equal(0, reader.Consumed); // Nothing is always next Assert.True(reader.IsNext(ReadOnlySpan <byte> .Empty, advancePast)); Assert.Equal(0, reader.Consumed); // Something isn't Assert.False(reader.IsNext(new byte[] { (byte)'\0' }, advancePast)); Assert.Equal(0, reader.Consumed); }
public void RewindEmptyFirstSpan() { // This is to hit the "if (memory.Length == 0)" branch in ResetReader. ReadOnlySequence <byte> bytes = SequenceFactory.Create(new byte[][] { new byte[0], new byte[] { 1, 2 }, new byte[] { 3, 4 } }); var reader = new ByteBufferReader(bytes); reader.Advance(3); Assert.True(reader.IsNext(4)); reader.Rewind(2); Assert.Equal(new byte[] { 1, 2 }, reader.CurrentSpan.ToArray()); }
public void IsNext_Value() { ReadOnlySequence <byte> chars = SequenceFactory.Create(new byte[][] { new byte[] { (byte)'A' }, new byte[] { (byte)'B', (byte)'C' }, }); var reader = new ByteBufferReader(chars); Assert.False(reader.IsNext((byte)'Z', advancePast: false)); Assert.False(reader.IsNext((byte)'B', advancePast: false)); Assert.True(reader.IsNext((byte)'A', advancePast: false)); Assert.True(reader.IsNext((byte)'A', advancePast: true)); Assert.True(reader.IsNext((byte)'B', advancePast: true)); Assert.True(reader.IsNext((byte)'C', advancePast: true)); Assert.False(reader.IsNext((byte)'C', advancePast: true)); Assert.True(reader.End); }
public void IsNext_Span() { ReadOnlySequence <byte> bytes = SequenceFactory.Create(new byte[][] { new byte[] { 0 }, new byte[] { 1, 2 }, new byte[] { 3, 4 }, new byte[] { 5, 6, 7, 8 } }); var reader = new ByteBufferReader(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); Assert.True(reader.IsNext(new byte[] { 7, 8 }, advancePast: true)); Assert.True(reader.End); }
public void TryReadTo_SkipDelimiter_ReadOnlySequence() { byte[] expected = Encoding.UTF8.GetBytes("This is our ^|understanding^|"); ReadOnlySequence <byte> bytes = BufferFactory.CreateUtf8("This is our ^|understanding^|| you see."); ByteBufferReader reader = new ByteBufferReader(bytes); Assert.True(reader.TryReadTo(out ReadOnlySequence <byte> sequence, (byte)'|', (byte)'^', advancePastDelimiter: true)); Assert.Equal(expected, sequence.ToArray()); Assert.True(reader.IsNext((byte)' ')); Assert.Equal(30, reader.Consumed); reader = new ByteBufferReader(bytes); Assert.True(reader.TryReadTo(out sequence, (byte)'|', (byte)'^', advancePastDelimiter: false)); Assert.Equal(expected, sequence.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 ByteBufferReader(bytes); Assert.True(reader.TryReadTo(out sequence, (byte)'|', (byte)'^', advancePastDelimiter: true)); Assert.Equal(expected, sequence.ToArray()); Assert.True(reader.IsNext((byte)' ')); Assert.Equal(30, reader.Consumed); reader = new ByteBufferReader(bytes); Assert.True(reader.TryReadTo(out sequence, (byte)'|', (byte)'^', advancePastDelimiter: false)); Assert.Equal(expected, sequence.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 ByteBufferReader(bytes); Assert.True(reader.TryReadTo(out sequence, (byte)'|', (byte)'^', advancePastDelimiter: true)); Assert.Equal(expected, sequence.ToArray()); Assert.True(reader.IsNext((byte)' ')); Assert.Equal(30, reader.Consumed); reader = new ByteBufferReader(bytes); Assert.True(reader.TryReadTo(out sequence, (byte)'|', (byte)'^', advancePastDelimiter: false)); Assert.Equal(expected, sequence.ToArray()); Assert.True(reader.IsNext((byte)'|')); Assert.Equal(29, reader.Consumed); // No trailing data bytes = BufferFactory.CreateUtf8("This is our ^|understanding^||"); reader = new ByteBufferReader(bytes); Assert.True(reader.TryReadTo(out sequence, (byte)'|', (byte)'^', advancePastDelimiter: false)); Assert.Equal(expected, sequence.ToArray()); Assert.True(reader.IsNext((byte)'|')); Assert.Equal(29, reader.Consumed); reader = new ByteBufferReader(bytes); Assert.True(reader.TryReadTo(out sequence, (byte)'|', (byte)'^', advancePastDelimiter: true)); Assert.Equal(expected, sequence.ToArray()); Assert.True(reader.End); Assert.Equal(30, reader.Consumed); // All delimiters skipped bytes = BufferFactory.CreateUtf8("This is our ^|understanding^|"); reader = new ByteBufferReader(bytes); Assert.False(reader.TryReadTo(out sequence, (byte)'|', (byte)'^', advancePastDelimiter: false)); Assert.Equal(0, reader.Consumed); reader = new ByteBufferReader(bytes); Assert.False(reader.TryReadTo(out sequence, (byte)'|', (byte)'^', advancePastDelimiter: true)); Assert.Equal(0, reader.Consumed); bytes = BufferFactory.CreateUtf8("abc^|de|"); reader = new ByteBufferReader(bytes); Assert.True(reader.TryReadTo(out sequence, (byte)'|', (byte)'^', advancePastDelimiter: true)); Assert.Equal(Encoding.UTF8.GetBytes("abc^|de"), sequence.ToArray()); Assert.True(reader.End); Assert.Equal(8, reader.Consumed); // Escape leads bytes = BufferFactory.CreateUtf8("^|a|b"); reader = new ByteBufferReader(bytes); Assert.True(reader.TryReadTo(out sequence, (byte)'|', (byte)'^', advancePastDelimiter: true)); Assert.Equal(Encoding.UTF8.GetBytes("^|a"), sequence.ToArray()); Assert.True(reader.IsNext((byte)'b')); Assert.Equal(4, reader.Consumed); // Delimiter starts second segment. bytes = BufferFactory.CreateUtf8("^", "|a|b"); reader = new ByteBufferReader(bytes); Assert.True(reader.TryReadTo(out sequence, (byte)'|', (byte)'^', advancePastDelimiter: true)); Assert.Equal(Encoding.UTF8.GetBytes("^|a"), sequence.ToArray()); Assert.True(reader.IsNext((byte)'b')); Assert.Equal(4, reader.Consumed); }
public void TryReadTo_SkipDelimiter_Runs() { ReadOnlySequence <byte> bytes = BufferFactory.CreateUtf8("abc^^|def"); ByteBufferReader reader = new ByteBufferReader(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 ByteBufferReader(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 ByteBufferReader(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 ByteBufferReader(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 ByteBufferReader(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 ByteBufferReader(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 ByteBufferReader(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 ByteBufferReader(bytes); Assert.False(reader.TryReadTo(out span, (byte)'|', (byte)'^', advancePastDelimiter: false)); Assert.True(reader.IsNext((byte)'a')); Assert.Equal(0, reader.Consumed); }