예제 #1
0
        public bool TryGet(ref Position position, out Memory <byte> item, bool advance = true)
        {
            if (position == default)
            {
                item = _data;
                if (advance)
                {
                    position = Position.Create(_next);
                }
                return(!_data.IsEmpty || _next != null);
            }
            else if (position.IsEnd)
            {
                item = default;
                return(false);
            }

            var sequence = position.GetItem <MemoryList>();

            item = sequence._data;
            if (advance)
            {
                position = Position.Create(sequence._next);
            }
            return(true);
        }
예제 #2
0
 public static Position PositionOf(this IMemoryList <byte> sequence, byte value)
 {
     while (sequence != null)
     {
         var index = sequence.Memory.Span.IndexOf(value);
         if (index != -1)
         {
             return(Position.Create(index, sequence));
         }
         sequence = sequence.Rest;
     }
     return(Position.End);
 }
예제 #3
0
 public static Position?PositionOf(this IMemoryList <byte> list, byte value)
 {
     while (list != null)
     {
         var current = list.Memory.Span;
         var index   = current.IndexOf(value);
         if (index != -1)
         {
             return(Position.Create(list, index));
         }
         list = list.Next;
     }
     return(null);
 }
예제 #4
0
        public ReadOnlyBytes Slice(Position position)
        {
            var kind = Kind;

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

            case Type.MemoryList:
                return(Slice(position, Position.Create((IMemoryList <byte>)_end, _endIndex)));

            default: throw new NotImplementedException();
            }
        }
예제 #5
0
        public bool TryGet(ref Position position, out ReadOnlyMemory <byte> item, bool advance = true)
        {
            if (position == default)
            {
                item = default;
                return(false);
            }

            var array = _start as byte[];

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

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

            throw new NotImplementedException();
        }
예제 #6
0
        public Position PositionAt(int index)
        {
            ReadOnlySpan <byte> first = _first.Span;
            int firstLength           = first.Length;

            if (index < firstLength)
            {
                if (_all == null)
                {
                    return(Position.Create(index));
                }
                else
                {
                    var allIndex = index + (_all.Memory.Length - firstLength);
                    return(Position.Create(allIndex, _all));
                }
            }
            if (Rest == null)
            {
                return(default);
예제 #7
0
        public Position PositionOf(byte value)
        {
            ReadOnlySpan <byte> first = _first.Span;
            int index = first.IndexOf(value);

            if (index != -1)
            {
                if (_all == null)
                {
                    return(Position.Create(index));
                }
                else
                {
                    var allIndex = index + (_all.Memory.Length - first.Length);
                    return(Position.Create(allIndex, _all));
                }
            }
            if (Rest == null)
            {
                return(Position.End);
            }
            return(PositionOf(Rest, value));
        }
예제 #8
0
        public bool TryGet(ref Position position, out ReadOnlyMemory <byte> value, bool advance = true)
        {
            if (position == default)
            {
                value = _first;
                if (advance)
                {
                    position.SetItem(Rest);
                }
                return(!_first.IsEmpty || _all != null);
            }
            if (position.IsEnd)
            {
                value = default;
                return(false);
            }

            var(segment, index) = position.Get <IMemoryList <byte> >();

            if (segment == null)
            {
                if (_all == null) // single segment ROB
                {
                    value = _first.Slice(index - (int)_totalLengthOrVirtualIndex);
                    if (advance)
                    {
                        position = Position.End;
                    }
                    return(true);
                }
                else
                {
                    value = default;
                    return(false);
                }
            }

            var memory = segment.Memory;

            // We need to slice off the last segment based on length of this ROB.
            // This ROB is a potentially shorted view over a longer segment list.
            var virtualIndex  = segment.VirtualIndex;
            var lengthOfFirst = virtualIndex - VirtualIndex;
            var lengthOfEnd   = lengthOfFirst + memory.Length;

            if (lengthOfEnd > Length)
            {
                if (advance)
                {
                    position = Position.End;
                }
                value = memory.Slice(0, (int)(Length - lengthOfFirst));
                if (index < value.Length)
                {
                    if (index > 0)
                    {
                        value = value.Slice(index);
                    }
                    return(true);
                }
                else
                {
                    value = ReadOnlyMemory <byte> .Empty;
                    return(false);
                }
            }
            else
            {
                value = memory.Slice(index);
                if (advance)
                {
                    position = Position.Create(0, segment.Rest);
                }
                return(true);
            }
        }