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

Unified Diff: frog/lib/string_implementation.dart

Issue 8907042: Support replaceAll for regexps. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 9 years 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: frog/lib/string_implementation.dart
diff --git a/frog/lib/string_implementation.dart b/frog/lib/string_implementation.dart
index 2f6ce1f24ad1e22abc23ea953597b446d71f15aa..5ec092c8b4ef6d502b99dbe8c01ebe40768b1db4 100644
--- a/frog/lib/string_implementation.dart
+++ b/frog/lib/string_implementation.dart
@@ -45,25 +45,36 @@ class StringImplementation implements String native "String" {
String _replaceFirst(String from, String to) native
"return this.replace(from, to);";
- String _replaceFirstRegExp(RegExp from, String to) native
+ String _replaceRegExp(RegExp from, String to) native
"return this.replace(from.re, to);";
String replaceFirst(Pattern from, String to) {
if (from is String) return _replaceFirst(from, to);
- if (from is RegExp) return _replaceFirstRegExp(from, to);
- for (match in from.allMatches(this)) {
+ if (from is RegExp) return _replaceRegExp(from, to);
+ for (var match in from.allMatches(this)) {
Jennifer Messerly 2011/12/14 20:36:56 Thanks for making this more consistent with the re
// We just care about the first match
return substring(0, match.start()) + to + substring(match.end());
}
}
- String replaceAll(Pattern from, String to) native @"""
-if (typeof(from) == 'string' || from instanceof String) {
- from = new RegExp(from.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'g');
- to = to.replace(/\$/g, '$$$$'); // Escape sequences are fun!
-}
+ String _replaceAll(String from, String to) native @"""
+from = new RegExp(from.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'g');
+to = to.replace(/\$/g, '$$$$'); // Escape sequences are fun!
return this.replace(from, to);""";
+ String replaceAll(Pattern from, String to) {
+ if (from is String) return _replaceAll(from, to);
+ if (from is RegExp) return _replaceRegExp(from.dynamic._global, to);
+ var buffer = new StringBuffer();
+ var lastMatchEnd = 0;
+ for (var match in from.allMatches(this)) {
+ buffer.add(substring(lastMatchEnd, match.start()));
+ buffer.add(to);
+ lastMatchEnd = match.end();
+ }
+ buffer.add(substring(lastMatchEnd));
+ }
+
List<String> split(Pattern pattern) native;
/*
« frog/lib/corelib_impl.dart ('K') | « frog/lib/corelib_impl.dart ('k') | frog/minfrog » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698