| 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 List<String> convert(String data) { |
| 12 var lines = new List<String>(); |
| 13 |
| 14 _LineSplitterSink._addSlice(data, 0, data.length, true, lines.add); |
| 15 |
| 16 return lines; |
| 17 } |
| 18 |
| 19 ChunkedConversionSink startChunkedConversion(ChunkedConversionSink<String> sin
k) { |
| 20 if (sink is! StringConversionSink) { |
| 21 sink = new StringConversionSink.from(sink); |
| 22 } |
| 23 return new _LineSplitterSink(sink); |
| 24 } |
| 25 } |
| 26 |
| 27 |
| 28 class _LineSplitterSink extends StringConversionSinkBase { |
| 29 static const int _LF = 10; |
| 30 static const int _CR = 13; |
| 31 |
| 32 final StringConversionSink _sink; |
| 33 |
| 34 String _carry; |
| 35 |
| 36 _LineSplitterSink(this._sink); |
| 37 |
| 38 void addSlice(String chunk, int start, int end, bool isLast) { |
| 39 if(_carry != null) { |
| 40 chunk = _carry + chunk.substring(start, end); |
| 41 start = 0; |
| 42 end = chunk.length; |
| 43 _carry = null; |
| 44 } |
| 45 _carry = _addSlice(chunk, start, end, isLast, _sink.add); |
| 46 if(isLast) _sink.close(); |
| 47 } |
| 48 |
| 49 void close() { |
| 50 addSlice('', 0, 0, true); |
| 51 } |
| 52 |
| 53 static String _addSlice(String chunk, int start, int end, bool isLast, void ad
der(String)) { |
| 54 String carry = null; |
| 55 int startPos = start; |
| 56 int pos = start; |
| 57 while (pos < end) { |
| 58 int skip = 0; |
| 59 int char = chunk.codeUnitAt(pos); |
| 60 if (char == _LF) { |
| 61 skip = 1; |
| 62 } else if (char == _CR) { |
| 63 skip = 1; |
| 64 if (pos + 1 < end) { |
| 65 if (chunk.codeUnitAt(pos + 1) == _LF) { |
| 66 skip = 2; |
| 67 } |
| 68 } else if (!isLast) { |
| 69 return chunk.substring(startPos, end); |
| 70 } |
| 71 } |
| 72 if (skip > 0) { |
| 73 adder(chunk.substring(startPos, pos)); |
| 74 startPos = pos = pos + skip; |
| 75 } else { |
| 76 pos++; |
| 77 } |
| 78 } |
| 79 if (pos != startPos) { |
| 80 // Add remaining |
| 81 carry = chunk.substring(startPos, pos); |
| 82 } |
| 83 if(isLast && carry != null) { |
| 84 adder(carry); |
| 85 return null; |
| 86 } |
| 87 return carry; |
| 88 } |
| 89 } |
| OLD | NEW |