Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of dart.io; | 5 part of dart.io; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * String encodings. | 8 * String encodings. |
| 9 */ | 9 */ |
| 10 class Encoding { | 10 class Encoding { |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 252 var controller = new StreamController(sync: true); | 252 var controller = new StreamController(sync: true); |
| 253 controller.stream | 253 controller.stream |
| 254 .transform(new StringEncoder(encoding)) | 254 .transform(new StringEncoder(encoding)) |
| 255 .listen((data) => bytes = data); | 255 .listen((data) => bytes = data); |
| 256 controller.add(string); | 256 controller.add(string); |
| 257 controller.close(); | 257 controller.close(); |
| 258 assert(bytes != null); | 258 assert(bytes != null); |
| 259 return bytes; | 259 return bytes; |
| 260 } | 260 } |
| 261 | 261 |
| 262 /** | |
| 263 * Use [LineSplitter] from `dart:convert` instead. | |
|
floitsch
2013/08/07 12:58:26
If you feel like it (if not I can do it later):
Ad
kevmoo-old
2013/08/07 19:41:08
Not sure what to add to _collection_dev/iterable.d
| |
| 264 */ | |
| 265 @deprecated | |
| 266 class LineTransformer implements StreamTransformer<String, String> { | |
| 267 final _decoder = new LineSplitter(); | |
| 262 | 268 |
| 263 class LineTransformer extends StreamEventTransformer<String, String> { | 269 Stream<String> bind(Stream<String> stream) { |
| 264 static const int _LF = 10; | 270 return _decoder.bind(stream); |
| 265 static const int _CR = 13; | |
| 266 | |
| 267 StringBuffer _buffer = new StringBuffer(); | |
| 268 String _carry; | |
| 269 | |
| 270 void _handle(String data, EventSink<String> sink, bool isClosing) { | |
| 271 if (_carry != null) { | |
| 272 data = _carry + data; | |
| 273 _carry = null; | |
| 274 } | |
| 275 int startPos = 0; | |
| 276 int pos = 0; | |
| 277 while (pos < data.length) { | |
| 278 int skip = 0; | |
| 279 int char = data.codeUnitAt(pos); | |
| 280 if (char == _LF) { | |
| 281 skip = 1; | |
| 282 } else if (char == _CR) { | |
| 283 skip = 1; | |
| 284 if (pos + 1 < data.length) { | |
| 285 if (data.codeUnitAt(pos + 1) == _LF) { | |
| 286 skip = 2; | |
| 287 } | |
| 288 } else if (!isClosing) { | |
| 289 _carry = data.substring(startPos); | |
| 290 return; | |
| 291 } | |
| 292 } | |
| 293 if (skip > 0) { | |
| 294 _buffer.write(data.substring(startPos, pos)); | |
| 295 sink.add(_buffer.toString()); | |
| 296 _buffer = new StringBuffer(); | |
| 297 startPos = pos = pos + skip; | |
| 298 } else { | |
| 299 pos++; | |
| 300 } | |
| 301 } | |
| 302 if (pos != startPos) { | |
| 303 // Add remaining | |
| 304 _buffer.write(data.substring(startPos, pos)); | |
| 305 } | |
| 306 if (isClosing && !_buffer.isEmpty) { | |
| 307 sink.add(_buffer.toString()); | |
| 308 _buffer = new StringBuffer(); | |
| 309 } | |
| 310 } | |
| 311 | |
| 312 void handleData(String data, EventSink<String> sink) { | |
| 313 _handle(data, sink, false); | |
| 314 } | |
| 315 | |
| 316 void handleDone(EventSink<String> sink) { | |
| 317 _handle("", sink, true); | |
| 318 sink.close(); | |
| 319 } | 271 } |
| 320 } | 272 } |
| 321 | 273 |
| 322 | 274 |
| 323 abstract class _SingleByteDecoder | 275 abstract class _SingleByteDecoder |
| 324 extends StreamEventTransformer<List<int>, String> { | 276 extends StreamEventTransformer<List<int>, String> { |
| 325 final int _replacementChar; | 277 final int _replacementChar; |
| 326 | 278 |
| 327 _SingleByteDecoder(this._replacementChar); | 279 _SingleByteDecoder(this._replacementChar); |
| 328 | 280 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 411 | 363 |
| 412 // Utility class for decoding Windows current code page data delivered | 364 // Utility class for decoding Windows current code page data delivered |
| 413 // as a stream of bytes. | 365 // as a stream of bytes. |
| 414 class _WindowsCodePageDecoder extends StreamEventTransformer<List<int>, String> { | 366 class _WindowsCodePageDecoder extends StreamEventTransformer<List<int>, String> { |
| 415 void handleData(List<int> data, EventSink<String> sink) { | 367 void handleData(List<int> data, EventSink<String> sink) { |
| 416 sink.add(_decodeBytes(data)); | 368 sink.add(_decodeBytes(data)); |
| 417 } | 369 } |
| 418 | 370 |
| 419 external static String _decodeBytes(List<int> bytes); | 371 external static String _decodeBytes(List<int> bytes); |
| 420 } | 372 } |
| OLD | NEW |