| 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 patch class RegExp { | 5 patch class RegExp { |
| 6 /* patch */ factory RegExp(String source, | 6 /* patch */ factory RegExp(String source, |
| 7 {bool multiLine: false, | 7 {bool multiLine: false, |
| 8 bool caseSensitive: true}) { | 8 bool caseSensitive: true}) { |
| 9 return new _JSSyntaxRegExp(source, | 9 return new _JSSyntaxRegExp(source, |
| 10 multiLine: multiLine, | 10 multiLine: multiLine, |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 bool caseSensitive: true}) native "JSSyntaxRegExp_factory"; | 69 bool caseSensitive: true}) native "JSSyntaxRegExp_factory"; |
| 70 | 70 |
| 71 Match firstMatch(String str) { | 71 Match firstMatch(String str) { |
| 72 List match = _ExecuteMatch(str, 0); | 72 List match = _ExecuteMatch(str, 0); |
| 73 if (match == null) { | 73 if (match == null) { |
| 74 return null; | 74 return null; |
| 75 } | 75 } |
| 76 return new _JSRegExpMatch(this, str, match); | 76 return new _JSRegExpMatch(this, str, match); |
| 77 } | 77 } |
| 78 | 78 |
| 79 Iterable<Match> allMatches(String str) { | 79 Iterable<Match> allMatches(String string, [int start = 0]) { |
| 80 if (str is! String) throw new ArgumentError(str); | 80 if (string is! String) throw new ArgumentError(string); |
| 81 return new _AllMatchesIterable(this, str); | 81 if (start is! int) throw new ArgumentError(start); |
| 82 if (0 > start || start > string.length) { |
| 83 throw new RangeError.range(start, 0, string.length); |
| 84 } |
| 85 return new _AllMatchesIterable(this, string, start); |
| 82 } | 86 } |
| 83 | 87 |
| 84 Match matchAsPrefix(String string, [int start = 0]) { | 88 Match matchAsPrefix(String string, [int start = 0]) { |
| 85 if (start < 0 || start > string.length) { | 89 if (start < 0 || start > string.length) { |
| 86 throw new RangeError.range(start, 0, string.length); | 90 throw new RangeError.range(start, 0, string.length); |
| 87 } | 91 } |
| 88 // Inefficient check that searches for a later match too. | 92 // Inefficient check that searches for a later match too. |
| 89 // Change this when possible. | 93 // Change this when possible. |
| 90 List<int> list = _ExecuteMatch(string, start); | 94 List<int> list = _ExecuteMatch(string, start); |
| 91 if (list == null) return null; | 95 if (list == null) return null; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 114 | 118 |
| 115 int get _groupCount native "JSSyntaxRegExp_getGroupCount"; | 119 int get _groupCount native "JSSyntaxRegExp_getGroupCount"; |
| 116 | 120 |
| 117 List _ExecuteMatch(String str, int start_index) | 121 List _ExecuteMatch(String str, int start_index) |
| 118 native "JSSyntaxRegExp_ExecuteMatch"; | 122 native "JSSyntaxRegExp_ExecuteMatch"; |
| 119 } | 123 } |
| 120 | 124 |
| 121 class _AllMatchesIterable extends IterableBase<Match> { | 125 class _AllMatchesIterable extends IterableBase<Match> { |
| 122 final _JSSyntaxRegExp _re; | 126 final _JSSyntaxRegExp _re; |
| 123 final String _str; | 127 final String _str; |
| 128 final int _start; |
| 124 | 129 |
| 125 const _AllMatchesIterable(this._re, this._str); | 130 _AllMatchesIterable(this._re, this._str, this._start); |
| 126 | 131 |
| 127 Iterator<Match> get iterator => new _AllMatchesIterator(_re, _str); | 132 Iterator<Match> get iterator => new _AllMatchesIterator(_re, _str, _start); |
| 128 } | 133 } |
| 129 | 134 |
| 130 class _AllMatchesIterator implements Iterator<Match> { | 135 class _AllMatchesIterator implements Iterator<Match> { |
| 131 final String _str; | 136 final String _str; |
| 137 int _nextIndex; |
| 132 _JSSyntaxRegExp _re; | 138 _JSSyntaxRegExp _re; |
| 133 Match _current; | 139 Match _current; |
| 134 | 140 |
| 135 _AllMatchesIterator(this._re, this._str); | 141 _AllMatchesIterator(this._re, this._str, this._nextIndex); |
| 136 | 142 |
| 137 Match get current => _current; | 143 Match get current => _current; |
| 138 | 144 |
| 139 bool moveNext() { | 145 bool moveNext() { |
| 140 if (_re == null) return false; // Cleared after a failed match. | 146 if (_re == null) return false; // Cleared after a failed match. |
| 141 int nextIndex = 0; | 147 if (_nextIndex <= _str.length) { |
| 142 if (_current != null) { | 148 var match = _re._ExecuteMatch(_str, _nextIndex); |
| 143 nextIndex = _current.end; | 149 if (match != null) { |
| 144 if (nextIndex == _current.start) { | 150 _current = new _JSRegExpMatch(_re, _str, match); |
| 145 // Zero-width match. Advance by one more. | 151 _nextIndex = _current.end; |
| 146 nextIndex++; | 152 if (_nextIndex == _current.start) { |
| 147 if (nextIndex > _str.length) { | 153 // Zero-width match. Advance by one more. |
| 148 _re = null; | 154 _nextIndex++; |
| 149 _current = null; | |
| 150 return false; | |
| 151 } | 155 } |
| 156 return true; |
| 152 } | 157 } |
| 153 } | 158 } |
| 154 var match = _re._ExecuteMatch(_str, nextIndex); | 159 _current = null; |
| 155 if (match == null) { | 160 _re = null; |
| 156 _current = null; | 161 return false; |
| 157 _re = null; | |
| 158 return false; | |
| 159 } | |
| 160 _current = new _JSRegExpMatch(_re, _str, match); | |
| 161 return true; | |
| 162 } | 162 } |
| 163 } | 163 } |
| OLD | NEW |