1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
/* Copyright 2013-present Facebook, Inc.
* Licensed under the Apache License, Version 2.0 */
#include "watchman.h"
using watchman::CaseSensitivity;
class NameExpr : public QueryExpr {
w_string name;
std::unordered_set<w_string> set;
CaseSensitivity caseSensitive;
bool wholename;
explicit NameExpr(
std::unordered_set<w_string>&& set,
CaseSensitivity caseSensitive,
bool wholename)
: set(std::move(set)), caseSensitive(caseSensitive), wholename(wholename) {}
public:
bool evaluate(struct w_query_ctx* ctx, const FileResult* file) override {
if (!set.empty()) {
bool matched;
w_string str;
if (wholename) {
str = w_query_ctx_get_wholename(ctx);
if (caseSensitive == CaseSensitivity::CaseInSensitive) {
str = str.piece().asLowerCase();
}
} else {
str = caseSensitive == CaseSensitivity::CaseInSensitive
? file->baseName().asLowerCase()
: file->baseName().asWString();
}
matched = set.find(str) != set.end();
return matched;
}
w_string_piece str;
if (wholename) {
str = w_query_ctx_get_wholename(ctx);
} else {
str = file->baseName();
}
if (caseSensitive == CaseSensitivity::CaseInSensitive) {
return w_string_equal_caseless(str, name);
}
return str == name;
}
static std::unique_ptr<QueryExpr>
parse(w_query*, const json_ref& term, CaseSensitivity caseSensitive) {
const char *pattern = nullptr, *scope = "basename";
const char *which =
caseSensitive == CaseSensitivity::CaseInSensitive ? "iname" : "name";
std::unordered_set<w_string> set;
if (!json_is_array(term)) {
throw QueryParseError("Expected array for '", which, "' term");
}
if (json_array_size(term) > 3) {
throw QueryParseError(
"Invalid number of arguments for '", which, "' term");
}
if (json_array_size(term) == 3) {
const auto& jscope = term.at(2);
if (!json_is_string(jscope)) {
throw QueryParseError("Argument 3 to '", which, "' must be a string");
}
scope = json_string_value(jscope);
if (strcmp(scope, "basename") && strcmp(scope, "wholename")) {
throw QueryParseError(
"Invalid scope '", scope, "' for ", which, " expression");
}
}
const auto& name = term.at(1);
if (json_is_array(name)) {
uint32_t i;
for (i = 0; i < json_array_size(name); i++) {
if (!json_is_string(json_array_get(name, i))) {
throw QueryParseError(
"Argument 2 to '",
which,
"' must be either a string or an array of string");
}
}
set.reserve(json_array_size(name));
for (i = 0; i < json_array_size(name); i++) {
w_string_t* element;
const char* ele;
const auto& jele = name.at(i);
ele = json_string_value(jele);
// We need to make a copy of the string since we do in-place separator
// normalization on the paths.
if (caseSensitive == CaseSensitivity::CaseInSensitive) {
element =
w_string_new_lower_typed(ele, json_to_w_string(jele).type());
} else {
element = w_string_new_typed(ele, json_to_w_string(jele).type());
}
w_string_in_place_normalize_separators(&element);
set.insert(element);
w_string_delref(element);
}
} else if (json_is_string(name)) {
pattern = json_string_value(name);
} else {
throw QueryParseError(
"Argument 2 to '",
which,
"' must be either a string or an array of string");
}
auto data = new NameExpr(std::move(set), caseSensitive,
!strcmp(scope, "wholename"));
if (pattern) {
// We need to make a copy of the string since we do in-place separator
// normalization on the paths.
w_string pat(pattern, json_to_w_string(name).type());
data->name = w_string_normalize_separators(pat);
}
return std::unique_ptr<QueryExpr>(data);
}
static std::unique_ptr<QueryExpr> parseName(
w_query* query,
const json_ref& term) {
return parse(query, term, query->case_sensitive);
}
static std::unique_ptr<QueryExpr> parseIName(
w_query* query,
const json_ref& term) {
return parse(query, term, CaseSensitivity::CaseInSensitive);
}
};
W_TERM_PARSER("name", NameExpr::parseName)
W_TERM_PARSER("iname", NameExpr::parseIName)
/* vim:ts=2:sw=2:et:
*/
|