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 /** | 10 /** |
11 * Returns a native version of the RegExp with the global flag set. | 11 * Returns a native version of the RegExp with the global flag set. |
12 * | 12 * |
13 * The RegExp's `lastIndex` property is zero when it is returned. | 13 * The RegExp's `lastIndex` property is zero when it is returned. |
14 * | 14 * |
15 * The returned regexp is shared, and its `lastIndex` property may be | 15 * The returned regexp is shared, and its `lastIndex` property may be |
16 * modified by other uses, so the returned regexp must be used immediately | 16 * 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. | 17 * when it's returned, with no user-provided code run in between. |
18 */ | 18 */ |
19 regExpGetGlobalNative(JSSyntaxRegExp regexp) { | 19 regExpGetGlobalNative(JSSyntaxRegExp regexp) { |
20 var nativeRegexp = regexp._nativeGlobalVersion; | 20 var nativeRegexp = regexp._nativeGlobalVersion; |
21 JS("void", "#.lastIndex = 0", nativeRegexp); | 21 JS("void", "#.lastIndex = 0", nativeRegexp); |
22 return nativeRegexp; | 22 return nativeRegexp; |
23 } | 23 } |
24 | 24 |
| 25 /** |
| 26 * Computes the number of captures in a regexp. |
| 27 * |
| 28 * This currently involves creating a new RegExp object with a different |
| 29 * source and running it against the empty string (the last part is usually |
| 30 * fast). |
| 31 * |
| 32 * The JSSyntaxRegExp could cache the result, and set the cache any time |
| 33 * it finds a match. |
| 34 */ |
| 35 int regExpCaptureCount(JSSyntaxRegExp regexp) { |
| 36 var nativeAnchoredRegExp = regexp._nativeAnchoredVersion; |
| 37 var match = JS('JSExtendableArray', "#.exec('')", nativeAnchoredRegExp); |
| 38 // The native-anchored regexp always have one capture more than the original, |
| 39 // and always matches the empty string. |
| 40 return match.length - 2; |
| 41 } |
| 42 |
25 class JSSyntaxRegExp implements RegExp { | 43 class JSSyntaxRegExp implements RegExp { |
26 final String pattern; | 44 final String pattern; |
27 final _nativeRegExp; | 45 final _nativeRegExp; |
28 var _nativeGlobalRegExp; | 46 var _nativeGlobalRegExp; |
29 var _nativeAnchoredRegExp; | 47 var _nativeAnchoredRegExp; |
30 | 48 |
| 49 String toString() => "RegExp/$pattern/"; |
31 | 50 |
32 JSSyntaxRegExp(String source, | 51 JSSyntaxRegExp(String source, |
33 { bool multiLine: false, | 52 { bool multiLine: false, |
34 bool caseSensitive: true }) | 53 bool caseSensitive: true }) |
35 : this.pattern = source, | 54 : this.pattern = source, |
36 this._nativeRegExp = | 55 this._nativeRegExp = |
37 makeNative(source, multiLine, caseSensitive, false); | 56 makeNative(source, multiLine, caseSensitive, false); |
38 | 57 |
39 get _nativeGlobalVersion { | 58 get _nativeGlobalVersion { |
40 if (_nativeGlobalRegExp != null) return _nativeGlobalRegExp; | 59 if (_nativeGlobalRegExp != null) return _nativeGlobalRegExp; |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 _current = null; | 228 _current = null; |
210 _string = null; // Marks iteration as ended. | 229 _string = null; // Marks iteration as ended. |
211 return false; | 230 return false; |
212 } | 231 } |
213 } | 232 } |
214 | 233 |
215 /** Find the first match of [regExp] in [string] at or after [start]. */ | 234 /** Find the first match of [regExp] in [string] at or after [start]. */ |
216 Match firstMatchAfter(JSSyntaxRegExp regExp, String string, int start) { | 235 Match firstMatchAfter(JSSyntaxRegExp regExp, String string, int start) { |
217 return regExp._execGlobal(string, start); | 236 return regExp._execGlobal(string, start); |
218 } | 237 } |
OLD | NEW |