OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of _interceptors; | 5 part of _interceptors; |
6 | 6 |
7 /** | 7 /** |
8 * The interceptor class for [String]. The compiler recognizes this | 8 * The interceptor class for [String]. The compiler recognizes this |
9 * class as an interceptor, and changes references to [:this:] to | 9 * class as an interceptor, and changes references to [:this:] to |
10 * actually use the receiver of the method, which is generated as an extra | 10 * actually use the receiver of the method, which is generated as an extra |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 if (startIndex < 0 || startIndex > this.length) { | 76 if (startIndex < 0 || startIndex > this.length) { |
77 throw new RangeError.range(startIndex, 0, this.length); | 77 throw new RangeError.range(startIndex, 0, this.length); |
78 } | 78 } |
79 return stringReplaceFirstUnchecked(this, from, to, startIndex); | 79 return stringReplaceFirstUnchecked(this, from, to, startIndex); |
80 } | 80 } |
81 | 81 |
82 List<String> split(Pattern pattern) { | 82 List<String> split(Pattern pattern) { |
83 checkNull(pattern); | 83 checkNull(pattern); |
84 if (pattern is String) { | 84 if (pattern is String) { |
85 return JS('JSExtendableArray', r'#.split(#)', this, pattern); | 85 return JS('JSExtendableArray', r'#.split(#)', this, pattern); |
86 } else if (pattern is JSSyntaxRegExp) { | 86 } else if (pattern is JSSyntaxRegExp && regExpCaptureCount(pattern) == 0) { |
87 var re = regExpGetNative(pattern); | 87 var re = regExpGetNative(pattern); |
88 return JS('JSExtendableArray', r'#.split(#)', this, re); | 88 return JS('JSExtendableArray', r'#.split(#)', this, re); |
89 } else { | 89 } else { |
90 throw "String.split(Pattern) UNIMPLEMENTED"; | 90 return _defaultSplit(pattern); |
91 } | 91 } |
92 } | 92 } |
93 | 93 |
| 94 List<String> _defaultSplit(Pattern pattern) { |
| 95 List<String> result = <String>[]; |
| 96 // End of most recent match. That is, start of next part to add to result. |
| 97 int start = 0; |
| 98 // Length of most recent match. |
| 99 // Set >0, so no match on the empty string causes the result to be [""]. |
| 100 int length = 1; |
| 101 for (var match in pattern.allMatches(this)) { |
| 102 int matchStart = match.start; |
| 103 int matchEnd = match.end; |
| 104 length = matchEnd - matchStart; |
| 105 if (length == 0 && start == matchStart) { |
| 106 // An empty match right after another match is ignored. |
| 107 // This includes an empty match at the start of the string. |
| 108 continue; |
| 109 } |
| 110 int end = matchStart; |
| 111 result.add(this.substring(start, end)); |
| 112 start = matchEnd; |
| 113 } |
| 114 if (start < this.length || length > 0) { |
| 115 // An empty match at the end of the string does not cause a "" at the end. |
| 116 // A non-empty match ending at the end of the string does add a "". |
| 117 result.add(this.substring(start)); |
| 118 } |
| 119 return result; |
| 120 } |
| 121 |
94 bool startsWith(Pattern pattern, [int index = 0]) { | 122 bool startsWith(Pattern pattern, [int index = 0]) { |
95 checkInt(index); | 123 checkInt(index); |
96 if (index < 0 || index > this.length) { | 124 if (index < 0 || index > this.length) { |
97 throw new RangeError.range(index, 0, this.length); | 125 throw new RangeError.range(index, 0, this.length); |
98 } | 126 } |
99 if (pattern is String) { | 127 if (pattern is String) { |
100 String other = pattern; | 128 String other = pattern; |
101 int otherLength = other.length; | 129 int otherLength = other.length; |
102 int endIndex = index + otherLength; | 130 int endIndex = index + otherLength; |
103 if (endIndex > length) return false; | 131 if (endIndex > length) return false; |
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
434 */ | 462 */ |
435 class _CodeUnits extends UnmodifiableListBase<int> { | 463 class _CodeUnits extends UnmodifiableListBase<int> { |
436 /** The string that this is the code units of. */ | 464 /** The string that this is the code units of. */ |
437 String _string; | 465 String _string; |
438 | 466 |
439 _CodeUnits(this._string); | 467 _CodeUnits(this._string); |
440 | 468 |
441 int get length => _string.length; | 469 int get length => _string.length; |
442 int operator[](int i) => _string.codeUnitAt(i); | 470 int operator[](int i) => _string.codeUnitAt(i); |
443 } | 471 } |
OLD | NEW |