OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.convert; | 5 part of dart.convert; |
6 | 6 |
7 // Character constants. | 7 // Character constants. |
8 const int _LF = 10; | 8 const int _LF = 10; |
9 const int _CR = 13; | 9 const int _CR = 13; |
10 | 10 |
11 /** | 11 /** |
12 * A [Converter] that splits a [String] into individual lines. | 12 * A [Converter] that splits a [String] into individual lines. |
13 * | 13 * |
14 * A line is terminated by either a CR (U+000D), a LF (U+000A), a | 14 * A line is terminated by either a CR (U+000D), a LF (U+000A), a |
15 * CR+LF sequence (DOS line ending), | 15 * CR+LF sequence (DOS line ending), |
16 * and a final non-empty line can be ended by the end of the string. | 16 * and a final non-empty line can be ended by the end of the string. |
17 * | 17 * |
18 * The returned lines do not contain the line terminators. | 18 * The returned lines do not contain the line terminators. |
19 */ | 19 */ |
20 class LineSplitter extends Converter<String, List<String>> { | 20 class LineSplitter extends |
| 21 ChunkedConverter<String, List<String>, String, String> { |
21 | 22 |
22 const LineSplitter(); | 23 const LineSplitter(); |
23 | 24 |
24 /// Split [lines] into individual lines. | 25 /// Split [lines] into individual lines. |
25 /// | 26 /// |
26 /// If [start] and [end] are provided, only split the contents of | 27 /// If [start] and [end] are provided, only split the contents of |
27 /// `lines.substring(start, end)`. The [start] and [end] values must | 28 /// `lines.substring(start, end)`. The [start] and [end] values must |
28 /// specify a valid sub-range of [lines] | 29 /// specify a valid sub-range of [lines] |
29 /// (`0 <= start <= end <= lines.length`). | 30 /// (`0 <= start <= end <= lines.length`). |
30 static Iterable<String> split(String lines, [int start = 0, int end]) sync* { | 31 static Iterable<String> split(String lines, [int start = 0, int end]) sync* { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 } | 73 } |
73 return lines; | 74 return lines; |
74 } | 75 } |
75 | 76 |
76 StringConversionSink startChunkedConversion(Sink<String> sink) { | 77 StringConversionSink startChunkedConversion(Sink<String> sink) { |
77 if (sink is! StringConversionSink) { | 78 if (sink is! StringConversionSink) { |
78 sink = new StringConversionSink.from(sink); | 79 sink = new StringConversionSink.from(sink); |
79 } | 80 } |
80 return new _LineSplitterSink(sink); | 81 return new _LineSplitterSink(sink); |
81 } | 82 } |
82 | |
83 // Override the base-class' bind, to provide a better type. | |
84 Stream<String> bind(Stream<String> stream) => super.bind(stream); | |
85 } | 83 } |
86 | 84 |
87 // TODO(floitsch): deal with utf8. | 85 // TODO(floitsch): deal with utf8. |
88 class _LineSplitterSink extends StringConversionSinkBase { | 86 class _LineSplitterSink extends StringConversionSinkBase { |
89 final StringConversionSink _sink; | 87 final StringConversionSink _sink; |
90 | 88 |
91 /// The carry-over from the previous chunk. | 89 /// The carry-over from the previous chunk. |
92 /// | 90 /// |
93 /// If the previous slice ended in a line without a line terminator, | 91 /// If the previous slice ended in a line without a line terminator, |
94 /// then the next slice may continue the line. | 92 /// then the next slice may continue the line. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 _sink.add(lines.substring(sliceStart, i)); | 150 _sink.add(lines.substring(sliceStart, i)); |
153 sliceStart = i + 1; | 151 sliceStart = i + 1; |
154 } | 152 } |
155 if (sliceStart < end) { | 153 if (sliceStart < end) { |
156 _carry = lines.substring(sliceStart, end); | 154 _carry = lines.substring(sliceStart, end); |
157 } else { | 155 } else { |
158 _skipLeadingLF = (char == _CR); | 156 _skipLeadingLF = (char == _CR); |
159 } | 157 } |
160 } | 158 } |
161 } | 159 } |
OLD | NEW |