Example #1
0
        public static bool TryReadUntill(ref ByteBufferReader reader, out ReadOnlySequence <byte> bytes, ReadOnlySpan <byte> delimiter)
        {
            if (delimiter.Length == 0)
            {
                bytes = default;
                return(true);
            }

            int matched = 0;
            var copy    = reader;
            var start   = reader.Position;
            var end     = reader.Position;

            while (!reader.End)
            {
                if (reader.Read() == delimiter[matched])
                {
                    matched++;
                }
                else
                {
                    end     = reader.Position;
                    matched = 0;
                }
                if (matched >= delimiter.Length)
                {
                    bytes = reader.Sequence.Slice(start, end);
                    return(true);
                }
            }
            reader = copy;
            bytes  = default;
            return(false);
        }
Example #2
0
        public static bool TryReadUntill(ref ByteBufferReader reader, out ReadOnlySequence <byte> bytes, byte delimiter)
        {
            var copy  = reader;
            var start = reader.Position;

            while (!reader.End)
            {
                SequencePosition end = reader.Position;
                if (reader.Read() == delimiter)
                {
                    bytes = reader.Sequence.Slice(start, end);
                    return(true);
                }
            }
            reader = copy;
            bytes  = default;
            return(false);
        }
Example #3
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 #4
0
        internal static int Peek(ByteBufferReader bytes, Span <byte> destination)
        {
            var first = bytes.UnreadSegment;

            if (first.Length > destination.Length)
            {
                first.Slice(0, destination.Length).CopyTo(destination);
                return(destination.Length);
            }
            else if (first.Length == destination.Length)
            {
                first.CopyTo(destination);
                return(destination.Length);
            }
            else
            {
                first.CopyTo(destination);
                int copied = first.Length;

                var next = bytes._nextSequencePosition;
                while (bytes._sequence.TryGet(ref next, out ReadOnlyMemory <byte> nextSegment, true))
                {
                    var nextSpan = nextSegment.Span;
                    if (nextSpan.Length > 0)
                    {
                        var toCopy = Math.Min(nextSpan.Length, destination.Length - copied);
                        nextSpan.Slice(0, toCopy).CopyTo(destination.Slice(copied));
                        copied += toCopy;
                        if (copied >= destination.Length)
                        {
                            break;
                        }
                    }
                }
                return(copied);
            }
        }
Example #5
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);
        }
Example #6
0
 public static int Peek(ByteBufferReader reader, Span <byte> destination)
 => ByteBufferReader.Peek(reader, destination);