/** * Chooses tokens from the file cache that match the specified pattern. * * @param pattern file pattern * @param search search string * @param mode search mode (0-2) * @param results search result * @param exclude exclude file from content search * @param pathSearch path flag * @param pc file cache * @param id search id * @throws InterruptedException interruption */ private void filter( final String pattern, final int[] search, final int mode, final TreeSet<String> results, final HashSet<String> exclude, final boolean pathSearch, final ProjectCache pc, final long id) throws InterruptedException { if (results.size() >= MAXHITS) return; for (final String path : pc) { // check if current file matches the pattern final String lc = path.toLowerCase(Locale.ENGLISH).replace('\\', '/'); final int offset = offset(lc, pathSearch); if (mode == 0 ? lc.startsWith(pattern, offset) : mode == 1 ? (lc.indexOf(pattern, offset) != -1) : matches(lc, pattern, offset)) { if (!exclude.contains(path)) { exclude.add(path); if (filterContent(path, search)) { results.add(path); if (results.size() >= MAXHITS) return; } } } if (id != filterId) throw new InterruptedException(); } }
/** * Chooses files that match the specified pattern. * * @param file file filter * @param content content filter * @param root root directory * @return sorted file paths * @throws InterruptedException interruption */ String[] filter(final String file, final String content, final IOFile root) throws InterruptedException { final long id = ++filterId; final TreeSet<String> results = new TreeSet<>(); final int[] search = new TokenParser(Token.lc(Token.token(content))).toArray(); // glob pattern final ProjectCache pc = cache(root); if (file.contains("*") || file.contains("?")) { final Pattern pt = Pattern.compile(IOFile.regex(file)); for (final String path : pc) { final int offset = offset(path, true); if (pt.matcher(path.substring(offset)).matches() && filterContent(path, search)) { results.add(path); if (results.size() >= MAXHITS) break; } if (id != filterId) throw new InterruptedException(); } } else { // starts-with, contains, camel case final String pttrn = file.toLowerCase(Locale.ENGLISH).replace('\\', '/'); final HashSet<String> exclude = new HashSet<>(); final boolean pathSearch = pttrn.indexOf('/') != -1; for (int i = 0; i < (pathSearch ? 2 : 3); i++) { filter(pttrn, search, i, results, exclude, pathSearch, pc, id); } } return results.toArray(new String[results.size()]); }