Chromium Code Reviews| 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) { |