Chromium Code Reviews| Index: utils/pub/utils.dart |
| diff --git a/utils/pub/utils.dart b/utils/pub/utils.dart |
| index f0065faa72aa6a82ccc4964da65e318b65515c1b..35bc40a96bbe4f4e1d4f48ec6d5c594546121917 100644 |
| --- a/utils/pub/utils.dart |
| +++ b/utils/pub/utils.dart |
| @@ -16,6 +16,16 @@ class FormatException implements Exception { |
| String toString() => message; |
| } |
| +/** A pair of values. */ |
| +class Pair<E, F> { |
| + E first; |
| + F last; |
| + |
| + Pair(this.first, this.last); |
| + |
| + String toString() => '($first, $last)'; |
| +} |
| + |
| // TODO(rnystrom): Move into String? |
| /** Pads [source] to [length] by adding spaces at the end. */ |
| String padRight(String source, int length) { |
| @@ -70,3 +80,18 @@ only(Iterable iter) { |
| assert(!iterator.hasNext()); |
| return obj; |
| } |
| + |
| +/** |
| + * Replace each instance of [matcher] in [source] with the return value of [fn]. |
| + */ |
| +String replace(String source, Pattern matcher, String fn(Match)) { |
| + var buf = new StringBuffer(); |
|
Bob Nystrom
2012/06/22 22:35:44
"buf" -> "buffer".
nweiz
2012/06/25 22:25:17
Done.
|
| + var i = 0; |
|
Bob Nystrom
2012/06/22 22:35:44
"i" -> "start"
nweiz
2012/06/25 22:25:17
Done.
|
| + for (var match in matcher.allMatches(source)) { |
| + buf.add(source.substring(i, match.start())); |
| + i = match.end(); |
| + buf.add(fn(match)); |
| + } |
| + buf.add(source.substring(i)); |
| + return buf.toString(); |
| +} |