Example #1
0
        public static bool TryRead(ref ByteBufferReader reader, out int value, bool littleEndian = false)
        {
            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);
        }
Example #2
0
        public static bool TryParse(ref ByteBufferReader reader, out ulong value)
        {
            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);
        }