Chromium Code Reviews| 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; |
| /* |