LineIndex constructor

LineIndex(
  1. String text
)

Implementation

factory LineIndex(String text) {
  final starts = <int>[0];
  final len = text.length;
  for (var i = 0; i < len; i++) {
    final c = text.codeUnitAt(i);
    if (c == 0x0a) {
      starts.add(i + 1);
    } else if (c == 0x0d) {
      // `\r\n` is handled by the `\n` branch; a lone `\r` starts a new line.
      if (i + 1 >= len || text.codeUnitAt(i + 1) != 0x0a) {
        starts.add(i + 1);
      }
    }
  }
  return LineIndex._(text, starts);
}