예제 #1
0
        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);
        }
예제 #2
0
        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));
        }
예제 #3
0
        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);
예제 #4
0
        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);
        }
예제 #5
0
        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);
        }