Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: dart/frog/leg/lib/regexp_helper.dart

Issue 9702048: RegExp is const, really. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | dart/tests/co19/co19-leg.status » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 }
OLDNEW
« no previous file with comments | « no previous file | dart/tests/co19/co19-leg.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698