Example #1
0
        public void MultiSegmentBytesReaderNumbers()
        {
            ReadOnlySequence <byte> bytes = SequenceFactory.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 },
            });

            ByteBufferReader reader = new ByteBufferReader(bytes);

            Assert.True(reader.TryReadTo(out ReadOnlySequence <byte> bytesValue, 2));
            Span <byte> span = bytesValue.ToArray();

            Assert.Equal(0, span[0]);
            Assert.Equal(1, span[1]);

            Assert.True(reader.TryReadTo(out bytesValue, 5));
            span = bytesValue.ToArray();
            Assert.Equal(3, span[0]);
            Assert.Equal(4, span[1]);

            Assert.True(reader.TryReadTo(out bytesValue, new byte[] { 8, 8 }));
            span = bytesValue.ToArray();
            Assert.Equal(6, span[0]);
            Assert.Equal(7, span[1]);

            //Assert.True(SequenceMarshal.TryRead(ref reader, out int intValue));
            Assert.True(reader.TryReadIntLE(out int intValue));
            Assert.Equal(BitConverter.ToInt32(new byte[] { 0, 1, 0, 2 }), intValue);

            Assert.True(reader.TryReadInt(out intValue));
            Assert.Equal(BitConverter.ToInt32(new byte[] { 4, 3, 2, 1 }), intValue);

            Assert.True(reader.TryReadLongLE(out long longValue));
            Assert.Equal(BitConverter.ToInt64(new byte[] { 5, 6, 7, 8, 9, 0, 1, 2 }), longValue);

            Assert.True(reader.TryReadLong(out longValue));
            Assert.Equal(BitConverter.ToInt64(new byte[] { 0, 9, 8, 7, 6, 5, 4, 3 }), longValue);

            Assert.True(reader.TryReadShortLE(out short shortValue));
            Assert.Equal(BitConverter.ToInt16(new byte[] { 1, 2 }), shortValue);

            Assert.True(reader.TryReadShort(out shortValue));
            Assert.Equal(BitConverter.ToInt16(new byte[] { 4, 3 }), shortValue);
        }
Example #2
0
        public void TryReadTo_Span_At_Segments_Boundary()
        {
            Span <byte>          delimiter = new byte[] { 13, 10 }; // \r\n
            BufferSegment <byte> segment   = new BufferSegment <byte>(Text.Encoding.ASCII.GetBytes("Hello\r"));

            segment.Append(Text.Encoding.ASCII.GetBytes("\nWorld"));                                // add next segment
            ReadOnlySequence <byte> inputSeq = new ReadOnlySequence <byte>(segment, 0, segment, 6); // span only the first segment!
            ByteBufferReader        sr       = new ByteBufferReader(inputSeq);
            bool r = sr.TryReadTo(out ReadOnlySpan <byte> _, delimiter);

            Assert.False(r);
            r = sr.TryReadTo(out ReadOnlySequence <byte> _, delimiter);
            Assert.False(r);
        }
Example #3
0
        public void SingleSegmentBytesReader()
        {
            byte[] buffer = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            var    bytes  = new ReadOnlySequence <byte>(buffer);
            var    reader = new ByteBufferReader(bytes);

            Assert.True(reader.TryReadTo(out ReadOnlySequence <byte> ab, 3));
            Assert.True(ab.First.Span.SequenceEqual(new byte[] { 1, 2 }));

            Assert.True(reader.TryReadTo(out ReadOnlySequence <byte> cd, 6));
            Assert.True(cd.First.Span.SequenceEqual(new byte[] { 4, 5 }));

            Assert.True(reader.TryReadTo(out ReadOnlySequence <byte> ef, new byte[] { 8, 9 }));
            Assert.True(ef.First.Span.SequenceEqual(new byte[] { 7 }));
        }
Example #4
0
        public void TryReadToSpan_Sequence(bool advancePastDelimiter)
        {
            ReadOnlySequence <byte> bytes = SequenceFactory.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 }
            });

            ByteBufferReader reader = new ByteBufferReader(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) };
                ByteBufferReader    copy      = reader;
                Assert.True(copy.TryReadTo(out ReadOnlySequence <byte> seq, searchFor, advancePastDelimiter));
                Assert.True(seq.ToArray().AsSpan().SequenceEqual(expected));
            }

            bytes = SequenceFactory.Create(new byte[][] {
                new byte[] { 47, 42, 66, 32, 42, 32, 66, 42, 47 }   // /*b * b*/
            });

            reader = new ByteBufferReader(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 }));
        }
Example #5
0
        public void EmptyBytesReader()
        {
            var bytes  = ReadOnlySequence <byte> .Empty;
            var reader = new ByteBufferReader(bytes);

            Assert.False(reader.TryReadTo(out ReadOnlySequence <byte> range, (byte)' '));
        }
Example #6
0
        public void TryReadTo_NotFound_Sequence(bool advancePastDelimiter)
        {
            ReadOnlySequence <byte> bytes = SequenceFactory.Create(new byte[][] {
                new byte[] { 1 },
                new byte[] { 2, 3, 255 }
            });

            ByteBufferReader reader = new ByteBufferReader(bytes);

            reader.Advance(4);
            Assert.False(reader.TryReadTo(out ReadOnlySequence <byte> span, 255, 0, advancePastDelimiter));
        }
Example #7
0
        public void TryReadTo_SingleDelimiter()
        {
            ReadOnlySequence <byte> bytes = SequenceFactory.Create(new byte[][] {
                new byte[] { 1 },
                new byte[] { 2, 3, 4, 5, 6 }
            });

            ByteBufferReader baseReader = new ByteBufferReader(bytes);

            ByteBufferReader spanReader     = baseReader;
            ByteBufferReader sequenceReader = baseReader;
            Span <byte>      delimiter      = new byte[] { 1 };

            for (int i = 1; i < 6; i += 1)
            {
                // Also check scanning from the start.
                ByteBufferReader resetReader = baseReader;
                delimiter[0] = (byte)i;
                Assert.True(spanReader.TryReadTo(out ReadOnlySpan <byte> span, delimiter, advancePastDelimiter: true));
                Assert.True(resetReader.TryReadTo(out span, delimiter, advancePastDelimiter: true));
                Assert.True(spanReader.TryPeek(out byte value));
                Assert.Equal(i + 1, value);
                Assert.True(resetReader.TryPeek(out value));
                Assert.Equal(i + 1, value);

                // Also check scanning from the start.
                resetReader  = baseReader;
                delimiter[0] = (byte)i;
                Assert.True(sequenceReader.TryReadTo(out ReadOnlySequence <byte> sequence, delimiter, advancePastDelimiter: true));
                Assert.True(resetReader.TryReadTo(out sequence, delimiter, advancePastDelimiter: true));
                Assert.True(sequenceReader.TryPeek(out value));
                Assert.Equal(i + 1, value);
                Assert.True(resetReader.TryPeek(out value));
                Assert.Equal(i + 1, value);
            }
        }
Example #8
0
        public void TryReadTo_Sequence(bool advancePastDelimiter, bool useEscapeOverload)
        {
            ReadOnlySequence <byte> bytes = SequenceFactory.Create(new byte[][] {
                new byte[] { 0 },
                new byte[] { 1, 2 },
                new byte[] { },
                new byte[] { 3, 4, 5, 6 }
            });

            ByteBufferReader reader = new ByteBufferReader(bytes);

            // Read to 0-5
            for (byte i = 0; i < bytes.Length - 1; i++)
            {
                ByteBufferReader 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());
                ReadOnlySequence <byte> .Enumerator 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));
            }
        }
Example #9
0
        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);
        }
Example #10
0
        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);
        }