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

Unified Diff: runtime/lib/string_base.dart

Issue 11312144: Allow function as replacement argument to String.replaceAll. (Closed) Base URL: https://dart.googlecode.com/svn/experimental/lib_v2/dart
Patch Set: Created 8 years, 1 month 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
« no previous file with comments | « no previous file | sdk/lib/_internal/compiler/implementation/lib/interceptors.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/lib/string_base.dart
diff --git a/runtime/lib/string_base.dart b/runtime/lib/string_base.dart
index 11b594ed9e562cebc71e654acdb8b433678031f5..81b01c9f70e138c93a398a65aba570bc033c533d 100644
--- a/runtime/lib/string_base.dart
+++ b/runtime/lib/string_base.dart
@@ -210,12 +210,28 @@ class _StringBase {
return buffer.add(this.substring(startIndex)).toString();
}
- String replaceAll(Pattern pattern, String replacement) {
+ String replaceAll(Pattern pattern, var replacement) {
if (pattern is! Pattern) {
throw new ArgumentError("${pattern} is not a Pattern");
}
+ if (replacement is Function) {
+ StringBuffer buffer = new StringBuffer();
+ int startIndex = 0;
+ for (Match match in pattern.allMatches(this)) {
+ if (match.start > startIndex) {
+ buffer.add(this.substring(startIndex, match.start));
+ }
+ buffer.add(replacement(match).toString());
+ startIndex = match.end;
+ }
+ if (startIndex < this.length) {
+ buffer.add(this.substring(startIndex));
+ }
+ return buffer.toString();
+ }
if (replacement is! String) {
- throw new ArgumentError("${replacement} is not a String");
+ throw new ArgumentError(
+ "${replacement} is not a String or Match->String function");
}
StringBuffer buffer = new StringBuffer();
int startIndex = 0;
« no previous file with comments | « no previous file | sdk/lib/_internal/compiler/implementation/lib/interceptors.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698