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

Side by Side Diff: packages/analyzer/lib/src/util/glob.dart

Issue 2990843002: Removed fixed dependencies (Closed)
Patch Set: Created 3 years, 4 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 unified diff | Download patch
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 /**
32 * The pattern string.
33 */
34 final String _pattern;
35
36 String _suffix;
37 RegExp _regex;
38
39 Glob(this._separator, this._pattern) {
40 if (_hasJustPrefix(_pattern, '**/*')) {
41 _suffix = _pattern.substring(4).toLowerCase();
42 } else if (_hasJustPrefix(_pattern, '**')) {
43 _suffix = _pattern.substring(2).toLowerCase();
44 } else {
45 _regex = _regexpFromGlobPattern(_pattern);
46 }
47 }
48
49 @override
50 int get hashCode => _pattern.hashCode;
51
52 bool operator ==(other) => other is Glob && _pattern == other._pattern;
53
54 /**
55 * Return `true` if the given [path] matches this glob.
56 * The given [path] must use the same [_separator] as the glob.
57 */
58 bool matches(String path) {
59 String posixPath = _toPosixPath(path);
60 if (_suffix != null) {
61 return posixPath.toLowerCase().endsWith(_suffix);
62 }
63 return _regex.matchAsPrefix(posixPath) != null;
64 }
65
66 @override
67 String toString() => _pattern;
68
69 /**
70 * Return the Posix version of the given [path].
71 */
72 String _toPosixPath(String path) {
73 if (_separator == '/') {
74 return path;
75 }
76 return path.replaceAll(_separator, '/');
77 }
78
79 /**
80 * Return `true` if the [pattern] start with the given [prefix] and does
81 * not have `*` or `?` characters after the [prefix].
82 */
83 static bool _hasJustPrefix(String pattern, String prefix) {
84 if (pattern.startsWith(prefix)) {
85 int prefixLength = prefix.length;
86 return pattern.indexOf('*', prefixLength) == -1 &&
87 pattern.indexOf('?', prefixLength) == -1;
88 }
89 return false;
90 }
91
92 static RegExp _regexpFromGlobPattern(String pattern) {
93 StringBuffer sb = new StringBuffer();
94 sb.write('^');
95 List<String> chars = pattern.split('');
96 for (int i = 0; i < chars.length; i++) {
97 String c = chars[i];
98 if (_specialChars.hasMatch(c)) {
99 sb.write(r'\');
100 sb.write(c);
101 } else if (c == '*') {
102 if (i + 1 < chars.length && chars[i + 1] == '*') {
103 sb.write('.*');
104 i++;
105 } else {
106 sb.write('[^/]*');
107 }
108 } else if (c == '?') {
109 sb.write('[^/]');
110 } else {
111 sb.write(c);
112 }
113 }
114 sb.write(r'$');
115 return new RegExp(sb.toString(), caseSensitive: false);
116 }
117 }
OLDNEW
« no previous file with comments | « packages/analyzer/lib/src/util/fast_uri.dart ('k') | packages/analyzer/lib/src/util/lru_map.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698