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

Unified Diff: frog/minfrog

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
« frog/lib/string_implementation.dart ('K') | « frog/lib/string_implementation.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: frog/minfrog
diff --git a/frog/minfrog b/frog/minfrog
index 8549688575ed3dd3df5c71a8d91235928b8cc8bd..77213654cfca4549a5b95a7d051ffb6c502ae568 100755
--- a/frog/minfrog
+++ b/frog/minfrog
@@ -1652,24 +1652,36 @@ StringImplementation.prototype.contains = function(pattern, startIndex) {
StringImplementation.prototype._replaceFirst = function(from, to) {
return this.replace(from, to);
}
-StringImplementation.prototype._replaceFirstRegExp = function(from, to) {
+StringImplementation.prototype._replaceRegExp = function(from, to) {
return this.replace(from.re, to);
}
StringImplementation.prototype.replaceFirst = function(from, to) {
if ((typeof(from) == 'string')) return this._replaceFirst(from, to);
- if (!!(from && from.is$RegExp())) return this._replaceFirstRegExp(from, to);
+ if (!!(from && from.is$RegExp())) return this._replaceRegExp(from, to);
var $$list = from.allMatches(this);
for (var $$i = from.allMatches(this).iterator(); $$i.hasNext$0(); ) {
var match = $$i.next$0();
return this.substring(0, match.start$0()) + to + this.substring(match.end$0());
}
}
+StringImplementation.prototype._replaceAll = function(from, to) {
+ from = new RegExp(from.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'g');
+ to = to.replace(/\$/g, '$$$$'); // Escape sequences are fun!
+ return this.replace(from, to);
+}
StringImplementation.prototype.replaceAll = function(from, to) {
- if (typeof(from) == 'string' || from instanceof String) {
- from = new RegExp(from.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'g');
- to = to.replace(/\$/g, '$$$$'); // Escape sequences are fun!
+ if ((typeof(from) == 'string')) return this._replaceAll(from, to);
+ if (!!(from && from.is$RegExp())) return this._replaceRegExp(from.get$dynamic().get$_global(), to);
+ var buffer = new StringBufferImpl("");
+ var lastMatchEnd = 0;
+ var $$list = from.allMatches(this);
+ for (var $$i = from.allMatches(this).iterator(); $$i.hasNext$0(); ) {
+ var match = $$i.next$0();
+ buffer.add$1(this.substring(lastMatchEnd, match.start$0()));
+ buffer.add$1(to);
+ lastMatchEnd = match.end$0();
}
- return this.replace(from, to);
+ buffer.add$1(this.substring(lastMatchEnd));
}
StringImplementation.prototype.hashCode = function() {
if (this.hash_ === undefined) {
« frog/lib/string_implementation.dart ('K') | « frog/lib/string_implementation.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698