| 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 pattern, | 6 /* patch */ factory RegExp(String pattern, |
| 7 {bool multiLine: false, | 7 {bool multiLine: false, |
| 8 bool caseSensitive: true}) { | 8 bool caseSensitive: true}) { |
| 9 return new _JSSyntaxRegExp(pattern, | 9 return new _JSSyntaxRegExp(pattern, |
| 10 multiLine: multiLine, | 10 multiLine: multiLine, |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 str) { |
| 80 if (str is! String) throw new ArgumentError(str); | 80 if (str is! String) throw new ArgumentError(str); |
| 81 List<Match> result = new List<Match>(); | 81 return new _AllMatchesIterable(this, str); |
| 82 int length = str.length; | |
| 83 int index = 0; | |
| 84 while (true) { | |
| 85 List match = _ExecuteMatch(str, index); | |
| 86 if (match == null) { | |
| 87 break; | |
| 88 } | |
| 89 result.add(new _JSRegExpMatch(this, str, match)); | |
| 90 // Find the index at which to start searching for the next match. | |
| 91 index = match[1]; | |
| 92 if (match[0] == index) { | |
| 93 // Zero-length match. | |
| 94 if (index == length) { | |
| 95 break; | |
| 96 } | |
| 97 index++; | |
| 98 } | |
| 99 } | |
| 100 return result; | |
| 101 } | 82 } |
| 102 | 83 |
| 103 bool hasMatch(String str) { | 84 bool hasMatch(String str) { |
| 104 List match = _ExecuteMatch(str, 0); | 85 List match = _ExecuteMatch(str, 0); |
| 105 return (match == null) ? false : true; | 86 return (match == null) ? false : true; |
| 106 } | 87 } |
| 107 | 88 |
| 108 String stringMatch(String str) { | 89 String stringMatch(String str) { |
| 109 List match = _ExecuteMatch(str, 0); | 90 List match = _ExecuteMatch(str, 0); |
| 110 if (match == null) { | 91 if (match == null) { |
| 111 return null; | 92 return null; |
| 112 } | 93 } |
| 113 return str._substringUnchecked(match[0], match[1]); | 94 return str._substringUnchecked(match[0], match[1]); |
| 114 } | 95 } |
| 115 | 96 |
| 116 String get pattern native "JSSyntaxRegExp_getPattern"; | 97 String get pattern native "JSSyntaxRegExp_getPattern"; |
| 117 | 98 |
| 118 bool get isMultiLine native "JSSyntaxRegExp_getIsMultiLine"; | 99 bool get isMultiLine native "JSSyntaxRegExp_getIsMultiLine"; |
| 119 | 100 |
| 120 bool get isCaseSensitive native "JSSyntaxRegExp_getIsCaseSensitive"; | 101 bool get isCaseSensitive native "JSSyntaxRegExp_getIsCaseSensitive"; |
| 121 | 102 |
| 122 int get _groupCount native "JSSyntaxRegExp_getGroupCount"; | 103 int get _groupCount native "JSSyntaxRegExp_getGroupCount"; |
| 123 | 104 |
| 124 List _ExecuteMatch(String str, int start_index) | 105 List _ExecuteMatch(String str, int start_index) |
| 125 native "JSSyntaxRegExp_ExecuteMatch"; | 106 native "JSSyntaxRegExp_ExecuteMatch"; |
| 126 } | 107 } |
| 108 |
| 109 class _AllMatchesIterable extends IterableBase<Match> { |
| 110 final _JSSyntaxRegExp _re; |
| 111 final String _str; |
| 112 |
| 113 const _AllMatchesIterable(this._re, this._str); |
| 114 |
| 115 Iterator<Match> get iterator => new _AllMatchesIterator(_re, _str); |
| 116 } |
| 117 |
| 118 class _AllMatchesIterator implements Iterator<Match> { |
| 119 final String _str; |
| 120 _JSSyntaxRegExp _re; |
| 121 Match _current; |
| 122 |
| 123 _AllMatchesIterator(this._re, this._str); |
| 124 |
| 125 Match get current => _current; |
| 126 |
| 127 bool moveNext() { |
| 128 if (_re == null) return false; // Cleared after a failed match. |
| 129 int nextIndex = 0; |
| 130 if (_current != null) { |
| 131 nextIndex = _current.end; |
| 132 if (nextIndex == _current.start) { |
| 133 // Zero-width match. Advance by one more. |
| 134 nextIndex++; |
| 135 if (nextIndex > _str.length) { |
| 136 _re = null; |
| 137 _current = null; |
| 138 return false; |
| 139 } |
| 140 } |
| 141 } |
| 142 var match = _re._ExecuteMatch(_str, nextIndex); |
| 143 if (match == null) { |
| 144 _current = null; |
| 145 _re = null; |
| 146 return false; |
| 147 } |
| 148 _current = new _JSRegExpMatch(_re, _str, match); |
| 149 return true; |
| 150 } |
| 151 } |
| OLD | NEW |