slice method

Delta slice(
  1. int start, [
  2. int? end
])

The slice() method does not change the original string. The start and end parameters specifies the part of the string to extract. The end position is optional.

Implementation

Delta slice(int start, [int? end]) {
  final result = Delta();
  final iterator = _OpIterator(_operations);
  int index = 0;

  while ((end == null || index < end) && iterator.hasNext) {
    TextOperation? nextOp;
    if (index < start) {
      nextOp = iterator.next(start - index);
    } else {
      nextOp = iterator.next(end == null ? null : end - index);
      result.add(nextOp);
    }

    index += nextOp.length;
  }

  return result;
}