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 const int _maxAscii = 0x7f; | 5 const int _maxAscii = 0x7f; |
6 const int _maxLatin1 = 0xff; | 6 const int _maxLatin1 = 0xff; |
7 const int _maxUtf16 = 0xffff; | 7 const int _maxUtf16 = 0xffff; |
8 const int _maxUnicode = 0x10ffff; | 8 const int _maxUnicode = 0x10ffff; |
9 | 9 |
10 patch class String { | 10 patch class String { |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 throw new RangeError.range(index, 0, this.length); | 305 throw new RangeError.range(index, 0, this.length); |
306 } | 306 } |
307 if (pattern is String) { | 307 if (pattern is String) { |
308 return _substringMatches(index, pattern); | 308 return _substringMatches(index, pattern); |
309 } | 309 } |
310 return pattern.matchAsPrefix(this, index) != null; | 310 return pattern.matchAsPrefix(this, index) != null; |
311 } | 311 } |
312 | 312 |
313 int indexOf(Pattern pattern, [int start = 0]) { | 313 int indexOf(Pattern pattern, [int start = 0]) { |
314 if ((start < 0) || (start > this.length)) { | 314 if ((start < 0) || (start > this.length)) { |
315 throw new RangeError.range(start, 0, this.length); | 315 throw new RangeError.range(start, 0, this.length, "start"); |
316 } | 316 } |
317 if (pattern is String) { | 317 if (pattern is String) { |
318 String other = pattern; | 318 String other = pattern; |
319 int maxIndex = this.length - other.length; | 319 int maxIndex = this.length - other.length; |
320 // TODO: Use an efficient string search (e.g. BMH). | 320 // TODO: Use an efficient string search (e.g. BMH). |
321 for (int index = start; index <= maxIndex; index++) { | 321 for (int index = start; index <= maxIndex; index++) { |
322 if (_substringMatches(index, other)) { | 322 if (_substringMatches(index, other)) { |
323 return index; | 323 return index; |
324 } | 324 } |
325 } | 325 } |
(...skipping 511 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
837 } | 837 } |
838 } | 838 } |
839 return _concatRangeNative(values, 0, numValues); | 839 return _concatRangeNative(values, 0, numValues); |
840 } | 840 } |
841 } | 841 } |
842 // All strings were one-byte strings. | 842 // All strings were one-byte strings. |
843 return _OneByteString._concatAll(values, totalLength); | 843 return _OneByteString._concatAll(values, totalLength); |
844 } | 844 } |
845 | 845 |
846 Iterable<Match> allMatches(String string, [int start = 0]) { | 846 Iterable<Match> allMatches(String string, [int start = 0]) { |
847 List<Match> result = new List<Match>(); | 847 if (start < 0 || start > string.length) { |
848 int length = string.length; | 848 throw new RangeError.range(start, 0, string.length, "start"); |
849 int patternLength = this.length; | |
850 int startIndex = start; | |
851 while (true) { | |
852 int position = string.indexOf(this, startIndex); | |
853 if (position == -1) { | |
854 break; | |
855 } | |
856 result.add(new _StringMatch(position, string, this)); | |
857 int endIndex = position + patternLength; | |
858 if (endIndex == length) { | |
859 break; | |
860 } else if (position == endIndex) { | |
861 ++startIndex; // empty match, advance and restart | |
862 } else { | |
863 startIndex = endIndex; | |
864 } | |
865 } | 849 } |
866 return result; | 850 return new _StringAllMatchesIterable(string, this, start); |
867 } | 851 } |
868 | 852 |
869 Match matchAsPrefix(String string, [int start = 0]) { | 853 Match matchAsPrefix(String string, [int start = 0]) { |
870 if (start < 0 || start > string.length) { | 854 if (start < 0 || start > string.length) { |
871 throw new RangeError.range(start, 0, string.length); | 855 throw new RangeError.range(start, 0, string.length); |
872 } | 856 } |
873 if (start + this.length > string.length) return null; | 857 if (start + this.length > string.length) return null; |
874 for (int i = 0; i < this.length; i++) { | 858 for (int i = 0; i < this.length; i++) { |
875 if (string.codeUnitAt(start + i) != this.codeUnitAt(i)) { | 859 if (string.codeUnitAt(start + i) != this.codeUnitAt(i)) { |
876 return null; | 860 return null; |
(...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1314 for (int g in groups) { | 1298 for (int g in groups) { |
1315 result.add(group(g)); | 1299 result.add(group(g)); |
1316 } | 1300 } |
1317 return result; | 1301 return result; |
1318 } | 1302 } |
1319 | 1303 |
1320 final int start; | 1304 final int start; |
1321 final String input; | 1305 final String input; |
1322 final String pattern; | 1306 final String pattern; |
1323 } | 1307 } |
| 1308 |
| 1309 |
| 1310 class _StringAllMatchesIterable extends Iterable<Match> { |
| 1311 final String _input; |
| 1312 final String _pattern; |
| 1313 final int _index; |
| 1314 |
| 1315 _StringAllMatchesIterable(this._input, this._pattern, this._index); |
| 1316 |
| 1317 Iterator<Match> get iterator => |
| 1318 new _StringAllMatchesIterator(_input, _pattern, _index); |
| 1319 |
| 1320 Match get first { |
| 1321 int index = _input.indexOf(_pattern, _index); |
| 1322 if (index >= 0) { |
| 1323 return new _StringMatch(index, _input, _pattern); |
| 1324 } |
| 1325 throw IterableElementError.noElement(); |
| 1326 } |
| 1327 } |
| 1328 |
| 1329 class _StringAllMatchesIterator implements Iterator<Match> { |
| 1330 final String _input; |
| 1331 final String _pattern; |
| 1332 int _index; |
| 1333 Match _current; |
| 1334 |
| 1335 _StringAllMatchesIterator(this._input, this._pattern, this._index); |
| 1336 |
| 1337 bool moveNext() { |
| 1338 if (_index + _pattern.length > _input.length) { |
| 1339 _current = null; |
| 1340 return false; |
| 1341 } |
| 1342 var index = _input.indexOf(_pattern, _index); |
| 1343 if (index < 0) { |
| 1344 _index = _input.length + 1; |
| 1345 _current = null; |
| 1346 return false; |
| 1347 } |
| 1348 int end = index + _pattern.length; |
| 1349 _current = new _StringMatch(index, _input, _pattern); |
| 1350 // Empty match, don't start at same location again. |
| 1351 if (end == _index) end++; |
| 1352 _index = end; |
| 1353 return true; |
| 1354 } |
| 1355 |
| 1356 Match get current => _current; |
| 1357 } |
OLD | NEW |