| 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 /** | 5 /** |
| 6 * Generic utility functions. Stuff that should possibly be in core. | 6 * Generic utility functions. Stuff that should possibly be in core. |
| 7 */ | 7 */ |
| 8 library utils; | 8 library utils; |
| 9 | 9 |
| 10 import 'dart:crypto'; | 10 import 'dart:crypto'; |
| 11 import 'dart:isolate'; | 11 import 'dart:isolate'; |
| 12 import 'dart:uri'; | 12 import 'dart:uri'; |
| 13 | 13 |
| 14 /** A pair of values. */ | 14 /** A pair of values. */ |
| 15 class Pair<E, F> { | 15 class Pair<E, F> { |
| 16 E first; | 16 E first; |
| 17 F last; | 17 F last; |
| 18 | 18 |
| 19 Pair(this.first, this.last); | 19 Pair(this.first, this.last); |
| 20 | 20 |
| 21 String toString() => '($first, $last)'; | 21 String toString() => '($first, $last)'; |
| 22 | 22 |
| 23 bool operator==(other) { | 23 bool operator==(other) { |
| 24 if (other is! Pair) return false; | 24 if (other is! Pair) return false; |
| 25 return other.first == first && other.last == last; | 25 return other.first == first && other.last == last; |
| 26 } | 26 } |
| 27 |
| 28 int get hashCode => first.hashCode ^ last.hashCode; |
| 27 } | 29 } |
| 28 | 30 |
| 29 // TODO(rnystrom): Move into String? | 31 // TODO(rnystrom): Move into String? |
| 30 /** Pads [source] to [length] by adding spaces at the end. */ | 32 /** Pads [source] to [length] by adding spaces at the end. */ |
| 31 String padRight(String source, int length) { | 33 String padRight(String source, int length) { |
| 32 final result = new StringBuffer(); | 34 final result = new StringBuffer(); |
| 33 result.add(source); | 35 result.add(source); |
| 34 | 36 |
| 35 while (result.length < length) { | 37 while (result.length < length) { |
| 36 result.add(' '); | 38 result.add(' '); |
| 37 } | 39 } |
| 38 | 40 |
| 39 return result.toString(); | 41 return result.toString(); |
| 40 } | 42 } |
| 41 | 43 |
| 42 /** | 44 /** |
| 43 * Runs [fn] after [future] completes, whether it completes successfully or not. | 45 * Runs [fn] after [future] completes, whether it completes successfully or not. |
| 44 * Essentially an asynchronous `finally` block. | 46 * Essentially an asynchronous `finally` block. |
| 45 */ | 47 */ |
| 46 always(Future future, fn()) { | 48 always(Future future, fn()) { |
| 47 var completer = new Completer(); | 49 var completer = new Completer(); |
| 48 future.then((_) => fn()); | 50 future.then((_) => fn()); |
| 49 future.handleException((_) { | 51 future.handleException((_) { |
| 50 fn(); | 52 fn(); |
| 51 return false; | 53 return false; |
| 52 }); | 54 }); |
| 53 } | 55 } |
| 54 | 56 |
| 55 /** | 57 /** |
| 56 * Flattens nested lists into a single list containing only non-list elements. | 58 * Flattens nested collections into a single list containing only non-list |
| 59 * elements. |
| 57 */ | 60 */ |
| 58 List flatten(List nested) { | 61 List flatten(Collection nested) { |
| 59 var result = []; | 62 var result = []; |
| 60 helper(list) { | 63 helper(list) { |
| 61 for (var element in list) { | 64 for (var element in list) { |
| 62 if (element is List) { | 65 if (element is List) { |
| 63 helper(element); | 66 helper(element); |
| 64 } else { | 67 } else { |
| 65 result.add(element); | 68 result.add(element); |
| 66 } | 69 } |
| 67 } | 70 } |
| 68 } | 71 } |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 | 196 |
| 194 /// Add all key/value pairs from [source] to [destination], overwriting any | 197 /// Add all key/value pairs from [source] to [destination], overwriting any |
| 195 /// pre-existing values. | 198 /// pre-existing values. |
| 196 void mapAddAll(Map destination, Map source) => | 199 void mapAddAll(Map destination, Map source) => |
| 197 source.forEach((key, value) => destination[key] = value); | 200 source.forEach((key, value) => destination[key] = value); |
| 198 | 201 |
| 199 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes | 202 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes |
| 200 /// replacing `+` with ` `. | 203 /// replacing `+` with ` `. |
| 201 String urlDecode(String encoded) => | 204 String urlDecode(String encoded) => |
| 202 decodeUriComponent(encoded.replaceAll("+", " ")); | 205 decodeUriComponent(encoded.replaceAll("+", " ")); |
| OLD | NEW |