slice method

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

Implementation

String slice(int start, [int? end]) {
  var length = this.length;
  if (length <= 0) {
    return '';
  }
  end ??= length;

  if (start < 0) {
    start = -start > length ? 0 : (length + start);
  }
  if (end > length) {
    end = length;
  }
  if (end < 0) {
    end += length;
  }
  length = start > end ? 0 : (end - start).zeroFillRightShift(0);
  start = start.zeroFillRightShift(0);

  var index = -1;
  var result = '';
  while (++index < length) {
    result += this[index + start];
  }
  return result;
}