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 utils; | 6 library utils; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:crypto'; | 9 import 'dart:crypto'; |
10 import 'dart:isolate'; | 10 import 'dart:isolate'; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 return task; | 65 return task; |
66 } | 66 } |
67 | 67 |
68 Future<List> get future => _completer.future; | 68 Future<List> get future => _completer.future; |
69 } | 69 } |
70 | 70 |
71 // TODO(rnystrom): Move into String? | 71 // TODO(rnystrom): Move into String? |
72 /// Pads [source] to [length] by adding spaces at the end. | 72 /// Pads [source] to [length] by adding spaces at the end. |
73 String padRight(String source, int length) { | 73 String padRight(String source, int length) { |
74 final result = new StringBuffer(); | 74 final result = new StringBuffer(); |
75 result.add(source); | 75 result.write(source); |
76 | 76 |
77 while (result.length < length) { | 77 while (result.length < length) { |
78 result.add(' '); | 78 result.write(' '); |
79 } | 79 } |
80 | 80 |
81 return result.toString(); | 81 return result.toString(); |
82 } | 82 } |
83 | 83 |
84 /// Flattens nested lists inside an iterable into a single list containing only | 84 /// Flattens nested lists inside an iterable into a single list containing only |
85 /// non-list elements. | 85 /// non-list elements. |
86 List flatten(Iterable nested) { | 86 List flatten(Iterable nested) { |
87 var result = []; | 87 var result = []; |
88 helper(list) { | 88 helper(list) { |
(...skipping 26 matching lines...) Expand all Loading... |
115 minuendSet.removeAll(subtrahend); | 115 minuendSet.removeAll(subtrahend); |
116 return minuendSet; | 116 return minuendSet; |
117 } | 117 } |
118 | 118 |
119 /// Replace each instance of [matcher] in [source] with the return value of | 119 /// Replace each instance of [matcher] in [source] with the return value of |
120 /// [fn]. | 120 /// [fn]. |
121 String replace(String source, Pattern matcher, String fn(Match)) { | 121 String replace(String source, Pattern matcher, String fn(Match)) { |
122 var buffer = new StringBuffer(); | 122 var buffer = new StringBuffer(); |
123 var start = 0; | 123 var start = 0; |
124 for (var match in matcher.allMatches(source)) { | 124 for (var match in matcher.allMatches(source)) { |
125 buffer.add(source.substring(start, match.start)); | 125 buffer.write(source.substring(start, match.start)); |
126 start = match.end; | 126 start = match.end; |
127 buffer.add(fn(match)); | 127 buffer.write(fn(match)); |
128 } | 128 } |
129 buffer.add(source.substring(start)); | 129 buffer.write(source.substring(start)); |
130 return buffer.toString(); | 130 return buffer.toString(); |
131 } | 131 } |
132 | 132 |
133 /// Returns whether or not [str] ends with [matcher]. | 133 /// Returns whether or not [str] ends with [matcher]. |
134 bool endsWithPattern(String str, Pattern matcher) { | 134 bool endsWithPattern(String str, Pattern matcher) { |
135 for (var match in matcher.allMatches(str)) { | 135 for (var match in matcher.allMatches(str)) { |
136 if (match.end == str.length) return true; | 136 if (match.end == str.length) return true; |
137 } | 137 } |
138 return false; | 138 return false; |
139 } | 139 } |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 /// The lines don't include line termination characters. A single trailing | 235 /// The lines don't include line termination characters. A single trailing |
236 /// newline is ignored. | 236 /// newline is ignored. |
237 Stream<String> streamToLines(Stream<String> stream) { | 237 Stream<String> streamToLines(Stream<String> stream) { |
238 var buffer = new StringBuffer(); | 238 var buffer = new StringBuffer(); |
239 return wrapStream(stream.transform(new StreamTransformer( | 239 return wrapStream(stream.transform(new StreamTransformer( |
240 handleData: (chunk, sink) { | 240 handleData: (chunk, sink) { |
241 var lines = chunk.split(_lineRegexp); | 241 var lines = chunk.split(_lineRegexp); |
242 var leftover = lines.removeLast(); | 242 var leftover = lines.removeLast(); |
243 for (var line in lines) { | 243 for (var line in lines) { |
244 if (!buffer.isEmpty) { | 244 if (!buffer.isEmpty) { |
245 buffer.add(line); | 245 buffer.write(line); |
246 line = buffer.toString(); | 246 line = buffer.toString(); |
247 buffer.clear(); | 247 buffer = new StringBuffer(); |
248 } | 248 } |
249 | 249 |
250 sink.add(line); | 250 sink.add(line); |
251 } | 251 } |
252 buffer.add(leftover); | 252 buffer.write(leftover); |
253 }, | 253 }, |
254 handleDone: (sink) { | 254 handleDone: (sink) { |
255 if (!buffer.isEmpty) sink.add(buffer.toString()); | 255 if (!buffer.isEmpty) sink.add(buffer.toString()); |
256 sink.close(); | 256 sink.close(); |
257 }))); | 257 }))); |
258 } | 258 } |
259 | 259 |
260 // TODO(nweiz): remove this when issue 8310 is fixed. | 260 // TODO(nweiz): remove this when issue 8310 is fixed. |
261 /// Returns a [Stream] identical to [stream], but piped through a new | 261 /// Returns a [Stream] identical to [stream], but piped through a new |
262 /// [StreamController]. This exists to work around issue 8310. | 262 /// [StreamController]. This exists to work around issue 8310. |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 | 334 |
335 /// Add all key/value pairs from [source] to [destination], overwriting any | 335 /// Add all key/value pairs from [source] to [destination], overwriting any |
336 /// pre-existing values. | 336 /// pre-existing values. |
337 void mapAddAll(Map destination, Map source) => | 337 void mapAddAll(Map destination, Map source) => |
338 source.forEach((key, value) => destination[key] = value); | 338 source.forEach((key, value) => destination[key] = value); |
339 | 339 |
340 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes | 340 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes |
341 /// replacing `+` with ` `. | 341 /// replacing `+` with ` `. |
342 String urlDecode(String encoded) => | 342 String urlDecode(String encoded) => |
343 decodeUriComponent(encoded.replaceAll("+", " ")); | 343 decodeUriComponent(encoded.replaceAll("+", " ")); |
OLD | NEW |