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 | 20 class LineSplitter |
21 ChunkedConverter<String, List<String>, String, String> { | 21 extends Converter<String, List<String>>/*=Object*/ |
| 22 implements Object/*=StreamTransformer<String, String>*/ { |
22 | 23 |
23 const LineSplitter(); | 24 const LineSplitter(); |
24 | 25 |
25 /// Split [lines] into individual lines. | 26 /// Split [lines] into individual lines. |
26 /// | 27 /// |
27 /// If [start] and [end] are provided, only split the contents of | 28 /// If [start] and [end] are provided, only split the contents of |
28 /// `lines.substring(start, end)`. The [start] and [end] values must | 29 /// `lines.substring(start, end)`. The [start] and [end] values must |
29 /// specify a valid sub-range of [lines] | 30 /// specify a valid sub-range of [lines] |
30 /// (`0 <= start <= end <= lines.length`). | 31 /// (`0 <= start <= end <= lines.length`). |
31 static Iterable<String> split(String lines, [int start = 0, int end]) sync* { | 32 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... |
73 } | 74 } |
74 return lines; | 75 return lines; |
75 } | 76 } |
76 | 77 |
77 StringConversionSink startChunkedConversion(Sink<String> sink) { | 78 StringConversionSink startChunkedConversion(Sink<String> sink) { |
78 if (sink is! StringConversionSink) { | 79 if (sink is! StringConversionSink) { |
79 sink = new StringConversionSink.from(sink); | 80 sink = new StringConversionSink.from(sink); |
80 } | 81 } |
81 return new _LineSplitterSink(sink); | 82 return new _LineSplitterSink(sink); |
82 } | 83 } |
| 84 |
| 85 Stream/*<String>*/ bind(Stream/*<String>*/ stream) { |
| 86 return new Stream<String>.eventTransformed( |
| 87 stream, |
| 88 (EventSink<String> sink) => new _LineSplitterEventSink(sink)); |
| 89 } |
83 } | 90 } |
84 | 91 |
85 // TODO(floitsch): deal with utf8. | 92 // TODO(floitsch): deal with utf8. |
86 class _LineSplitterSink extends StringConversionSinkBase { | 93 class _LineSplitterSink extends StringConversionSinkBase { |
87 final StringConversionSink _sink; | 94 final StringConversionSink _sink; |
88 | 95 |
89 /// The carry-over from the previous chunk. | 96 /// The carry-over from the previous chunk. |
90 /// | 97 /// |
91 /// If the previous slice ended in a line without a line terminator, | 98 /// If the previous slice ended in a line without a line terminator, |
92 /// then the next slice may continue the line. | 99 /// then the next slice may continue the line. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 _sink.add(lines.substring(sliceStart, i)); | 157 _sink.add(lines.substring(sliceStart, i)); |
151 sliceStart = i + 1; | 158 sliceStart = i + 1; |
152 } | 159 } |
153 if (sliceStart < end) { | 160 if (sliceStart < end) { |
154 _carry = lines.substring(sliceStart, end); | 161 _carry = lines.substring(sliceStart, end); |
155 } else { | 162 } else { |
156 _skipLeadingLF = (char == _CR); | 163 _skipLeadingLF = (char == _CR); |
157 } | 164 } |
158 } | 165 } |
159 } | 166 } |
| 167 |
| 168 class _LineSplitterEventSink extends _LineSplitterSink |
| 169 implements EventSink<String> { |
| 170 final EventSink<String> _eventSink; |
| 171 |
| 172 _LineSplitterEventSink(EventSink<String> eventSink) |
| 173 : _eventSink = eventSink, |
| 174 super(new StringConversionSink.from(eventSink)); |
| 175 |
| 176 void addError(Object o, [StackTrace stackTrace]) { |
| 177 _eventSink.addError(o, stackTrace); |
| 178 } |
| 179 } |
OLD | NEW |