| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of dart.convert; | |
| 6 | |
| 7 /** | |
| 8 * This class splits [String] values into individual lines. | |
| 9 */ | |
| 10 class LineSplitter extends Converter<String, List<String>> { | |
| 11 | |
| 12 const LineSplitter(); | |
| 13 | |
| 14 List<String> convert(String data) { | |
| 15 var lines = new List<String>(); | |
| 16 | |
| 17 _LineSplitterSink._addSlice(data, 0, data.length, true, lines.add); | |
| 18 | |
| 19 return lines; | |
| 20 } | |
| 21 | |
| 22 StringConversionSink startChunkedConversion(Sink<dynamic> sink) { | |
| 23 if (sink is! StringConversionSink) { | |
| 24 sink = new StringConversionSink.from(sink); | |
| 25 } | |
| 26 return new _LineSplitterSink(sink); | |
| 27 } | |
| 28 } | |
| 29 | |
| 30 // TODO(floitsch): deal with utf8. | |
| 31 class _LineSplitterSink extends StringConversionSinkBase { | |
| 32 static const int _LF = 10; | |
| 33 static const int _CR = 13; | |
| 34 | |
| 35 final StringConversionSink _sink; | |
| 36 | |
| 37 String _carry; | |
| 38 | |
| 39 _LineSplitterSink(this._sink); | |
| 40 | |
| 41 void addSlice(String chunk, int start, int end, bool isLast) { | |
| 42 if (_carry != null) { | |
| 43 chunk = _carry + chunk.substring(start, end); | |
| 44 start = 0; | |
| 45 end = chunk.length; | |
| 46 _carry = null; | |
| 47 } | |
| 48 _carry = _addSlice(chunk, start, end, isLast, _sink.add); | |
| 49 if (isLast) _sink.close(); | |
| 50 } | |
| 51 | |
| 52 void close() { | |
| 53 addSlice('', 0, 0, true); | |
| 54 } | |
| 55 | |
| 56 static String _addSlice(String chunk, int start, int end, bool isLast, | |
| 57 void adder(String val)) { | |
| 58 | |
| 59 int pos = start; | |
| 60 while (pos < end) { | |
| 61 int skip = 0; | |
| 62 int char = chunk.codeUnitAt(pos); | |
| 63 if (char == _LF) { | |
| 64 skip = 1; | |
| 65 } else if (char == _CR) { | |
| 66 skip = 1; | |
| 67 if (pos + 1 < end) { | |
| 68 if (chunk.codeUnitAt(pos + 1) == _LF) { | |
| 69 skip = 2; | |
| 70 } | |
| 71 } else if (!isLast) { | |
| 72 return chunk.substring(start, end); | |
| 73 } | |
| 74 } | |
| 75 if (skip > 0) { | |
| 76 adder(chunk.substring(start, pos)); | |
| 77 start = pos = pos + skip; | |
| 78 } else { | |
| 79 pos++; | |
| 80 } | |
| 81 } | |
| 82 if (pos != start) { | |
| 83 var carry = chunk.substring(start, pos); | |
| 84 if (isLast) { | |
| 85 // Add remaining | |
| 86 adder(carry); | |
| 87 } else { | |
| 88 return carry; | |
| 89 } | |
| 90 } | |
| 91 return null; | |
| 92 } | |
| 93 } | |
| OLD | NEW |