| Index: sdk/lib/convert/line_splitter.dart
|
| diff --git a/sdk/lib/convert/line_splitter.dart b/sdk/lib/convert/line_splitter.dart
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..5093e73edd0a64de3634059167f4d92b416a4119
|
| --- /dev/null
|
| +++ b/sdk/lib/convert/line_splitter.dart
|
| @@ -0,0 +1,89 @@
|
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
|
| +// for details. All rights reserved. Use of this source code is governed by a
|
| +// BSD-style license that can be found in the LICENSE file.
|
| +
|
| +part of dart.convert;
|
| +
|
| +/**
|
| + * This class splits [String] values into individual lines.
|
| + */
|
| +class LineSplitter extends Converter<String, List<String>> {
|
| + List<String> convert(String data) {
|
| + var lines = new List<String>();
|
| +
|
| + _LineSplitterSink._addSlice(data, 0, data.length, true, lines.add);
|
| +
|
| + return lines;
|
| + }
|
| +
|
| + ChunkedConversionSink startChunkedConversion(ChunkedConversionSink<String> sink) {
|
| + if (sink is! StringConversionSink) {
|
| + sink = new StringConversionSink.from(sink);
|
| + }
|
| + return new _LineSplitterSink(sink);
|
| + }
|
| +}
|
| +
|
| +
|
| +class _LineSplitterSink extends StringConversionSinkBase {
|
| + static const int _LF = 10;
|
| + static const int _CR = 13;
|
| +
|
| + final StringConversionSink _sink;
|
| +
|
| + String _carry;
|
| +
|
| + _LineSplitterSink(this._sink);
|
| +
|
| + void addSlice(String chunk, int start, int end, bool isLast) {
|
| + if(_carry != null) {
|
| + chunk = _carry + chunk.substring(start, end);
|
| + start = 0;
|
| + end = chunk.length;
|
| + _carry = null;
|
| + }
|
| + _carry = _addSlice(chunk, start, end, isLast, _sink.add);
|
| + if(isLast) _sink.close();
|
| + }
|
| +
|
| + void close() {
|
| + addSlice('', 0, 0, true);
|
| + }
|
| +
|
| + static String _addSlice(String chunk, int start, int end, bool isLast, void adder(String)) {
|
| + String carry = null;
|
| + int startPos = start;
|
| + int pos = start;
|
| + while (pos < end) {
|
| + int skip = 0;
|
| + int char = chunk.codeUnitAt(pos);
|
| + if (char == _LF) {
|
| + skip = 1;
|
| + } else if (char == _CR) {
|
| + skip = 1;
|
| + if (pos + 1 < end) {
|
| + if (chunk.codeUnitAt(pos + 1) == _LF) {
|
| + skip = 2;
|
| + }
|
| + } else if (!isLast) {
|
| + return chunk.substring(startPos, end);
|
| + }
|
| + }
|
| + if (skip > 0) {
|
| + adder(chunk.substring(startPos, pos));
|
| + startPos = pos = pos + skip;
|
| + } else {
|
| + pos++;
|
| + }
|
| + }
|
| + if (pos != startPos) {
|
| + // Add remaining
|
| + carry = chunk.substring(startPos, pos);
|
| + }
|
| + if(isLast && carry != null) {
|
| + adder(carry);
|
| + return null;
|
| + }
|
| + return carry;
|
| + }
|
| +}
|
|
|