Example #1
0
        public static bool TryParse <TSequence>(ref BufferReader <TSequence> reader, out ulong value)
            where TSequence : ISequence <ReadOnlyMemory <byte> >
        {
            var unread = reader.UnreadSegment;

            if (Utf8Parser.TryParse(unread, out value, out int consumed))
            {
                if (unread.Length > consumed)
                {
                    reader.Advance(consumed);
                    return(true);
                }
            }

            Span <byte> tempSpan = stackalloc byte[30];
            var         copied   = BufferReader.Peek(reader, tempSpan);

            if (Utf8Parser.TryParse(tempSpan.Slice(0, copied), out value, out consumed))
            {
                reader.Advance(consumed);
                return(true);
            }

            return(false);
        }
Example #2
0
        public static bool TryRead <TSequence>(ref BufferReader <TSequence> reader, out int value, bool littleEndian = false)
            where TSequence : ISequence <ReadOnlyMemory <byte> >
        {
            var unread = reader.UnreadSegment;

            if (littleEndian)
            {
                if (BinaryPrimitives.TryReadInt32LittleEndian(unread, out value))
                {
                    reader.Advance(sizeof(int));
                    return(true);
                }
            }
            else if (BinaryPrimitives.TryReadInt32BigEndian(unread, out value))
            {
                reader.Advance(sizeof(int));
                return(true);
            }

            Span <byte> tempSpan = stackalloc byte[4];
            var         copied   = BufferReader.Peek(reader, tempSpan);

            if (copied < 4)
            {
                value = default;
                return(false);
            }

            if (littleEndian)
            {
                value = BinaryPrimitives.ReadInt32LittleEndian(tempSpan);
            }
            else
            {
                value = BinaryPrimitives.ReadInt32BigEndian(tempSpan);
            }
            reader.Advance(sizeof(int));
            return(true);
        }