| 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'; |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 Future sleep(int milliSeconds) { | 127 Future sleep(int milliSeconds) { |
| 128 var completer = new Completer(); | 128 var completer = new Completer(); |
| 129 new Timer(milliSeconds, completer.complete); | 129 new Timer(milliSeconds, completer.complete); |
| 130 return completer.future; | 130 return completer.future; |
| 131 } | 131 } |
| 132 | 132 |
| 133 /// Configures [future] so that its result (success or exception) is passed on | 133 /// Configures [future] so that its result (success or exception) is passed on |
| 134 /// to [completer]. | 134 /// to [completer]. |
| 135 void chainToCompleter(Future future, Completer completer) { | 135 void chainToCompleter(Future future, Completer completer) { |
| 136 future.handleException((e) { | 136 future.handleException((e) { |
| 137 completer.completeException(e); | 137 completer.completeException(e, future.stackTrace); |
| 138 return true; | 138 return true; |
| 139 }); | 139 }); |
| 140 future.then(completer.complete); | 140 future.then(completer.complete); |
| 141 } | 141 } |
| 142 | 142 |
| 143 /// Like [String.split], but only splits on the first occurrence of the pattern. | 143 /// Like [String.split], but only splits on the first occurrence of the pattern. |
| 144 /// This will always return an array of two elements or fewer. | 144 /// This will always return an array of two elements or fewer. |
| 145 List<String> split1(String toSplit, String pattern) { | 145 List<String> split1(String toSplit, String pattern) { |
| 146 if (toSplit.isEmpty) return <String>[]; | 146 if (toSplit.isEmpty) return <String>[]; |
| 147 | 147 |
| 148 var index = toSplit.indexOf(pattern); | 148 var index = toSplit.indexOf(pattern); |
| 149 if (index == -1) return [toSplit]; | 149 if (index == -1) return [toSplit]; |
| 150 return [toSplit.substring(0, index), | 150 return [toSplit.substring(0, index), |
| 151 toSplit.substring(index + pattern.length)]; | 151 toSplit.substring(index + pattern.length)]; |
| 152 } | 152 } |
| OLD | NEW |