OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /// Generic utility functions. Stuff that should possibly be in core. | 5 /// Generic utility functions. Stuff that should possibly be in core. |
6 library pub.utils; | 6 library pub.utils; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import "dart:collection"; | 9 import "dart:collection"; |
10 import "dart:convert"; | 10 import "dart:convert"; |
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 // fixed. | 400 // fixed. |
401 /// Splits [text] on its line breaks in a Windows-line-break-friendly way. | 401 /// Splits [text] on its line breaks in a Windows-line-break-friendly way. |
402 List<String> splitLines(String text) => | 402 List<String> splitLines(String text) => |
403 text.split("\n").map((line) => line.replaceFirst(_trailingCR, "")).toList(); | 403 text.split("\n").map((line) => line.replaceFirst(_trailingCR, "")).toList(); |
404 | 404 |
405 /// Converts a stream of arbitrarily chunked strings into a line-by-line stream. | 405 /// Converts a stream of arbitrarily chunked strings into a line-by-line stream. |
406 /// The lines don't include line termination characters. A single trailing | 406 /// The lines don't include line termination characters. A single trailing |
407 /// newline is ignored. | 407 /// newline is ignored. |
408 Stream<String> streamToLines(Stream<String> stream) { | 408 Stream<String> streamToLines(Stream<String> stream) { |
409 var buffer = new StringBuffer(); | 409 var buffer = new StringBuffer(); |
410 return stream.transform(new StreamTransformer( | 410 return stream.transform(new StreamTransformer.fromHandlers( |
411 handleData: (chunk, sink) { | 411 handleData: (chunk, sink) { |
412 var lines = splitLines(chunk); | 412 var lines = splitLines(chunk); |
413 var leftover = lines.removeLast(); | 413 var leftover = lines.removeLast(); |
414 for (var line in lines) { | 414 for (var line in lines) { |
415 if (!buffer.isEmpty) { | 415 if (!buffer.isEmpty) { |
416 buffer.write(line); | 416 buffer.write(line); |
417 line = buffer.toString(); | 417 line = buffer.toString(); |
418 buffer = new StringBuffer(); | 418 buffer = new StringBuffer(); |
419 } | 419 } |
420 | 420 |
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
734 error is DirectoryException || | 734 error is DirectoryException || |
735 error is FileException || | 735 error is FileException || |
736 error is HttpException || | 736 error is HttpException || |
737 error is HttpException || | 737 error is HttpException || |
738 error is LinkException || | 738 error is LinkException || |
739 error is OSError || | 739 error is OSError || |
740 error is ProcessException || | 740 error is ProcessException || |
741 error is SocketException || | 741 error is SocketException || |
742 error is WebSocketException; | 742 error is WebSocketException; |
743 } | 743 } |
OLD | NEW |