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

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

Issue 677013002: Make dart2js String.split match the VM version. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Tweak docs. Created 6 years, 2 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
Index: sdk/lib/_internal/compiler/js_lib/js_string.dart
diff --git a/sdk/lib/_internal/compiler/js_lib/js_string.dart b/sdk/lib/_internal/compiler/js_lib/js_string.dart
index ec1ece176fc8da1ce7579e6a203f91aea73a8647..c7fd9aba51aa0702c034753a901475d7c965e070 100644
--- a/sdk/lib/_internal/compiler/js_lib/js_string.dart
+++ b/sdk/lib/_internal/compiler/js_lib/js_string.dart
@@ -83,14 +83,42 @@ class JSString extends Interceptor implements String, JSIndexable {
checkNull(pattern);
if (pattern is String) {
return JS('JSExtendableArray', r'#.split(#)', this, pattern);
- } else if (pattern is JSSyntaxRegExp) {
+ } else if (pattern is JSSyntaxRegExp && regExpCaptureCount(pattern) == 0) {
var re = regExpGetNative(pattern);
return JS('JSExtendableArray', r'#.split(#)', this, re);
} else {
- throw "String.split(Pattern) UNIMPLEMENTED";
+ return _defaultSplit(pattern);
}
}
+ List<String> _defaultSplit(Pattern pattern) {
+ List<String> result = <String>[];
+ // End of most recent match. That is, start of next part to add to result.
+ int start = 0;
+ // Length of most recent match.
+ // Set >0, so no match on the empty string causes the result to be [""].
+ int length = 1;
+ for (var match in pattern.allMatches(this)) {
+ int matchStart = match.start;
+ int matchEnd = match.end;
+ length = matchEnd - matchStart;
+ if (length == 0 && start == matchStart) {
+ // An empty match right after another match is ignored.
+ // This includes an empty match at the start of the string.
+ continue;
+ }
+ int end = matchStart;
+ result.add(this.substring(start, end));
+ start = matchEnd;
+ }
+ if (start < this.length || length > 0) {
+ // An empty match at the end of the string does not cause a "" at the end.
+ // A non-empty match ending at the end of the string does add a "".
+ result.add(this.substring(start));
+ }
+ return result;
+ }
+
bool startsWith(Pattern pattern, [int index = 0]) {
checkInt(index);
if (index < 0 || index > this.length) {
« no previous file with comments | « sdk/lib/_internal/compiler/js_lib/interceptors.dart ('k') | sdk/lib/_internal/compiler/js_lib/regexp_helper.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698