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

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

Issue 462463003: Add optional startIndex to String.replaceFirst (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Little fixes Created 6 years, 4 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/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/lib/string_helper.dart
diff --git a/sdk/lib/_internal/lib/string_helper.dart b/sdk/lib/_internal/lib/string_helper.dart
index c134844a30c98dff720ac7b0f4920fde9688a661..ff2505729a1b3cce8352be876681941925776479 100644
--- a/sdk/lib/_internal/lib/string_helper.dart
+++ b/sdk/lib/_internal/lib/string_helper.dart
@@ -77,6 +77,14 @@ stringReplaceJS(receiver, replacer, to) {
return JS('String', r'#.replace(#, #)', receiver, replacer, to);
}
+stringReplaceFirstRE(receiver, regexp, to, startIndex) {
+ var match = regexp._execGlobal(receiver, startIndex);
+ if (match == null) return receiver;
+ var start = match.start;
+ var end = match.end;
+ return "${receiver.substring(0,start)}$to${receiver.substring(end)}";
+}
+
const String ESCAPE_REGEXP = r'[[\]{}()*+?.\\^$|]';
stringReplaceAllUnchecked(receiver, from, to) {
@@ -185,12 +193,16 @@ stringReplaceAllStringFuncUnchecked(receiver, pattern, onMatch, onNonMatch) {
}
-stringReplaceFirstUnchecked(receiver, from, to) {
+stringReplaceFirstUnchecked(receiver, from, to, [int startIndex = 0]) {
if (from is String) {
- return stringReplaceJS(receiver, from, to);
+ var index = receiver.indexOf(from, startIndex);
+ if (index < 0) return receiver;
+ return '${receiver.substring(0, index)}$to'
+ '${receiver.substring(index + from.length)}';
} else if (from is JSSyntaxRegExp) {
- var re = regExpGetNative(from);
- return stringReplaceJS(receiver, re, to);
+ return startIndex == 0 ?
+ stringReplaceJS(receiver, regExpGetNative(from), to) :
+ stringReplaceFirstRE(receiver, from, to, startIndex);
} else {
checkNull(from);
// TODO(floitsch): implement generic String.replace (with patterns).
« no previous file with comments | « sdk/lib/_internal/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