Exemplo n.º 1
0
        public ReadWriteBytes(SequencePosition first, SequencePosition last)
        {
            (_start, _startIndex) = first.Get <object>();
            (_end, _endIndex)     = last.Get <object>();

            Validate();
        }
Exemplo n.º 2
0
        public SequencePosition GetPosition(SequencePosition origin, long offset)
        {
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }

            var previous = origin;

            while (TryGet(ref origin, out var memory))
            {
                var length = memory.Length;
                if (length < offset)
                {
                    offset  -= length;
                    previous = origin;
                }
                else
                {
                    var(segment, index) = origin.Get <ReadOnlySequenceSegment <byte> >();
                    return(new SequencePosition(segment, (int)(index + offset)));
                }
            }
            throw new ArgumentOutOfRangeException(nameof(offset));
        }
Exemplo n.º 3
0
        public ReadWriteBytes Slice(SequencePosition start, SequencePosition end)
        {
            var kind = Kind;

            switch (kind)
            {
            case Type.Array:
                var(array, index) = start.Get <byte[]>();
                return(new ReadWriteBytes(array, index, end.GetInteger() - index));

            case Type.MemoryList:
                var(startList, startIndex) = start.Get <ReadOnlySequenceSegment <byte> >();
                var(endList, endIndex)     = end.Get <ReadOnlySequenceSegment <byte> >();
                return(new ReadWriteBytes(startList, startIndex, endList, endIndex));

            default:
                throw new NotImplementedException();
            }
        }
Exemplo n.º 4
0
        public bool TryGet(ref SequencePosition position, out ReadOnlyMemory <byte> item, bool advance = true)
        {
            if (position == default)
            {
                item = default;
                return(false);
            }

            var(buffer, index) = position.Get <BufferSequence>();
            item = buffer.WrittenMemory.Slice(index);
            if (advance)
            {
                position = new SequencePosition(Next, 0);
            }
            return(true);
        }
Exemplo n.º 5
0
        public ReadWriteBytes Slice(SequencePosition position)
        {
            var kind = Kind;

            switch (kind)
            {
            case Type.Array:
                var(array, index) = position.Get <byte[]>();
                return(new ReadWriteBytes(array, index, array.Length - index));

            case Type.MemoryList:
                return(Slice(position, new SequencePosition((ReadOnlySequenceSegment <byte>)_end, _endIndex)));

            default: throw new NotImplementedException();
            }
        }
Exemplo n.º 6
0
        public bool TryGet(ref SequencePosition position, out Memory <byte> item, bool advance = true)
        {
            if (position == default)
            {
                item = default;
                return(false);
            }

            var(list, index) = position.Get <BufferList>();
            item             = list._data.Slice(index);
            if (advance)
            {
                position = new SequencePosition(list._next, 0);
            }
            return(true);
        }
Exemplo n.º 7
0
        public bool TryGet(ref SequencePosition position, out Memory <byte> item, bool advance = true)
        {
            if (position == default)
            {
                item = default;
                return(false);
            }

            var array = _start as byte[];

            if (array != null)
            {
                var start  = _startIndex + position.GetInteger();
                var length = _endIndex - _startIndex - position.GetInteger();
                item = new Memory <byte>(array, start, length);
                if (advance)
                {
                    position = default;
                }
                return(true);
            }

            if (Kind == Type.MemoryList)
            {
                var(node, index) = position.Get <ReadOnlySequenceSegment <byte> >();
                item             = MemoryMarshal.AsMemory(node.Memory.Slice(index));
                if (ReferenceEquals(node, _end))
                {
                    item = item.Slice(0, _endIndex - index);
                    if (advance)
                    {
                        position = default;
                    }
                }
                else
                {
                    if (advance)
                    {
                        position = new SequencePosition(node.Next, 0);
                    }
                }
                return(true);
            }

            throw new NotImplementedException();
        }
Exemplo n.º 8
0
 public SequencePosition GetPosition(SequencePosition origin, long offset)
 {
     if (offset < 0)
     {
         throw new InvalidOperationException("cannot seek backwards");
     }
     var(node, index) = origin.Get <Node>();
     while (offset-- > 0)
     {
         if (node != null)
         {
             node = node._next;
         }
         else
         {
             throw new ArgumentOutOfRangeException(nameof(offset));
         }
     }
     return(new SequencePosition(node, 0));
 }
Exemplo n.º 9
0
        public bool TryGet(ref SequencePosition position, out T item, bool advance = true)
        {
            if (_count == 0 || position == default)
            {
                item = default;
                return(false);
            }

            var(node, index) = position.Get <Node>();
            if (node == null || index != 0)
            {
                item     = default;
                position = default;
                return(false);
            }

            item = node._item;
            if (advance)
            {
                position = new SequencePosition(node._next, 0);
            }
            return(true);
        }