Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(931)

Side by Side Diff: sdk/lib/convert/line_splitter.dart

Issue 22070003: convert: new LineSplitter, (io: Deprecate LineTransformer) (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: todos Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 StringConversionSink startChunkedConversion(
20 ChunkedConversionSink<String> sink) {
21
22 if (sink is! StringConversionSink) {
23 sink = new StringConversionSink.from(sink);
24 }
25 return new _LineSplitterSink(sink);
26 }
27 }
28
29 // TODO(floitsch): deal with utf8.
30 class _LineSplitterSink extends StringConversionSinkBase {
31 static const int _LF = 10;
32 static const int _CR = 13;
33
34 final StringConversionSink _sink;
35
36 String _carry;
37
38 _LineSplitterSink(this._sink);
39
40 void addSlice(String chunk, int start, int end, bool isLast) {
41 if(_carry != null) {
Søren Gjesse 2013/08/12 12:57:27 Space between if and (.
42 chunk = _carry + chunk.substring(start, end);
43 start = 0;
44 end = chunk.length;
45 _carry = null;
46 }
47 _carry = _addSlice(chunk, start, end, isLast, _sink.add);
48 if(isLast) _sink.close();
Søren Gjesse 2013/08/12 12:57:27 Ditto.
49 }
50
51 void close() {
52 addSlice('', 0, 0, true);
53 }
54
55 static String _addSlice(String chunk, int start, int end, bool isLast, void ad der(String val)) {
Søren Gjesse 2013/08/12 12:57:27 Long line.
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(start, end);
70 }
71 }
72 if (skip > 0) {
73 adder(chunk.substring(start, pos));
74 start = pos = pos + skip;
75 } else {
76 pos++;
77 }
78 }
79 if (pos != start) {
80 var carry = chunk.substring(start, pos);
81 if(isLast) {
Søren Gjesse 2013/08/12 12:57:27 Space between if and (.
82 // Add remaining
83 adder(carry);
84 } else {
85 return carry;
86 }
87 }
88 return null;
89 }
90 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698