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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 return minuendSet; | 90 return minuendSet; |
91 } | 91 } |
92 | 92 |
93 /** | 93 /** |
94 * 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]. |
95 */ | 95 */ |
96 String replace(String source, Pattern matcher, String fn(Match)) { | 96 String replace(String source, Pattern matcher, String fn(Match)) { |
97 var buffer = new StringBuffer(); | 97 var buffer = new StringBuffer(); |
98 var start = 0; | 98 var start = 0; |
99 for (var match in matcher.allMatches(source)) { | 99 for (var match in matcher.allMatches(source)) { |
100 buffer.add(source.substring(start, match.start())); | 100 buffer.add(source.substring(start, match.start)); |
101 start = match.end(); | 101 start = match.end; |
102 buffer.add(fn(match)); | 102 buffer.add(fn(match)); |
103 } | 103 } |
104 buffer.add(source.substring(start)); | 104 buffer.add(source.substring(start)); |
105 return buffer.toString(); | 105 return buffer.toString(); |
106 } | 106 } |
107 | 107 |
108 /** | 108 /** |
109 * Returns whether or not [str] ends with [matcher]. | 109 * Returns whether or not [str] ends with [matcher]. |
110 */ | 110 */ |
111 bool endsWithPattern(String str, Pattern matcher) { | 111 bool endsWithPattern(String str, Pattern matcher) { |
112 for (var match in matcher.allMatches(str)) { | 112 for (var match in matcher.allMatches(str)) { |
113 if (match.end() == str.length) return true; | 113 if (match.end == str.length) return true; |
114 } | 114 } |
115 return false; | 115 return false; |
116 } | 116 } |
117 | 117 |
118 /** | 118 /** |
119 * Returns the hex-encoded sha1 hash of [source]. | 119 * Returns the hex-encoded sha1 hash of [source]. |
120 */ | 120 */ |
121 String sha1(String source) => | 121 String sha1(String source) => |
122 CryptoUtils.bytesToHex(new SHA1().update(source.charCodes).digest()); | 122 CryptoUtils.bytesToHex(new SHA1().update(source.charCodes).digest()); |
123 | 123 |
124 /** | 124 /** |
125 * Returns a [Future] that completes in [milliSeconds]. | 125 * Returns a [Future] that completes in [milliSeconds]. |
126 */ | 126 */ |
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 } |
OLD | NEW |