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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 | 97 |
98 /// Returns whether or not [str] ends with [matcher]. | 98 /// Returns whether or not [str] ends with [matcher]. |
99 bool endsWithPattern(String str, Pattern matcher) { | 99 bool endsWithPattern(String str, Pattern matcher) { |
100 for (var match in matcher.allMatches(str)) { | 100 for (var match in matcher.allMatches(str)) { |
101 if (match.end == str.length) return true; | 101 if (match.end == str.length) return true; |
102 } | 102 } |
103 return false; | 103 return false; |
104 } | 104 } |
105 | 105 |
106 /// Returns the hex-encoded sha1 hash of [source]. | 106 /// Returns the hex-encoded sha1 hash of [source]. |
107 String sha1(String source) => | 107 String sha1(String source) { |
108 CryptoUtils.bytesToHex(new SHA1().add(source.charCodes).close()); | 108 var sha = new SHA1(); |
| 109 sha.add(source.charCodes); |
| 110 return CryptoUtils.bytesToHex(sha.close()); |
| 111 } |
109 | 112 |
110 /// Returns a [Future] that completes in [milliseconds]. | 113 /// Returns a [Future] that completes in [milliseconds]. |
111 Future sleep(int milliseconds) { | 114 Future sleep(int milliseconds) { |
112 var completer = new Completer(); | 115 var completer = new Completer(); |
113 new Timer(milliseconds, completer.complete); | 116 new Timer(milliseconds, (_) => completer.complete()); |
114 return completer.future; | 117 return completer.future; |
115 } | 118 } |
116 | 119 |
117 /// Configures [future] so that its result (success or exception) is passed on | 120 /// Configures [future] so that its result (success or exception) is passed on |
118 /// to [completer]. | 121 /// to [completer]. |
119 void chainToCompleter(Future future, Completer completer) { | 122 void chainToCompleter(Future future, Completer completer) { |
120 future | 123 future |
121 .then(completer.complete) | 124 .then(completer.complete) |
122 .catchError((e) { | 125 .catchError((e) { |
123 completer.completeError(e.error, e.stackTrace); | 126 completer.completeError(e.error, e.stackTrace); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 // TODO(rnystrom): Remove this when #7781 is fixed. | 190 // TODO(rnystrom): Remove this when #7781 is fixed. |
188 /// When an error is rethrown in an async callback, you can end up with nested | 191 /// When an error is rethrown in an async callback, you can end up with nested |
189 /// AsyncErrors. This unwraps them to find the real originating error. | 192 /// AsyncErrors. This unwraps them to find the real originating error. |
190 getRealError(error) { | 193 getRealError(error) { |
191 while (error is AsyncError) { | 194 while (error is AsyncError) { |
192 error = error.error; | 195 error = error.error; |
193 } | 196 } |
194 | 197 |
195 return error; | 198 return error; |
196 } | 199 } |
OLD | NEW |