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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | dart/tests/co19/co19-leg.status » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: dart/frog/leg/lib/regexp_helper.dart
diff --git a/dart/frog/leg/lib/regexp_helper.dart b/dart/frog/leg/lib/regexp_helper.dart
index df8858d831582d3effcc56c361afff5521acea71..ff9289b750e1d54bfcfdb86a0ee4438866e9b2d7 100644
--- a/dart/frog/leg/lib/regexp_helper.dart
+++ b/dart/frog/leg/lib/regexp_helper.dart
@@ -3,22 +3,16 @@
// BSD-style license that can be found in the LICENSE file.
class RegExpWrapper {
- final re;
-
- // TODO(ahe): This constructor is clearly not const. We need some
- // better way to handle constant regular expressions. One might
- // question if regular expressions are really constant as we have
- // tests that expect an exception from the constructor.
- const RegExpWrapper(pattern, multiLine, ignoreCase, global)
- : re = makeRegExp(pattern, "${multiLine == true ? 'm' : ''}${
- ignoreCase == true ? 'i' : ''}${
- global == true ? 'g' : ''}");
-
- RegExpWrapper.fromRegExp(other, global)
- // TODO(ahe): Use redirection.
- : re = makeRegExp(other.pattern, "${other.multiLine == true ? 'm' : ''}${
- other.ignoreCase == true ? 'i' : ''}${
- global == true ? 'g' : ''}");
+ final String pattern;
+ final bool multiLine;
+ final bool ignoreCase;
+ final bool global;
+
+ const RegExpWrapper(this.pattern,
+ this.multiLine, this.ignoreCase, this.global);
+
+ const RegExpWrapper.fromRegExp(other, global)
+ : this(other.pattern, other.multiLine, other.ignoreCase, global);
exec(str) {
var result = JS('List', @'#.exec(#)', re, checkString(str));
@@ -30,10 +24,22 @@ class RegExpWrapper {
static matchStart(m) => JS('int', @'#.index', m);
- static makeRegExp(pattern, flags) {
+ get re() {
+ var r = JS('var', @'#._re', this);
+ if (r === null) {
+ r = JS('var', @'#._re = #', this, makeRegExp());
+ }
+ return r;
+ }
+
+ makeRegExp() {
checkString(pattern);
+ StringBuffer sb = new StringBuffer();
+ if (multiLine) sb.add('m');
+ if (ignoreCase) sb.add('i');
+ if (global) sb.add('g');
try {
- return JS('Object', @'new RegExp(#, #)', pattern, flags);
+ return JS('Object', @'new RegExp(#, #)', pattern, sb.toString());
} catch (var e) {
throw new IllegalJSRegExpException(pattern,
JS('String', @'String(#)', e));
« 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