| 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 class RegExpWrapper { | 5 class RegExpWrapper { |
| 6 final re; | 6 final String pattern; |
| 7 final bool multiLine; |
| 8 final bool ignoreCase; |
| 9 final bool global; |
| 7 | 10 |
| 8 // TODO(ahe): This constructor is clearly not const. We need some | 11 const RegExpWrapper(this.pattern, |
| 9 // better way to handle constant regular expressions. One might | 12 this.multiLine, this.ignoreCase, this.global); |
| 10 // question if regular expressions are really constant as we have | |
| 11 // tests that expect an exception from the constructor. | |
| 12 const RegExpWrapper(pattern, multiLine, ignoreCase, global) | |
| 13 : re = makeRegExp(pattern, "${multiLine == true ? 'm' : ''}${ | |
| 14 ignoreCase == true ? 'i' : ''}${ | |
| 15 global == true ? 'g' : ''}"); | |
| 16 | 13 |
| 17 RegExpWrapper.fromRegExp(other, global) | 14 const RegExpWrapper.fromRegExp(other, global) |
| 18 // TODO(ahe): Use redirection. | 15 : this(other.pattern, other.multiLine, other.ignoreCase, global); |
| 19 : re = makeRegExp(other.pattern, "${other.multiLine == true ? 'm' : ''}${ | |
| 20 other.ignoreCase == true ? 'i' : ''}${ | |
| 21 global == true ? 'g' : ''}"); | |
| 22 | 16 |
| 23 exec(str) { | 17 exec(str) { |
| 24 var result = JS('List', @'#.exec(#)', re, checkString(str)); | 18 var result = JS('List', @'#.exec(#)', re, checkString(str)); |
| 25 if (JS('bool', @'# === null', result)) return null; | 19 if (JS('bool', @'# === null', result)) return null; |
| 26 return result; | 20 return result; |
| 27 } | 21 } |
| 28 | 22 |
| 29 test(str) => JS('bool', @'#.test(#)', re, checkString(str)); | 23 test(str) => JS('bool', @'#.test(#)', re, checkString(str)); |
| 30 | 24 |
| 31 static matchStart(m) => JS('int', @'#.index', m); | 25 static matchStart(m) => JS('int', @'#.index', m); |
| 32 | 26 |
| 33 static makeRegExp(pattern, flags) { | 27 get re() { |
| 28 var r = JS('var', @'#._re', this); |
| 29 if (r === null) { |
| 30 r = JS('var', @'#._re = #', this, makeRegExp()); |
| 31 } |
| 32 return r; |
| 33 } |
| 34 |
| 35 makeRegExp() { |
| 34 checkString(pattern); | 36 checkString(pattern); |
| 37 StringBuffer sb = new StringBuffer(); |
| 38 if (multiLine) sb.add('m'); |
| 39 if (ignoreCase) sb.add('i'); |
| 40 if (global) sb.add('g'); |
| 35 try { | 41 try { |
| 36 return JS('Object', @'new RegExp(#, #)', pattern, flags); | 42 return JS('Object', @'new RegExp(#, #)', pattern, sb.toString()); |
| 37 } catch (var e) { | 43 } catch (var e) { |
| 38 throw new IllegalJSRegExpException(pattern, | 44 throw new IllegalJSRegExpException(pattern, |
| 39 JS('String', @'String(#)', e)); | 45 JS('String', @'String(#)', e)); |
| 40 } | 46 } |
| 41 } | 47 } |
| 42 } | 48 } |
| OLD | NEW |