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

Unified Diff: sdk/lib/_internal/compiler/implementation/lib/string_helper.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 | « sdk/lib/_internal/compiler/implementation/lib/interceptors.dart ('k') | sdk/lib/core/regexp.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/_internal/compiler/implementation/lib/string_helper.dart
diff --git a/sdk/lib/_internal/compiler/implementation/lib/string_helper.dart b/sdk/lib/_internal/compiler/implementation/lib/string_helper.dart
index 3a35778c677a431b508c26c8ed97d583c9c8b487..7b1b07e7ca4facc11f9b6dacb71665cd52192a95 100644
--- a/sdk/lib/_internal/compiler/implementation/lib/string_helper.dart
+++ b/sdk/lib/_internal/compiler/implementation/lib/string_helper.dart
@@ -33,7 +33,7 @@ class StringMatch implements Match {
List<Match> allMatchesInStringUnchecked(String needle, String haystack) {
// Copied from StringBase.allMatches in
- // ../../../runtime/lib/string.dart
+ // /runtime/lib/string_base.dart
List<Match> result = new List<Match>();
int length = haystack.length;
int patternLength = needle.length;
@@ -76,6 +76,10 @@ stringReplaceJS(receiver, replacer, to) {
}
stringReplaceAllUnchecked(receiver, from, to) {
+ if (to is Function) {
+ return stringReplaceAllFuncUnchecked(receiver, from, to);
+ }
+ checkString(to);
if (from is String) {
if (from == "") {
if (receiver == "") {
@@ -109,6 +113,57 @@ stringReplaceAllUnchecked(receiver, from, to) {
}
}
+stringReplaceAllFuncUnchecked(receiver, pattern, replace) {
+ if (pattern is! Pattern) {
+ throw new ArgumentError("${pattern} is not a Pattern");
+ }
+ if (pattern is String) {
+ // Inline the allMatches code.
+ return stringReplaceAllStringFuncUnchecked(receiver, pattern, replace);
+ }
+ StringBuffer buffer = new StringBuffer();
+ int startIndex = 0;
+ for (Match match in pattern.allMatches(receiver)) {
+ if (startIndex < match.start) {
+ buffer.add(receiver.substring(startIndex, match.start));
+ }
+ buffer.add(replace(match).toString());
+ startIndex = match.end;
+ }
+ if (startIndex < receiver.length) {
+ buffer.add(receiver.substring(startIndex));
+ }
+ return buffer.toString();
+}
+
+stringReplaceAllStringFuncUnchecked(receiver, pattern, replace) {
+ int length = receiver.length;
+ int patternLength = pattern.length;
+ int startIndex = 0;
+ StringBuffer buffer = new StringBuffer();
+ while (true) {
+ int position = receiver.indexOf(pattern, startIndex);
+ if (position == -1) {
+ if (startIndex < length) buffer.add(receiver.substring(startIndex));
+ return buffer.toString();
+ }
+ if (startIndex < position) {
+ buffer.add(receiver.substring(startIndex, position));
+ }
+ buffer.add(
+ replace(new StringMatch(position, receiver, pattern)).toString());
+ int endIndex = position + patternLength;
+ if (endIndex == length) {
+ return buffer.toString();
+ } else if (position == endIndex) {
+ ++startIndex; // empty match, advance and restart
+ } else {
+ startIndex = endIndex;
+ }
+ }
+}
+
+
stringReplaceFirstUnchecked(receiver, from, to) {
if (from is String) {
return stringReplaceJS(receiver, from, to);
« no previous file with comments | « sdk/lib/_internal/compiler/implementation/lib/interceptors.dart ('k') | sdk/lib/core/regexp.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698