| 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 _js_helper; | 5 part of _js_helper; |
| 6 | 6 |
| 7 // Helper method used by internal libraries. | 7 // Helper method used by internal libraries. |
| 8 regExpGetNative(JSSyntaxRegExp regexp) => regexp._nativeRegExp; | 8 regExpGetNative(JSSyntaxRegExp regexp) => regexp._nativeRegExp; |
| 9 | 9 |
| 10 class JSSyntaxRegExp implements RegExp { | 10 class JSSyntaxRegExp implements RegExp { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 {bool multiLine: false, | 26 {bool multiLine: false, |
| 27 bool caseSensitive: true}) | 27 bool caseSensitive: true}) |
| 28 : this._internal(pattern, multiLine, caseSensitive, false); | 28 : this._internal(pattern, multiLine, caseSensitive, false); |
| 29 | 29 |
| 30 JSSyntaxRegExp._globalVersionOf(JSSyntaxRegExp other) | 30 JSSyntaxRegExp._globalVersionOf(JSSyntaxRegExp other) |
| 31 : this._internal(other.pattern, | 31 : this._internal(other.pattern, |
| 32 other.isMultiLine, | 32 other.isMultiLine, |
| 33 other.isCaseSensitive, | 33 other.isCaseSensitive, |
| 34 true); | 34 true); |
| 35 | 35 |
| 36 JSSyntaxRegExp._anchoredVersionOf(JSSyntaxRegExp other) |
| 37 : this._internal(other.pattern + "|()", |
| 38 other.isMultiLine, |
| 39 other.isCaseSensitive, |
| 40 true); |
| 41 |
| 36 static makeNative( | 42 static makeNative( |
| 37 String pattern, bool multiLine, bool caseSensitive, bool global) { | 43 String pattern, bool multiLine, bool caseSensitive, bool global) { |
| 38 checkString(pattern); | 44 checkString(pattern); |
| 39 String m = multiLine ? 'm' : ''; | 45 String m = multiLine ? 'm' : ''; |
| 40 String i = caseSensitive ? '' : 'i'; | 46 String i = caseSensitive ? '' : 'i'; |
| 41 String g = global ? 'g' : ''; | 47 String g = global ? 'g' : ''; |
| 42 // We're using the JavaScript's try catch instead of the Dart one | 48 // We're using the JavaScript's try catch instead of the Dart one |
| 43 // to avoid dragging in Dart runtime support just because of using | 49 // to avoid dragging in Dart runtime support just because of using |
| 44 // RegExp. | 50 // RegExp. |
| 45 var regexp = JS('', | 51 var regexp = JS('', |
| (...skipping 29 matching lines...) Expand all Loading... |
| 75 String stringMatch(String str) { | 81 String stringMatch(String str) { |
| 76 var match = firstMatch(str); | 82 var match = firstMatch(str); |
| 77 return match == null ? null : match.group(0); | 83 return match == null ? null : match.group(0); |
| 78 } | 84 } |
| 79 | 85 |
| 80 Iterable<Match> allMatches(String str) { | 86 Iterable<Match> allMatches(String str) { |
| 81 checkString(str); | 87 checkString(str); |
| 82 return new _AllMatchesIterable(this, str); | 88 return new _AllMatchesIterable(this, str); |
| 83 } | 89 } |
| 84 | 90 |
| 91 Match matchAsPrefix(String string, [int start = 0]) { |
| 92 if (start < 0 || start > string.length) { |
| 93 throw new RangeError.range(start, 0, string.length); |
| 94 } |
| 95 // An "anchored version" of a regexp is created by adding "|()" to the |
| 96 // source. This means that the regexp always matches at the first position |
| 97 // that it tries, and you can see if the original regexp matched, or it |
| 98 // was the added zero-width match that matched, by looking at the last |
| 99 // capture. If it is a String, the match participated, otherwise it didn't. |
| 100 JSSyntaxRegExp regexp = new JSSyntaxRegExp._anchoredVersionOf(this); |
| 101 if (start > 0) { |
| 102 JS("void", "#.lastIndex = #", regExpGetNative(regexp), start); |
| 103 } |
| 104 _MatchImplementation match = regexp.firstMatch(string); |
| 105 if (match == null) return null; |
| 106 if (match._groups[match._groups.length - 1] != null) return null; |
| 107 match._groups.length -= 1; |
| 108 return match; |
| 109 } |
| 110 |
| 85 String get pattern => _pattern; | 111 String get pattern => _pattern; |
| 86 bool get isMultiLine => _isMultiLine; | 112 bool get isMultiLine => _isMultiLine; |
| 87 bool get isCaseSensitive => _isCaseSensitive; | 113 bool get isCaseSensitive => _isCaseSensitive; |
| 88 } | 114 } |
| 89 | 115 |
| 90 class _MatchImplementation implements Match { | 116 class _MatchImplementation implements Match { |
| 91 final String pattern; | 117 final String pattern; |
| 92 final String str; | 118 final String str; |
| 93 final int start; | 119 final int start; |
| 94 final int end; | 120 final int end; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 // firstMatch actually acts as nextMatch because of | 163 // firstMatch actually acts as nextMatch because of |
| 138 // hidden global flag. | 164 // hidden global flag. |
| 139 if (_current != null && _current.start == _current.end) { | 165 if (_current != null && _current.start == _current.end) { |
| 140 // Advance implicit start-position if last match was empty. | 166 // Advance implicit start-position if last match was empty. |
| 141 JS("void", "#.lastIndex++", regExpGetNative(_re)); | 167 JS("void", "#.lastIndex++", regExpGetNative(_re)); |
| 142 } | 168 } |
| 143 _current = _re.firstMatch(_str); | 169 _current = _re.firstMatch(_str); |
| 144 return _current != null; | 170 return _current != null; |
| 145 } | 171 } |
| 146 } | 172 } |
| OLD | NEW |