Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(569)

Unified Diff: utils/pub/utils.dart

Issue 10628016: Add a Pub source for pub.dartlang.org. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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();
+}

Powered by Google App Engine
This is Rietveld 408576698