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

Side by Side Diff: test/generated_sdk/lib/convert/string_conversion.dart

Issue 1162723007: remove generated_sdk from checked in code (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 5 years, 6 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
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 provides an interface for converters to
9 * efficiently transmit String data.
10 *
11 * Instead of limiting the interface to one non-chunked String it accepts
12 * partial strings or can be transformed into a byte sink that
13 * accepts UTF-8 code units.
14 *
15 * This abstract class will likely get more methods over time. Implementers are
16 * urged to extend [StringConversionSinkBase] or to mix in
17 * [StringConversionSinkMixin], to ensure that their class covers the newly
18 * added methods.
19 */
20 abstract class StringConversionSink
21 extends ChunkedConversionSink<String> {
22 StringConversionSink();
23 factory StringConversionSink.withCallback(void callback(String accumulated))
24 = _StringCallbackSink;
25 factory StringConversionSink.from(Sink<String> sink)
26 = _StringAdapterSink;
27
28 /**
29 * Creates a new instance wrapping the given [sink].
30 *
31 * Every string that is added to the returned instance is forwarded to
32 * the [sink]. The instance is allowed to buffer and is not required to
33 * forward immediately.
34 */
35 factory StringConversionSink.fromStringSink(StringSink sink) =
36 _StringSinkConversionSink;
37
38 /**
39 * Adds the next [chunk] to `this`.
40 *
41 * Adds the substring defined by [start] and [end]-exclusive to `this`.
42 *
43 * If [isLast] is `true` closes `this`.
44 */
45 void addSlice(String chunk, int start, int end, bool isLast);
46
47 /**
48 * Returns `this` as a sink that accepts UTF-8 input.
49 *
50 * If used, this method must be the first and only call to `this`. It
51 * invalidates `this`. All further operations must be performed on the result.
52 */
53 ByteConversionSink asUtf8Sink(bool allowMalformed);
54 // - asRuneSink
55 // - asCodeUnitsSink
56
57 /**
58 * Returns `this` as a [ClosableStringSink].
59 *
60 * If used, this method must be the first and only call to `this`. It
61 * invalidates `this`. All further operations must be performed on the result.
62 */
63 ClosableStringSink asStringSink();
64 }
65
66 /**
67 * A [ClosableStringSink] extends the [StringSink] interface by adding a
68 * `close` method.
69 */
70 abstract class ClosableStringSink extends StringSink {
71 /**
72 * Creates a new instance combining a [StringSink] [sink] and a callback
73 * [onClose] which is invoked when the returned instance is closed.
74 */
75 factory ClosableStringSink.fromStringSink(StringSink sink, void onClose())
76 = _ClosableStringSink;
77
78 /**
79 * Closes `this` and flushes any outstanding data.
80 */
81 void close();
82 }
83
84 typedef void _StringSinkCloseCallback();
85
86 /**
87 * This class wraps an existing [StringSink] and invokes a
88 * closure when [close] is invoked.
89 */
90 class _ClosableStringSink implements ClosableStringSink {
91 final _StringSinkCloseCallback _callback;
92 final StringSink _sink;
93
94 _ClosableStringSink(this._sink, this._callback);
95
96 void close() => _callback();
97
98 void writeCharCode(int charCode) => _sink.writeCharCode(charCode);
99 void write(Object o) => _sink.write(o);
100 void writeln([Object o = ""]) => _sink.writeln(o);
101 void writeAll(Iterable objects, [String separator = ""])
102 => _sink.writeAll(objects, separator);
103 }
104
105 /**
106 * This class wraps an existing [StringConversionSink] and exposes a
107 * [ClosableStringSink] interface. The wrapped sink only needs to implement
108 * `add` and `close`.
109 */
110 // TODO(floitsch): make this class public?
111 class _StringConversionSinkAsStringSinkAdapter implements ClosableStringSink {
112 static const _MIN_STRING_SIZE = 16;
113
114 StringBuffer _buffer;
115 StringConversionSink _chunkedSink;
116
117 _StringConversionSinkAsStringSinkAdapter(this._chunkedSink)
118 : _buffer = new StringBuffer();
119
120 void close() {
121 if (_buffer.isNotEmpty) _flush();
122 _chunkedSink.close();
123 }
124
125 void writeCharCode(int charCode) {
126 _buffer.writeCharCode(charCode);
127 if (_buffer.length > _MIN_STRING_SIZE) _flush();
128 }
129
130 void write(Object o) {
131 if (_buffer.isNotEmpty) _flush();
132 String str = o.toString();
133 _chunkedSink.add(o.toString());
134 }
135
136 void writeln([Object o = ""]) {
137 _buffer.writeln(o);
138 if (_buffer.length > _MIN_STRING_SIZE) _flush();
139 }
140
141 void writeAll(Iterable objects, [String separator = ""]) {
142 if (_buffer.isNotEmpty) _flush();
143 Iterator iterator = objects.iterator;
144 if (!iterator.moveNext()) return;
145 if (separator.isEmpty) {
146 do {
147 _chunkedSink.add(iterator.current.toString());
148 } while (iterator.moveNext());
149 } else {
150 _chunkedSink.add(iterator.current.toString());
151 while (iterator.moveNext()) {
152 write(separator);
153 _chunkedSink.add(iterator.current.toString());
154 }
155 }
156 }
157
158 void _flush() {
159 String accumulated = _buffer.toString();
160 _buffer.clear();
161 _chunkedSink.add(accumulated);
162 }
163 }
164
165 /**
166 * This class provides a base-class for converters that need to accept String
167 * inputs.
168 */
169 abstract class StringConversionSinkBase extends StringConversionSinkMixin {
170 }
171
172 /**
173 * This class provides a mixin for converters that need to accept String
174 * inputs.
175 */
176 abstract class StringConversionSinkMixin implements StringConversionSink {
177
178 void addSlice(String str, int start, int end, bool isLast);
179 void close();
180
181 void add(String str) => addSlice(str, 0, str.length, false);
182
183 ByteConversionSink asUtf8Sink(bool allowMalformed) {
184 return new _Utf8ConversionSink(this, allowMalformed);
185 }
186
187 ClosableStringSink asStringSink() {
188 return new _StringConversionSinkAsStringSinkAdapter(this);
189 }
190 }
191
192 /**
193 * This class is a [StringConversionSink] that wraps a [StringSink].
194 */
195 class _StringSinkConversionSink extends StringConversionSinkBase {
196 StringSink _stringSink;
197 _StringSinkConversionSink(StringSink this._stringSink);
198
199 void close() {}
200 void addSlice(String str, int start, int end, bool isLast) {
201 if (start != 0 || end != str.length) {
202 for (int i = start; i < end; i++) {
203 _stringSink.writeCharCode(str.codeUnitAt(i));
204 }
205 } else {
206 _stringSink.write(str);
207 }
208 if (isLast) close();
209 }
210
211 void add(String str) => _stringSink.write(str);
212
213 ByteConversionSink asUtf8Sink(bool allowMalformed) {
214 return new _Utf8StringSinkAdapter(this, _stringSink, allowMalformed);
215 }
216
217 ClosableStringSink asStringSink() {
218 return new ClosableStringSink.fromStringSink(_stringSink, this.close);
219 }
220 }
221
222 /**
223 * This class accumulates all chunks into one string
224 * and invokes a callback when the sink is closed.
225 *
226 * This class can be used to terminate a chunked conversion.
227 */
228 class _StringCallbackSink extends _StringSinkConversionSink {
229 final _ChunkedConversionCallback<String> _callback;
230 _StringCallbackSink(this._callback) : super(new StringBuffer());
231
232 void close() {
233 StringBuffer buffer = _stringSink;
234 String accumulated = buffer.toString();
235 buffer.clear();
236 _callback(accumulated);
237 }
238
239 ByteConversionSink asUtf8Sink(bool allowMalformed) {
240 return new _Utf8StringSinkAdapter(
241 this, _stringSink, allowMalformed);
242 }
243 }
244
245 /**
246 * This class adapts a simple [ChunkedConversionSink] to a
247 * [StringConversionSink].
248 *
249 * All additional methods of the [StringConversionSink] (compared to the
250 * ChunkedConversionSink) are redirected to the `add` method.
251 */
252 class _StringAdapterSink extends StringConversionSinkBase {
253 final Sink<String> _sink;
254
255 _StringAdapterSink(this._sink);
256
257 void add(String str) => _sink.add(str);
258
259 void addSlice(String str, int start, int end, bool isLast) {
260 if (start == 0 && end == str.length) {
261 add(str);
262 } else {
263 add(str.substring(start, end));
264 }
265 if (isLast) close();
266 }
267
268 void close() => _sink.close();
269 }
270
271
272 /**
273 * Decodes UTF-8 code units and stores them in a [StringSink].
274 */
275 class _Utf8StringSinkAdapter extends ByteConversionSink {
276 final _Utf8Decoder _decoder;
277 final Sink _sink;
278
279 _Utf8StringSinkAdapter(this._sink,
280 StringSink stringSink, bool allowMalformed)
281 : _decoder = new _Utf8Decoder(stringSink, allowMalformed);
282
283 void close() {
284 _decoder.close();
285 if(_sink != null) _sink.close();
286 }
287
288 void add(List<int> chunk) {
289 addSlice(chunk, 0, chunk.length, false);
290 }
291
292 void addSlice(List<int> codeUnits, int startIndex, int endIndex,
293 bool isLast) {
294 _decoder.convert(codeUnits, startIndex, endIndex);
295 if (isLast) close();
296 }
297 }
298
299 /**
300 * Decodes UTF-8 code units.
301 *
302 * Forwards the decoded strings to the given [StringConversionSink].
303 */
304 // TODO(floitsch): make this class public?
305 class _Utf8ConversionSink extends ByteConversionSink {
306
307 final _Utf8Decoder _decoder;
308 final StringConversionSink _chunkedSink;
309 final StringBuffer _buffer;
310 _Utf8ConversionSink(StringConversionSink sink, bool allowMalformed)
311 : this._(sink, new StringBuffer(), allowMalformed);
312
313 _Utf8ConversionSink._(this._chunkedSink, StringBuffer stringBuffer,
314 bool allowMalformed)
315 : _decoder = new _Utf8Decoder(stringBuffer, allowMalformed),
316 _buffer = stringBuffer;
317
318 void close() {
319 _decoder.close();
320 if (_buffer.isNotEmpty) {
321 String accumulated = _buffer.toString();
322 _buffer.clear();
323 _chunkedSink.addSlice(accumulated, 0, accumulated.length, true);
324 } else {
325 _chunkedSink.close();
326 }
327 }
328
329 void add(List<int> chunk) {
330 addSlice(chunk, 0, chunk.length, false);
331 }
332
333 void addSlice(List<int> chunk, int startIndex, int endIndex, bool isLast) {
334 _decoder.convert(chunk, startIndex, endIndex);
335 if (_buffer.isNotEmpty) {
336 String accumulated = _buffer.toString();
337 _chunkedSink.addSlice(accumulated, 0, accumulated.length, isLast);
338 _buffer.clear();
339 return;
340 }
341 if (isLast) close();
342 }
343 }
OLDNEW
« no previous file with comments | « test/generated_sdk/lib/convert/line_splitter.dart ('k') | test/generated_sdk/lib/convert/utf.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698