Пример #1
0
        public ReadWriteBytes(Collections.Sequences.Position first, Collections.Sequences.Position last)
        {
            (_start, _startIndex) = first.Get <object>();
            (_end, _endIndex)     = last.Get <object>();

            Validate();
        }
Пример #2
0
        public ReadWriteBytes Slice(Collections.Sequences.Position start, Collections.Sequences.Position end)
        {
            var kind = Kind;

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

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

            default:
                throw new NotImplementedException();
            }
        }
Пример #3
0
        public ReadWriteBytes Slice(Collections.Sequences.Position 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, Collections.Sequences.Position.Create((IMemoryList <byte>)_end, _endIndex)));

            default: throw new NotImplementedException();
            }
        }
Пример #4
0
        public bool TryGet(ref Collections.Sequences.Position position, out Memory <byte> item, bool advance = true)
        {
            if (position == default)
            {
                item = default;
                return(false);
            }

            var(list, index) = position.Get <MemoryList>();
            item             = list._data.Slice(index);
            if (advance)
            {
                position = Collections.Sequences.Position.Create(list._next);
            }
            return(true);
        }
Пример #5
0
        public bool TryGet(ref Collections.Sequences.Position 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 + (int)position;
                var length = _endIndex - _startIndex - (int)position;
                item = new Memory <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 = Collections.Sequences.Position.Create(node.Next);
                    }
                }
                return(true);
            }

            throw new NotImplementedException();
        }