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

Side by Side 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: Tweaks for review comments. 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 unified diff | Download patch
« no previous file with comments | « pkg/analyzer/lib/source/path_filter.dart ('k') | pkg/analyzer/test/src/util/glob_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 library analyzer.src.util.glob;
6
7 /**
8 * A pattern that matches against filesystem path-like strings with wildcards.
9 *
10 * The pattern matches strings as follows:
11 * * The pattern must use `/` as the path separator.
12 * * The whole string must match, not a substring.
13 * * Any non wildcard is matched as a literal.
14 * * '*' matches one or more characters except '/'.
15 * * '?' matches exactly one character except '/'.
16 * * '**' matches one or more characters including '/'.
17 */
18 class Glob {
19 /**
20 * The special characters are: \ ^ $ . | + [ ] ( ) { }
21 * as defined here: http://ecma-international.org/ecma-262/5.1/#sec-15.10
22 */
23 static final RegExp _specialChars =
24 new RegExp(r'([\\\^\$\.\|\+\[\]\(\)\{\}])');
25
26 /**
27 * The path separator used to separate components in file paths.
28 */
29 final String _separator;
30
31 final String pattern;
32 final RegExp _regex;
33
34 Glob(this._separator, String pattern)
35 : pattern = pattern,
36 _regex = _regexpFromGlobPattern(pattern);
37
38 @override
39 int get hashCode => pattern.hashCode;
40
41 bool operator ==(other) => other is Glob && pattern == other.pattern;
42
43 /**
44 * Return `true` if the given [path] matches this glob.
45 * The given [path] must use the same [_separator] as the glob.
46 */
47 bool matches(String path) {
48 String posixPath = _toPosixPath(path);
49 return _regex.matchAsPrefix(posixPath) != null;
50 }
51
52 @override
53 String toString() => pattern;
54
55 /**
56 * Return the Posix version of the given [path].
57 */
58 String _toPosixPath(String path) {
59 if (_separator == '/') {
60 return path;
61 }
62 return path.replaceAll(_separator, '/');
63 }
64
65 static RegExp _regexpFromGlobPattern(String pattern) {
66 StringBuffer sb = new StringBuffer();
67 sb.write('^');
68 List<String> chars = pattern.split('');
69 for (int i = 0; i < chars.length; i++) {
70 String c = chars[i];
71 if (_specialChars.hasMatch(c)) {
72 sb.write(r'\');
73 sb.write(c);
74 } else if (c == '*') {
75 if (i + 1 < chars.length && chars[i + 1] == '*') {
76 sb.write('.*');
77 i++;
78 } else {
79 sb.write('[^/]*');
80 }
81 } else if (c == '?') {
82 sb.write('[^/]');
83 } else {
84 sb.write(c);
85 }
86 }
87 sb.write(r'$');
88 return new RegExp(sb.toString(), caseSensitive: false);
89 }
90 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/source/path_filter.dart ('k') | pkg/analyzer/test/src/util/glob_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698