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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 */ | 74 */ |
75 only(Iterable iter) { | 75 only(Iterable iter) { |
76 var iterator = iter.iterator(); | 76 var iterator = iter.iterator(); |
77 assert(iterator.hasNext()); | 77 assert(iterator.hasNext()); |
78 var obj = iterator.next(); | 78 var obj = iterator.next(); |
79 assert(!iterator.hasNext()); | 79 assert(!iterator.hasNext()); |
80 return obj; | 80 return obj; |
81 } | 81 } |
82 | 82 |
83 /** | 83 /** |
| 84 * Returns a set containing all elements in [minuend] that are not in |
| 85 * [subtrahend]. |
| 86 */ |
| 87 Set setMinus(Collection minuend, Collection subtrahend) { |
| 88 var minuendSet = new Set.from(minuend); |
| 89 minuendSet.removeAll(subtrahend); |
| 90 return minuendSet; |
| 91 } |
| 92 |
| 93 /** |
84 * Replace each instance of [matcher] in [source] with the return value of [fn]. | 94 * Replace each instance of [matcher] in [source] with the return value of [fn]. |
85 */ | 95 */ |
86 String replace(String source, Pattern matcher, String fn(Match)) { | 96 String replace(String source, Pattern matcher, String fn(Match)) { |
87 var buffer = new StringBuffer(); | 97 var buffer = new StringBuffer(); |
88 var start = 0; | 98 var start = 0; |
89 for (var match in matcher.allMatches(source)) { | 99 for (var match in matcher.allMatches(source)) { |
90 buffer.add(source.substring(start, match.start())); | 100 buffer.add(source.substring(start, match.start())); |
91 start = match.end(); | 101 start = match.end(); |
92 buffer.add(fn(match)); | 102 buffer.add(fn(match)); |
93 } | 103 } |
(...skipping 18 matching lines...) Expand all Loading... |
112 CryptoUtils.bytesToHex(new SHA1().update(source.charCodes()).digest()); | 122 CryptoUtils.bytesToHex(new SHA1().update(source.charCodes()).digest()); |
113 | 123 |
114 /** | 124 /** |
115 * Returns a [Future] that completes in [milliSeconds]. | 125 * Returns a [Future] that completes in [milliSeconds]. |
116 */ | 126 */ |
117 Future sleep(int milliSeconds) { | 127 Future sleep(int milliSeconds) { |
118 var completer = new Completer(); | 128 var completer = new Completer(); |
119 new Timer(milliSeconds, completer.complete); | 129 new Timer(milliSeconds, completer.complete); |
120 return completer.future; | 130 return completer.future; |
121 } | 131 } |
OLD | NEW |