Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 { | |
|
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
| |
| 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 _specialChars = new RegExp(r'([\\\^\$\.\|\+\[\]\(\)\{\}])'); | |
| 24 | |
| 25 /** | |
| 26 * The path separator used to separate components in file paths. | |
| 27 */ | |
| 28 final String _separator; | |
| 29 | |
| 30 final RegExp _regex; | |
| 31 | |
| 32 Glob(this._separator, String pattern) | |
| 33 : _regex = Glob._regexpFromGlobPattern(pattern); | |
| 34 | |
| 35 /** | |
| 36 * Return `true` if the given [path] matches this glob. | |
| 37 * The given [path] must use the same [_separator] as the glob. | |
| 38 */ | |
| 39 bool matches(String path) { | |
| 40 String posixPath = _toPosixPath(path); | |
| 41 return _regex.matchAsPrefix(posixPath) != null; | |
| 42 } | |
| 43 | |
| 44 /** | |
| 45 * Return the Posix version of the given [path]. | |
| 46 */ | |
| 47 String _toPosixPath(String path) { | |
| 48 if (_separator == '/') { | |
| 49 return path; | |
| 50 } | |
| 51 return path.replaceAll(_separator, '/'); | |
| 52 } | |
| 53 | |
| 54 static RegExp _regexpFromGlobPattern(String pattern) { | |
| 55 StringBuffer sb = new StringBuffer(); | |
| 56 sb.write('^'); | |
| 57 List<String> chars = pattern.split(''); | |
| 58 for (int i = 0; i < chars.length; i++) { | |
| 59 String c = chars[i]; | |
| 60 if (_specialChars.hasMatch(c)) { | |
| 61 sb.write(r'\'); | |
| 62 sb.write(c); | |
| 63 } else if (c == '*') { | |
| 64 if (i + 1 < chars.length && chars[i + 1] == '*') { | |
| 65 sb.write('.*'); | |
| 66 i++; | |
| 67 } else { | |
| 68 sb.write('[^/]*'); | |
| 69 } | |
| 70 } else if (c == '?') { | |
| 71 sb.write('[^/]'); | |
| 72 } else { | |
| 73 sb.write(c); | |
| 74 } | |
| 75 } | |
| 76 sb.write(r'$'); | |
| 77 return new RegExp(sb.toString(), caseSensitive: false); | |
| 78 } | |
| 79 } | |
| OLD | NEW |