| 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 [StreamTransformer] that splits a [String] into individual lines. | 12 * A [StreamTransformer] 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 | 20 |
| 21 class LineSplitter | 21 class LineSplitter extends Converter<String, List<String>> /*=Object*/ |
| 22 extends Converter<String, List<String>>/*=Object*/ | 22 implements |
| 23 implements ChunkedConverter<String, List<String>, String, String> | 23 ChunkedConverter<String, List<String>, String, String> |
| 24 /*=StreamTransformer<String, String>*/ { | 24 /*=StreamTransformer<String, String>*/ { |
| 25 | |
| 26 const LineSplitter(); | 25 const LineSplitter(); |
| 27 | 26 |
| 28 /// Split [lines] into individual lines. | 27 /// Split [lines] into individual lines. |
| 29 /// | 28 /// |
| 30 /// If [start] and [end] are provided, only split the contents of | 29 /// If [start] and [end] are provided, only split the contents of |
| 31 /// `lines.substring(start, end)`. The [start] and [end] values must | 30 /// `lines.substring(start, end)`. The [start] and [end] values must |
| 32 /// specify a valid sub-range of [lines] | 31 /// specify a valid sub-range of [lines] |
| 33 /// (`0 <= start <= end <= lines.length`). | 32 /// (`0 <= start <= end <= lines.length`). |
| 34 static Iterable<String> split(String lines, [int start = 0, int end]) sync* { | 33 static Iterable<String> split(String lines, [int start = 0, int end]) sync* { |
| 35 end = RangeError.checkValidRange(start, end, lines.length); | 34 end = RangeError.checkValidRange(start, end, lines.length); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 | 78 |
| 80 StringConversionSink startChunkedConversion(Sink<String> sink) { | 79 StringConversionSink startChunkedConversion(Sink<String> sink) { |
| 81 if (sink is! StringConversionSink) { | 80 if (sink is! StringConversionSink) { |
| 82 sink = new StringConversionSink.from(sink); | 81 sink = new StringConversionSink.from(sink); |
| 83 } | 82 } |
| 84 return new _LineSplitterSink(sink); | 83 return new _LineSplitterSink(sink); |
| 85 } | 84 } |
| 86 | 85 |
| 87 Stream/*<String>*/ bind(Stream<String> stream) { | 86 Stream/*<String>*/ bind(Stream<String> stream) { |
| 88 return new Stream<String>.eventTransformed( | 87 return new Stream<String>.eventTransformed( |
| 89 stream, | 88 stream, (EventSink<String> sink) => new _LineSplitterEventSink(sink)); |
| 90 (EventSink<String> sink) => new _LineSplitterEventSink(sink)); | |
| 91 } | 89 } |
| 92 } | 90 } |
| 93 | 91 |
| 94 // TODO(floitsch): deal with utf8. | 92 // TODO(floitsch): deal with utf8. |
| 95 class _LineSplitterSink extends StringConversionSinkBase { | 93 class _LineSplitterSink extends StringConversionSinkBase { |
| 96 final StringConversionSink _sink; | 94 final StringConversionSink _sink; |
| 97 | 95 |
| 98 /// The carry-over from the previous chunk. | 96 /// The carry-over from the previous chunk. |
| 99 /// | 97 /// |
| 100 /// 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, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 119 if (isLast) close(); | 117 if (isLast) close(); |
| 120 return; | 118 return; |
| 121 } | 119 } |
| 122 if (_carry != null) { | 120 if (_carry != null) { |
| 123 assert(!_skipLeadingLF); | 121 assert(!_skipLeadingLF); |
| 124 chunk = _carry + chunk.substring(start, end); | 122 chunk = _carry + chunk.substring(start, end); |
| 125 start = 0; | 123 start = 0; |
| 126 end = chunk.length; | 124 end = chunk.length; |
| 127 _carry = null; | 125 _carry = null; |
| 128 } else if (_skipLeadingLF) { | 126 } else if (_skipLeadingLF) { |
| 129 if (chunk.codeUnitAt(start) == _LF) { | 127 if (chunk.codeUnitAt(start) == _LF) { |
| 130 start += 1; | 128 start += 1; |
| 131 } | 129 } |
| 132 _skipLeadingLF = false; | 130 _skipLeadingLF = false; |
| 133 } | 131 } |
| 134 _addLines(chunk, start, end); | 132 _addLines(chunk, start, end); |
| 135 if (isLast) close(); | 133 if (isLast) close(); |
| 136 } | 134 } |
| 137 | 135 |
| 138 void close() { | 136 void close() { |
| 139 if (_carry != null) { | 137 if (_carry != null) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 final EventSink<String> _eventSink; | 170 final EventSink<String> _eventSink; |
| 173 | 171 |
| 174 _LineSplitterEventSink(EventSink<String> eventSink) | 172 _LineSplitterEventSink(EventSink<String> eventSink) |
| 175 : _eventSink = eventSink, | 173 : _eventSink = eventSink, |
| 176 super(new StringConversionSink.from(eventSink)); | 174 super(new StringConversionSink.from(eventSink)); |
| 177 | 175 |
| 178 void addError(Object o, [StackTrace stackTrace]) { | 176 void addError(Object o, [StackTrace stackTrace]) { |
| 179 _eventSink.addError(o, stackTrace); | 177 _eventSink.addError(o, stackTrace); |
| 180 } | 178 } |
| 181 } | 179 } |
| OLD | NEW |