ReadWriteBytes SliceRest(int index, int length) { if (_rest == null) { if (First.Length == index && length == 0) { return(new ReadWriteBytes(Buffer <byte> .Empty)); } else { throw new ArgumentOutOfRangeException("index or length"); } } // TODO (pri 2): this could be optimized var rest = new ReadWriteBytes(_rest, length); rest = rest.Slice(index - First.Length, length); return(rest); }
public ReadWriteBytes Slice(Position from, Position to) { var(fromSegment, fromIndex) = from.Get <IMemoryList <byte> >(); var(toSegment, toIndex) = to.Get <IMemoryList <byte> >(); if (fromSegment == null) { var indexFrom = First.Length - fromIndex; var indexTo = First.Length - toIndex; return(Slice(indexFrom, indexTo - indexFrom + 1)); } 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 ReadWriteBytes(fromSegment, length + fromIndex); slice = slice.Slice(fromIndex); return(slice); }