| 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 dart._js_helper; | 5 part of dart._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 List<String> _stringList(List l) => l == null ? l : JS('', 'dart.list(#, #)', l,
String); |
| 11 |
| 10 /** | 12 /** |
| 11 * Returns a native version of the RegExp with the global flag set. | 13 * Returns a native version of the RegExp with the global flag set. |
| 12 * | 14 * |
| 13 * The RegExp's `lastIndex` property is zero when it is returned. | 15 * The RegExp's `lastIndex` property is zero when it is returned. |
| 14 * | 16 * |
| 15 * The returned regexp is shared, and its `lastIndex` property may be | 17 * The returned regexp is shared, and its `lastIndex` property may be |
| 16 * modified by other uses, so the returned regexp must be used immediately | 18 * modified by other uses, so the returned regexp must be used immediately |
| 17 * when it's returned, with no user-provided code run in between. | 19 * when it's returned, with no user-provided code run in between. |
| 18 */ | 20 */ |
| 19 regExpGetGlobalNative(JSSyntaxRegExp regexp) { | 21 regExpGetGlobalNative(JSSyntaxRegExp regexp) { |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 '})()', source, m, i, g); | 100 '})()', source, m, i, g); |
| 99 if (JS('bool', '# instanceof RegExp', regexp)) return regexp; | 101 if (JS('bool', '# instanceof RegExp', regexp)) return regexp; |
| 100 // The returned value is the JavaScript exception. Turn it into a | 102 // The returned value is the JavaScript exception. Turn it into a |
| 101 // Dart exception. | 103 // Dart exception. |
| 102 String errorMessage = JS('String', r'String(#)', regexp); | 104 String errorMessage = JS('String', r'String(#)', regexp); |
| 103 throw new FormatException( | 105 throw new FormatException( |
| 104 "Illegal RegExp pattern: $source, $errorMessage"); | 106 "Illegal RegExp pattern: $source, $errorMessage"); |
| 105 } | 107 } |
| 106 | 108 |
| 107 Match firstMatch(String string) { | 109 Match firstMatch(String string) { |
| 108 List<String> m = JS('JSExtendableArray|Null', | 110 List m = JS('JSExtendableArray|Null', |
| 109 r'#.exec(#)', | 111 r'#.exec(#)', |
| 110 _nativeRegExp, | 112 _nativeRegExp, |
| 111 checkString(string)); | 113 checkString(string)); |
| 112 if (m == null) return null; | 114 if (m == null) return null; |
| 113 return new _MatchImplementation(this, m); | 115 return new _MatchImplementation(this, _stringList(m)); |
| 114 } | 116 } |
| 115 | 117 |
| 116 bool hasMatch(String string) { | 118 bool hasMatch(String string) { |
| 117 return JS('bool', r'#.test(#)', _nativeRegExp, checkString(string)); | 119 return JS('bool', r'#.test(#)', _nativeRegExp, checkString(string)); |
| 118 } | 120 } |
| 119 | 121 |
| 120 String stringMatch(String string) { | 122 String stringMatch(String string) { |
| 121 var match = firstMatch(string); | 123 var match = firstMatch(string); |
| 122 if (match != null) return match.group(0); | 124 if (match != null) return match.group(0); |
| 123 return null; | 125 return null; |
| 124 } | 126 } |
| 125 | 127 |
| 126 Iterable<Match> allMatches(String string, [int start = 0]) { | 128 Iterable<Match> allMatches(String string, [int start = 0]) { |
| 127 checkString(string); | 129 checkString(string); |
| 128 checkInt(start); | 130 checkInt(start); |
| 129 if (start < 0 || start > string.length) { | 131 if (start < 0 || start > string.length) { |
| 130 throw new RangeError.range(start, 0, string.length); | 132 throw new RangeError.range(start, 0, string.length); |
| 131 } | 133 } |
| 132 return new _AllMatchesIterable(this, string, start); | 134 return new _AllMatchesIterable(this, string, start); |
| 133 } | 135 } |
| 134 | 136 |
| 135 Match _execGlobal(String string, int start) { | 137 Match _execGlobal(String string, int start) { |
| 136 Object regexp = _nativeGlobalVersion; | 138 Object regexp = _nativeGlobalVersion; |
| 137 JS("void", "#.lastIndex = #", regexp, start); | 139 JS("void", "#.lastIndex = #", regexp, start); |
| 138 List match = JS("JSExtendableArray|Null", "#.exec(#)", regexp, string); | 140 List match = JS("JSExtendableArray|Null", "#.exec(#)", regexp, string); |
| 139 if (match == null) return null; | 141 if (match == null) return null; |
| 140 return new _MatchImplementation(this, match); | 142 return new _MatchImplementation(this, _stringList(match)); |
| 141 } | 143 } |
| 142 | 144 |
| 143 Match _execAnchored(String string, int start) { | 145 Match _execAnchored(String string, int start) { |
| 144 Object regexp = _nativeAnchoredVersion; | 146 Object regexp = _nativeAnchoredVersion; |
| 145 JS("void", "#.lastIndex = #", regexp, start); | 147 JS("void", "#.lastIndex = #", regexp, start); |
| 146 List match = JS("JSExtendableArray|Null", "#.exec(#)", regexp, string); | 148 List match = JS("JSExtendableArray|Null", "#.exec(#)", regexp, string); |
| 147 if (match == null) return null; | 149 if (match == null) return null; |
| 148 // If the last capture group participated, the original regexp did not | 150 // If the last capture group participated, the original regexp did not |
| 149 // match at the start position. | 151 // match at the start position. |
| 150 if (match[match.length - 1] != null) return null; | 152 if (match[match.length - 1] != null) return null; |
| 151 match.length -= 1; | 153 match.length -= 1; |
| 152 return new _MatchImplementation(this, match); | 154 return new _MatchImplementation(this, _stringList(match)); |
| 153 } | 155 } |
| 154 | 156 |
| 155 Match matchAsPrefix(String string, [int start = 0]) { | 157 Match matchAsPrefix(String string, [int start = 0]) { |
| 156 if (start < 0 || start > string.length) { | 158 if (start < 0 || start > string.length) { |
| 157 throw new RangeError.range(start, 0, string.length); | 159 throw new RangeError.range(start, 0, string.length); |
| 158 } | 160 } |
| 159 return _execAnchored(string, start); | 161 return _execAnchored(string, start); |
| 160 } | 162 } |
| 161 | 163 |
| 162 bool get isMultiLine => _isMultiLine; | 164 bool get isMultiLine => _isMultiLine; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 _current = null; | 230 _current = null; |
| 229 _string = null; // Marks iteration as ended. | 231 _string = null; // Marks iteration as ended. |
| 230 return false; | 232 return false; |
| 231 } | 233 } |
| 232 } | 234 } |
| 233 | 235 |
| 234 /** Find the first match of [regExp] in [string] at or after [start]. */ | 236 /** Find the first match of [regExp] in [string] at or after [start]. */ |
| 235 Match firstMatchAfter(JSSyntaxRegExp regExp, String string, int start) { | 237 Match firstMatchAfter(JSSyntaxRegExp regExp, String string, int start) { |
| 236 return regExp._execGlobal(string, start); | 238 return regExp._execGlobal(string, start); |
| 237 } | 239 } |
| OLD | NEW |