Chromium Code Reviews| 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..c1e25b985fa63d389ae080ef7f4fe382d16c2662 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,14 @@ 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)}'; |
|
Lasse Reichstein Nielsen
2014/08/20 08:58:53
Long line.
srawlins
2014/08/22 14:46:40
Done.
|
| } 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); |
|
Lasse Reichstein Nielsen
2014/08/20 08:58:53
Two long lines.
srawlins
2014/08/22 14:46:40
Done.
|
| } else { |
| checkNull(from); |
| // TODO(floitsch): implement generic String.replace (with patterns). |