Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clp-s: Add support for using "?" in wildcard queries on single-word values (resolves #392). #409

Merged
merged 7 commits into from
May 24, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
escape character for exact match
  • Loading branch information
wraymo committed May 16, 2024
commit 799f93de6bbe63d31d8ab5e15f906f36edf92de1
25 changes: 21 additions & 4 deletions components/core/src/clp_s/search/Output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,24 @@ void Output::populate_string_queries(std::shared_ptr<Expression> const& expr) {
if (query_string.find('*') == std::string::npos
&& query_string.find('?') == std::string::npos)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to my other comment we should be handling escaping here as well. Specifically if there are no unescaped * or ? we should enter this code block, process the string to unescape escaped characters as necessary, then do the precise search.

E.g. \\var\? should enter this code block and get unescaped to become \var? before performing exact match.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the function wildcard_match_unsafe_case_sensitive can handle escape characters correctly. The corner case was mentioned above and will be fixed later.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but this code block uses m_var_dict->get_entry_matching_value() which doesn't respect escape characters.

Copy link
Contributor

@gibber9809 gibber9809 May 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe for the condition here we can add a method like

bool has_unescaped_wildchards(std::string const& query) {
    bool escaped{false};
    for (size_t i = 0; i < query.size(); ++i) {
        if ('*' == query[i] || '?' == query[i]) {
            return true;
        }
        if ('\' == query[i]) {
            ++i;
        }
    }
    return false;
}

and replace the if condition with false == has_unescaped_wildcards(query)?

{
auto entry = m_var_dict->get_entry_matching_value(query_string, m_ignore_case);
// Unescape '\' before matching
std::string unescaped_query_string;
bool escape = false;
for (char const c : query_string) {
if (escape) {
unescaped_query_string.push_back(c);
escape = false;
} else if (c == '\\') {
escape = true;
} else {
unescaped_query_string.push_back(c);
}
}

const auto *entry = m_var_dict->get_entry_matching_value(
unescaped_query_string,
m_ignore_case
);

if (entry != nullptr) {
matching_vars.insert(entry->get_id());
Expand All @@ -931,14 +948,14 @@ void Output::populate_string_queries(std::shared_ptr<Expression> const& expr) {
sub_query
))
{
for (auto& var : sub_query.get_vars()) {
for (const auto& var : sub_query.get_vars()) {
if (var.is_precise_var()) {
auto entry = var.get_var_dict_entry();
const auto *entry = var.get_var_dict_entry();
if (entry != nullptr) {
matching_vars.insert(entry->get_id());
}
} else {
for (auto entry : var.get_possible_var_dict_entries()) {
for (const auto *entry : var.get_possible_var_dict_entries()) {
matching_vars.insert(entry->get_id());
}
}
Expand Down
Loading