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

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

Issue 1452473002: Replace Glob implementation. (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
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
new file mode 100644
index 0000000000000000000000000000000000000000..880482fc400f83d90615fa624b7035eb98723b76
--- /dev/null
+++ b/pkg/analyzer/lib/src/util/glob.dart
@@ -0,0 +1,79 @@
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library analyzer.src.util.glob;
+
+/**
+ * A pattern that matches against filesystem path-like strings with wildcards.
+ *
+ * The pattern matches strings as follows:
+ * * The pattern must use `/` as the path separator.
+ * * The whole string must match, not a substring.
+ * * Any non wildcard is matched as a literal.
+ * * '*' matches one or more characters except '/'.
+ * * '?' matches exactly one character except '/'.
+ * * '**' matches one or more characters including '/'.
+ */
+class Glob {
Brian Wilkerson 2015/11/15 16:27:47 Why not use the implementation from quiver?
scheglov 2015/11/15 18:42:11 We do, partially. But we need to solve the followi
+ /**
+ * The special characters are: \ ^ $ . | + [ ] ( ) { }
+ * as defined here: http://ecma-international.org/ecma-262/5.1/#sec-15.10
+ */
+ static final _specialChars = new RegExp(r'([\\\^\$\.\|\+\[\]\(\)\{\}])');
+
+ /**
+ * The path separator used to separate components in file paths.
+ */
+ final String _separator;
+
+ final RegExp _regex;
+
+ Glob(this._separator, String pattern)
+ : _regex = Glob._regexpFromGlobPattern(pattern);
+
+ /**
+ * Return `true` if the given [path] matches this glob.
+ * The given [path] must use the same [_separator] as the glob.
+ */
+ bool matches(String path) {
+ String posixPath = _toPosixPath(path);
+ return _regex.matchAsPrefix(posixPath) != null;
+ }
+
+ /**
+ * Return the Posix version of the given [path].
+ */
+ String _toPosixPath(String path) {
+ if (_separator == '/') {
+ return path;
+ }
+ return path.replaceAll(_separator, '/');
+ }
+
+ static RegExp _regexpFromGlobPattern(String pattern) {
+ StringBuffer sb = new StringBuffer();
+ sb.write('^');
+ List<String> chars = pattern.split('');
+ for (int i = 0; i < chars.length; i++) {
+ String c = chars[i];
+ if (_specialChars.hasMatch(c)) {
+ sb.write(r'\');
+ sb.write(c);
+ } else if (c == '*') {
+ if (i + 1 < chars.length && chars[i + 1] == '*') {
+ sb.write('.*');
+ i++;
+ } else {
+ sb.write('[^/]*');
+ }
+ } else if (c == '?') {
+ sb.write('[^/]');
+ } else {
+ sb.write(c);
+ }
+ }
+ sb.write(r'$');
+ return new RegExp(sb.toString(), caseSensitive: false);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698