Chromium Code Reviews| Index: lib/compiler/implementation/lib/coreimpl_patch.dart |
| diff --git a/lib/compiler/implementation/lib/coreimpl_patch.dart b/lib/compiler/implementation/lib/coreimpl_patch.dart |
| index b329a33839cde95845b32145f947b949b0352176..502af6dc9ead5828db32fa5fbc7ccfb2fadbc2c6 100644 |
| --- a/lib/compiler/implementation/lib/coreimpl_patch.dart |
| +++ b/lib/compiler/implementation/lib/coreimpl_patch.dart |
| @@ -70,3 +70,132 @@ patch class StopwatchImplementation { |
| patch static int _frequency() => 1000; |
| patch static int _now() => Primitives.dateNow(); |
| } |
| + |
| + |
| +// Patch for RegExp implementation. |
| +// TODO(ager): Split out into regexp_patch.dart and allow #source in |
|
Lasse Reichstein Nielsen
2012/08/23 09:24:06
Patch files should be named .dartp, not .dart.
Mads Ager (google)
2012/08/23 09:38:23
No. They are Dart files. I removed the .dartp exte
|
| +// patch files? |
| +patch class JSSyntaxRegExp { |
| + final String _pattern; |
| + final bool _multiLine; |
| + final bool _ignoreCase; |
| + |
| + patch const JSSyntaxRegExp(String pattern, |
| + [bool multiLine = false, |
| + bool ignoreCase = false]) |
| + : _pattern = pattern, |
| + _multiLine = multiLine, |
| + _ignoreCase = ignoreCase; |
| + |
| + patch Match firstMatch(String str) { |
| + List<String> m = regExpExec(this, checkString(str)); |
| + if (m === null) return null; |
| + var matchStart = regExpMatchStart(m); |
| + // m.lastIndex only works with flag 'g'. |
| + var matchEnd = matchStart + m[0].length; |
| + return new _MatchImplementation(pattern, str, matchStart, matchEnd, m); |
| + } |
| + |
| + patch bool hasMatch(String str) => regExpTest(this, checkString(str)); |
| + |
| + patch String stringMatch(String str) { |
| + var match = firstMatch(str); |
| + return match === null ? null : match.group(0); |
| + } |
| + |
| + patch Iterable<Match> allMatches(String str) { |
| + checkString(str); |
| + return new _AllMatchesIterable(this, str); |
| + } |
| + |
| + patch String get pattern() => _pattern; |
| + patch bool get multiLine() => _multiLine; |
| + patch bool get ignoreCase() => _ignoreCase; |
| + |
| + static JSSyntaxRegExp _globalVersionOf(JSSyntaxRegExp other) { |
| + JSSyntaxRegExp re = new JSSyntaxRegExp(other.pattern, |
| + other.multiLine, |
| + other.ignoreCase); |
| + regExpAttachGlobalNative(re); |
| + return re; |
| + } |
| + |
| + _getNative() => regExpGetNative(this); |
| +} |
| + |
| +class _MatchImplementation implements Match { |
|
Lasse Reichstein Nielsen
2012/08/23 09:24:06
Name it _RegExpMatch or even _JsRegExpMatch.
There
Mads Ager (google)
2012/08/23 09:38:23
I don't want to rock the boat with that right now.
|
| + const _MatchImplementation( |
| + String this.pattern, |
| + String this.str, |
| + int this._start, |
| + int this._end, |
| + List<String> this._groups); |
| + |
| + final String pattern; |
|
Lasse Reichstein Nielsen
2012/08/23 09:24:06
Fields above constructor.
Mads Ager (google)
2012/08/23 09:38:23
Copied from other file. I can move them in a separ
|
| + final String str; |
| + final int _start; |
| + final int _end; |
| + final List<String> _groups; |
| + |
| + int start() => _start; |
|
Lasse Reichstein Nielsen
2012/08/23 09:24:06
Seems superflous, just call the final field "start
Mads Ager (google)
2012/08/23 09:38:23
Yes, but this is not a getter and I don't want to
|
| + int end() => _end; |
| + String group(int index) => _groups[index]; |
| + String operator [](int index) => group(index); |
| + int groupCount() => _groups.length - 1; |
| + |
| + List<String> groups(List<int> groups) { |
| + List<String> out = []; |
| + for (int i in groups) { |
| + out.add(group(i)); |
| + } |
| + return out; |
| + } |
| +} |
| + |
| +class _AllMatchesIterable implements Iterable<Match> { |
|
Lasse Reichstein Nielsen
2012/08/23 09:24:06
Put RegExp in the name somewhere.
Mads Ager (google)
2012/08/23 09:38:23
Copied.
|
| + final JSSyntaxRegExp _re; |
| + final String _str; |
| + |
| + const _AllMatchesIterable(this._re, this._str); |
| + |
| + Iterator<Match> iterator() => new _AllMatchesIterator(_re, _str); |
| +} |
| + |
| +class _AllMatchesIterator implements Iterator<Match> { |
| + final RegExp _re; |
| + final String _str; |
| + Match _next; |
| + bool _done; |
| + |
| + _AllMatchesIterator(JSSyntaxRegExp re, String this._str) |
| + : _done = false, _re = JSSyntaxRegExp._globalVersionOf(re); |
| + |
| + Match next() { |
| + if (!hasNext()) { |
| + throw const NoMoreElementsException(); |
| + } |
| + |
| + // _next is set by #hasNext |
|
Lasse Reichstein Nielsen
2012/08/23 09:24:06
I prefer [hasNext] to #hasNext. More consistent, e
Mads Ager (google)
2012/08/23 09:38:23
Copied. I'll update.
|
| + var next = _next; |
| + _next = null; |
| + return next; |
| + } |
| + |
| + bool hasNext() { |
| + if (_done) { |
| + return false; |
| + } else if (_next != null) { |
| + return true; |
| + } |
| + |
| + _next = _re.firstMatch(_str); |
|
Lasse Reichstein Nielsen
2012/08/23 09:24:06
Slightly tricky that firstMatch really acts as nex
Mads Ager (google)
2012/08/23 09:38:23
Copied. I'll add a comment in a separate patch.
|
| + if (_next == null) { |
| + _done = true; |
| + return false; |
| + } else { |
| + return true; |
| + } |
| + } |
| +} |
| + |
| + |