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

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: 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 ChunkedConversionSink startChunkedConversion(ChunkedConversionSink<String> sin k) {
floitsch 2013/08/07 12:58:26 80 chars.
floitsch 2013/08/07 12:58:26 Change return type to StringConversionSink
kevmoo-old 2013/08/07 17:13:41 Done.
kevmoo-old 2013/08/07 17:13:41 Done.
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 val)) {
floitsch 2013/08/07 12:58:26 80 chars. No need to make this method static. By
kevmoo-old 2013/08/07 17:13:41 I use _addSlice in LineSplitter, hence the static.
54 int pos = start;
55 while (pos < end) {
56 int skip = 0;
57 int char = chunk.codeUnitAt(pos);
58 if (char == _LF) {
59 skip = 1;
60 } else if (char == _CR) {
61 skip = 1;
62 if (pos + 1 < end) {
63 if (chunk.codeUnitAt(pos + 1) == _LF) {
64 skip = 2;
65 }
66 } else if (!isLast) {
67 return chunk.substring(start, end);
68 }
69 }
70 if (skip > 0) {
71 adder(chunk.substring(start, pos));
72 start = pos = pos + skip;
73 } else {
74 pos++;
75 }
76 }
77 if (pos != start) {
78 var carry = chunk.substring(start, pos);
79 if(isLast) {
80 // Add remaining
81 adder(carry);
82 } else {
83 return carry;
84 }
85 }
86 return null;
87 }
88 }
floitsch 2013/08/07 12:58:26 Add TODO(floitsch): deal with utf8.
kevmoo-old 2013/08/07 17:13:41 Done.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698