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

Unified Diff: sdk/lib/_internal/compiler/implementation/lib/regexp_helper.dart

Issue 15709018: Proof-of-concept matchPrefix on Pattern. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed other comments. Created 7 years, 6 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 | « sdk/lib/_internal/compiler/implementation/lib/js_string.dart ('k') | sdk/lib/core/pattern.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/_internal/compiler/implementation/lib/regexp_helper.dart
diff --git a/sdk/lib/_internal/compiler/implementation/lib/regexp_helper.dart b/sdk/lib/_internal/compiler/implementation/lib/regexp_helper.dart
index 45ab03c85aa57d738c66c9740c750559c7926247..d2feaa81ee5643a83bdcfab971d401199ca6684d 100644
--- a/sdk/lib/_internal/compiler/implementation/lib/regexp_helper.dart
+++ b/sdk/lib/_internal/compiler/implementation/lib/regexp_helper.dart
@@ -33,6 +33,12 @@ class JSSyntaxRegExp implements RegExp {
other.isCaseSensitive,
true);
+ JSSyntaxRegExp._anchoredVersionOf(JSSyntaxRegExp other)
+ : this._internal(other.pattern + "|()",
+ other.isMultiLine,
+ other.isCaseSensitive,
+ true);
+
static makeNative(
String pattern, bool multiLine, bool caseSensitive, bool global) {
checkString(pattern);
@@ -82,6 +88,26 @@ class JSSyntaxRegExp implements RegExp {
return new _AllMatchesIterable(this, str);
}
+ Match matchAsPrefix(String string, [int start = 0]) {
+ if (start < 0 || start > string.length) {
+ throw new RangeError.range(start, 0, string.length);
+ }
+ // An "anchored version" of a regexp is created by adding "|()" to the
+ // source. This means that the regexp always matches at the first position
+ // that it tries, and you can see if the original regexp matched, or it
+ // was the added zero-width match that matched, by looking at the last
+ // capture. If it is a String, the match participated, otherwise it didn't.
+ JSSyntaxRegExp regexp = new JSSyntaxRegExp._anchoredVersionOf(this);
+ if (start > 0) {
+ JS("void", "#.lastIndex = #", regExpGetNative(regexp), start);
+ }
+ _MatchImplementation match = regexp.firstMatch(string);
+ if (match == null) return null;
+ if (match._groups[match._groups.length - 1] != null) return null;
+ match._groups.length -= 1;
+ return match;
+ }
+
String get pattern => _pattern;
bool get isMultiLine => _isMultiLine;
bool get isCaseSensitive => _isCaseSensitive;
« no previous file with comments | « sdk/lib/_internal/compiler/implementation/lib/js_string.dart ('k') | sdk/lib/core/pattern.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698