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

Unified Diff: pkg/analyzer/lib/src/util/glob.dart

Issue 1448243002: Specialize Glob for the often case of suffix matching. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 1 month 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 | pkg/analyzer/test/src/util/glob_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analyzer/lib/src/util/glob.dart
diff --git a/pkg/analyzer/lib/src/util/glob.dart b/pkg/analyzer/lib/src/util/glob.dart
index 72cb56681bf64a37aa4423a8a99fc72c8b5d3243..705255d4f9fad4fe6b03571db7e2ce6e418b00b2 100644
--- a/pkg/analyzer/lib/src/util/glob.dart
+++ b/pkg/analyzer/lib/src/util/glob.dart
@@ -28,17 +28,28 @@ class Glob {
*/
final String _separator;
- final String pattern;
- final RegExp _regex;
+ /**
+ * The pattern string.
+ */
+ final String _pattern;
+
+ String _suffix;
+ RegExp _regex;
- Glob(this._separator, String pattern)
- : pattern = pattern,
- _regex = _regexpFromGlobPattern(pattern);
+ Glob(this._separator, this._pattern) {
+ if (_hasJustPrefix(_pattern, '**/*')) {
+ _suffix = _pattern.substring(4);
Brian Wilkerson 2015/11/17 14:54:07 Probably ought to use toLowerCase() for later matc
+ } else if (_hasJustPrefix(_pattern, '**')) {
+ _suffix = _pattern.substring(2);
+ } else {
+ _regex = _regexpFromGlobPattern(_pattern);
+ }
+ }
@override
- int get hashCode => pattern.hashCode;
+ int get hashCode => _pattern.hashCode;
- bool operator ==(other) => other is Glob && pattern == other.pattern;
+ bool operator ==(other) => other is Glob && _pattern == other._pattern;
/**
* Return `true` if the given [path] matches this glob.
@@ -46,11 +57,14 @@ class Glob {
*/
bool matches(String path) {
String posixPath = _toPosixPath(path);
+ if (_suffix != null) {
+ return posixPath.toLowerCase().endsWith(_suffix);
+ }
return _regex.matchAsPrefix(posixPath) != null;
}
@override
- String toString() => pattern;
+ String toString() => _pattern;
/**
* Return the Posix version of the given [path].
@@ -62,6 +76,19 @@ class Glob {
return path.replaceAll(_separator, '/');
}
+ /**
+ * Return `true` if the [pattern] start with the given [prefix] and does
+ * not have `*` or `?` characters.
+ */
+ static bool _hasJustPrefix(String pattern, String prefix) {
+ if (pattern.startsWith(prefix)) {
+ int prefixLength = prefix.length;
+ return pattern.indexOf('*', prefixLength) == -1 &&
+ pattern.indexOf('?', prefixLength) == -1;
+ }
+ return false;
+ }
+
static RegExp _regexpFromGlobPattern(String pattern) {
StringBuffer sb = new StringBuffer();
sb.write('^');
« no previous file with comments | « no previous file | pkg/analyzer/test/src/util/glob_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698