Example #1
0
        public bool TryGet(ref Position position, out Memory <byte> item, bool advance = true)
        {
            if (position == default)
            {
                item = _data;
                if (advance)
                {
                    position.SetItem(_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.SetItem(sequence._next);
            }
            return(true);
        }
Example #2
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);
            }
        }