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

Unified Diff: sdk/lib/_internal/compiler/js_lib/string_helper.dart

Issue 920453002: Add String.replaceFirstMapped. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: With change to _interpolate methods. Created 5 years, 10 months 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/js_lib/js_string.dart ('k') | sdk/lib/core/string.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/_internal/compiler/js_lib/string_helper.dart
diff --git a/sdk/lib/_internal/compiler/js_lib/string_helper.dart b/sdk/lib/_internal/compiler/js_lib/string_helper.dart
index ff2505729a1b3cce8352be876681941925776479..77b8ab9c34a95b95cb4c4c44604578362ffed8ee 100644
--- a/sdk/lib/_internal/compiler/js_lib/string_helper.dart
+++ b/sdk/lib/_internal/compiler/js_lib/string_helper.dart
@@ -193,21 +193,34 @@ stringReplaceAllStringFuncUnchecked(receiver, pattern, onMatch, onNonMatch) {
}
-stringReplaceFirstUnchecked(receiver, from, to, [int startIndex = 0]) {
+stringReplaceFirstUnchecked(receiver, from, to, int startIndex) {
if (from is String) {
- var index = receiver.indexOf(from, startIndex);
+ int index = receiver.indexOf(from, startIndex);
if (index < 0) return receiver;
return '${receiver.substring(0, index)}$to'
'${receiver.substring(index + from.length)}';
sra1 2015/02/17 17:01:00 This common pattern (3x) makes me think that we sh
- } else if (from is JSSyntaxRegExp) {
+ }
+ if (from is JSSyntaxRegExp) {
return startIndex == 0 ?
stringReplaceJS(receiver, regExpGetNative(from), to) :
stringReplaceFirstRE(receiver, from, to, startIndex);
- } else {
- checkNull(from);
- // TODO(floitsch): implement generic String.replace (with patterns).
- throw "String.replace(Pattern) UNIMPLEMENTED";
}
+ checkNull(from);
+ Iterator<Match> matches = from.allMatches(receiver, startIndex).iterator;
+ if (!matches.moveNext()) return receiver;
+ Match match = matches.current;
+ return '${receiver.substring(0, match.start)}$to'
+ '${receiver.substring(match.end)}';
+}
+
+stringReplaceFirstMappedUnchecked(receiver, from, replace,
+ int startIndex) {
+ Iterator<Match> matches = from.allMatches(receiver, startIndex).iterator;
+ if (!matches.moveNext()) return receiver;
+ Match match = matches.current;
+ String replacement = "${replace(match)}";
+ return '${receiver.substring(0, match.start)}$replacement'
+ '${receiver.substring(match.end)}';
}
stringJoinUnchecked(array, separator) {
« no previous file with comments | « sdk/lib/_internal/compiler/js_lib/js_string.dart ('k') | sdk/lib/core/string.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698