-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
line_splitter.dart
174 lines (155 loc) · 4.89 KB
/
line_splitter.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// @dart = 2.6
part of dart.convert;
// Character constants.
const int _LF = 10;
const int _CR = 13;
/// A [StreamTransformer] that splits a [String] into individual lines.
///
/// A line is terminated by either a CR (U+000D), a LF (U+000A), a
/// CR+LF sequence (DOS line ending),
/// and a final non-empty line can be ended by the end of the string.
///
/// The returned lines do not contain the line terminators.
class LineSplitter extends StreamTransformerBase<String, String> {
const LineSplitter();
/// Split [lines] into individual lines.
///
/// If [start] and [end] are provided, only split the contents of
/// `lines.substring(start, end)`. The [start] and [end] values must
/// specify a valid sub-range of [lines]
/// (`0 <= start <= end <= lines.length`).
static Iterable<String> split(String lines, [int start = 0, int end]) sync* {
end = RangeError.checkValidRange(start, end, lines.length);
var sliceStart = start;
var char = 0;
for (var i = start; i < end; i++) {
var previousChar = char;
char = lines.codeUnitAt(i);
if (char != _CR) {
if (char != _LF) continue;
if (previousChar == _CR) {
sliceStart = i + 1;
continue;
}
}
yield lines.substring(sliceStart, i);
sliceStart = i + 1;
}
if (sliceStart < end) {
yield lines.substring(sliceStart, end);
}
}
List<String> convert(String data) {
var lines = <String>[];
var end = data.length;
var sliceStart = 0;
var char = 0;
for (var i = 0; i < end; i++) {
var previousChar = char;
char = data.codeUnitAt(i);
if (char != _CR) {
if (char != _LF) continue;
if (previousChar == _CR) {
sliceStart = i + 1;
continue;
}
}
lines.add(data.substring(sliceStart, i));
sliceStart = i + 1;
}
if (sliceStart < end) {
lines.add(data.substring(sliceStart, end));
}
return lines;
}
StringConversionSink startChunkedConversion(Sink<String> sink) {
return _LineSplitterSink(
sink is StringConversionSink ? sink : StringConversionSink.from(sink));
}
Stream<String> bind(Stream<String> stream) {
return Stream<String>.eventTransformed(
stream, (EventSink<String> sink) => _LineSplitterEventSink(sink));
}
}
// TODO(floitsch): deal with utf8.
class _LineSplitterSink extends StringConversionSinkBase {
final StringConversionSink _sink;
/// The carry-over from the previous chunk.
///
/// If the previous slice ended in a line without a line terminator,
/// then the next slice may continue the line.
String _carry;
/// Whether to skip a leading LF character from the next slice.
///
/// If the previous slice ended on a CR character, a following LF
/// would be part of the same line termination, and should be ignored.
///
/// Only `true` when [_carry] is `null`.
bool _skipLeadingLF = false;
_LineSplitterSink(this._sink);
void addSlice(String chunk, int start, int end, bool isLast) {
end = RangeError.checkValidRange(start, end, chunk.length);
// If the chunk is empty, it's probably because it's the last one.
// Handle that here, so we know the range is non-empty below.
if (start >= end) {
if (isLast) close();
return;
}
if (_carry != null) {
assert(!_skipLeadingLF);
chunk = _carry + chunk.substring(start, end);
start = 0;
end = chunk.length;
_carry = null;
} else if (_skipLeadingLF) {
if (chunk.codeUnitAt(start) == _LF) {
start += 1;
}
_skipLeadingLF = false;
}
_addLines(chunk, start, end);
if (isLast) close();
}
void close() {
if (_carry != null) {
_sink.add(_carry);
_carry = null;
}
_sink.close();
}
void _addLines(String lines, int start, int end) {
var sliceStart = start;
var char = 0;
for (var i = start; i < end; i++) {
var previousChar = char;
char = lines.codeUnitAt(i);
if (char != _CR) {
if (char != _LF) continue;
if (previousChar == _CR) {
sliceStart = i + 1;
continue;
}
}
_sink.add(lines.substring(sliceStart, i));
sliceStart = i + 1;
}
if (sliceStart < end) {
_carry = lines.substring(sliceStart, end);
} else {
_skipLeadingLF = (char == _CR);
}
}
}
class _LineSplitterEventSink extends _LineSplitterSink
implements EventSink<String> {
final EventSink<String> _eventSink;
_LineSplitterEventSink(EventSink<String> eventSink)
: _eventSink = eventSink,
super(StringConversionSink.from(eventSink));
void addError(Object o, [StackTrace stackTrace]) {
_eventSink.addError(o, stackTrace);
}
}