lines property
Lazily splits this into individual lines.
A line is terminated by either:
- a CR, carriage return: U+000D ('\r')
- a LF, line feed (Unix line break): U+000A ('\n') or
- a CR+LF sequence (DOS/Windows line break), and
- a final non-empty line can be ended by the end of the input.
The resulting lines do not contain the line terminators.
final string =
'Dart is: \r an object-oriented \n class-based \n garbage-collected '
'\r\n language with C-style syntax \r\n';
// 'Dart is: '
// ' an object-oriented '
// ' class-based '
// ' garbage-collected '
// ' language with C-style syntax '
for (final line in string.lines) {
print(line);
}
''.lines // []
This is a lazy alternative to LineSplitter.convert, which is eager.
Implementation
@lazy
@useResult Iterable<String> get lines sync* {
const LF = 10; // ignore: constant_identifier_names
const CR = 13; // ignore: constant_identifier_names
var start = 0;
for (var char = 0, i = 0; i < length; i++) {
final previous = char;
switch (char = codeUnitAt(i)) {
case != CR && != LF:
continue; // Normal characters
case != CR when previous == CR:
start = i + 1;
continue; // CR LF
default:
yield substring(start, i);
start = i + 1;
}
}
if (start < length) {
yield substring(start);
}
}