SourceSpan subspan()

in lib/src/span.dart [186:233]


  SourceSpan subspan(int start, [int? end]) {
    RangeError.checkValidRange(start, end, length);
    if (start == 0 && (end == null || end == length)) return this;

    final text = this.text;
    final startLocation = this.start;
    var line = startLocation.line;
    var column = startLocation.column;

    // Adjust [line] and [column] as necessary if the character at [i] in [text]
    // is a newline.
    void consumeCodePoint(int i) {
      final codeUnit = text.codeUnitAt(i);
      if (codeUnit == $lf ||
          // A carriage return counts as a newline, but only if it's not
          // followed by a line feed.
          (codeUnit == $cr &&
              (i + 1 == text.length || text.codeUnitAt(i + 1) != $lf))) {
        line += 1;
        column = 0;
      } else {
        column += 1;
      }
    }

    for (var i = 0; i < start; i++) {
      consumeCodePoint(i);
    }

    final newStartLocation = SourceLocation(startLocation.offset + start,
        sourceUrl: sourceUrl, line: line, column: column);

    SourceLocation newEndLocation;
    if (end == null || end == length) {
      newEndLocation = this.end;
    } else if (end == start) {
      newEndLocation = newStartLocation;
    } else {
      for (var i = start; i < end; i++) {
        consumeCodePoint(i);
      }
      newEndLocation = SourceLocation(startLocation.offset + end,
          sourceUrl: sourceUrl, line: line, column: column);
    }

    return SourceSpan(
        newStartLocation, newEndLocation, text.substring(start, end));
  }