ReadOnlyBytes SliceRest(int index, int length) { if (_rest == null) { if (First.Length == index && length == 0) { return(new ReadOnlyBytes(ReadOnlyMemory <byte> .Empty)); } else { throw new ArgumentOutOfRangeException("index or length"); } } // TODO (pri 2): this could be optimized var rest = new ReadOnlyBytes(_rest, length); rest = rest.Slice(index - First.Length, length); return(rest); }
public ReadOnlyBytes Slice(Cursor from, Cursor to) { if (from._node == null) { var indexFrom = First.Length - from._index; var indexTo = First.Length - to._index; return(Slice(indexFrom, indexTo - indexFrom + 1)); } var headIndex = _all.Index + _all.First.Length - _first.Length; var newHeadIndex = from._node.Index + from._index; var newEndIndex = to._node.Index + to._index; var slicedOffFront = newHeadIndex - headIndex; var length = newEndIndex - newHeadIndex; // TODO: this could be optimized to avoid the Slice var a = new ReadOnlyBytes(from._node, length + from._index); a = a.Slice(from._index); return(a); }
public ReadOnlyBytes Slice(Position from, Position to) { var(fromSegment, fromIndex) = from.Get <IMemoryList <byte> >(); var(toSegment, toIndex) = to.Get <IMemoryList <byte> >(); if (fromSegment == null) { return(Slice(fromIndex - _totalLengthOrVirtualIndex, toIndex - fromIndex)); } var headIndex = _all.VirtualIndex + _all.Memory.Length - _first.Length; var newHeadIndex = fromSegment.VirtualIndex + fromIndex; var newEndIndex = toSegment.VirtualIndex + toIndex; var slicedOffFront = newHeadIndex - headIndex; var length = newEndIndex - newHeadIndex; // TODO: this could be optimized to avoid the Slice var slice = new ReadOnlyBytes(fromSegment, length + fromIndex); slice = slice.Slice(fromIndex); return(slice); }