mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-14 22:42:14 -07:00
refactor string matcher
This commit is contained in:
parent
ff25ecaec0
commit
d34a1910f9
1 changed files with 10 additions and 18 deletions
|
|
@ -189,31 +189,23 @@ static void setupParserRules()
|
||||||
};
|
};
|
||||||
|
|
||||||
search["StringValue"] = [](const peg::SemanticValues &sv) -> StringMatcher {
|
search["StringValue"] = [](const peg::SemanticValues &sv) -> StringMatcher {
|
||||||
// Extract common matching logic with strict matching option
|
// Helper function for word boundary matching
|
||||||
auto createMatcher = [](const QString &target, bool strictMatch = true) -> StringMatcher {
|
auto createWordBoundaryMatcher = [](const QString &target) {
|
||||||
return [target, strictMatch](const QString &s) -> bool {
|
QString pattern = QString("\\b%1\\b").arg(QRegularExpression::escape(target));
|
||||||
if (!strictMatch && s.contains(target, Qt::CaseInsensitive)) {
|
QRegularExpression regex(pattern, QRegularExpression::CaseInsensitiveOption);
|
||||||
return true;
|
return [regex](const QString &s) { return regex.match(s).hasMatch(); };
|
||||||
}
|
|
||||||
|
|
||||||
// For strict mode, try both exact word matching and substring matching
|
|
||||||
if (s.split(" ").contains(target, Qt::CaseInsensitive)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Also check if the target appears as a substring (for multi-word terms)
|
|
||||||
return s.contains(target, Qt::CaseInsensitive);
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (sv.choice() == 0) {
|
if (sv.choice() == 0) {
|
||||||
const auto target = std::any_cast<QString>(sv[0]);
|
const auto target = std::any_cast<QString>(sv[0]);
|
||||||
return createMatcher(target, true); // Use strict matching
|
return createWordBoundaryMatcher(target);
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto target = std::any_cast<QStringList>(sv[0]);
|
const auto target = std::any_cast<QStringList>(sv[0]);
|
||||||
return [=](const QString &s) -> bool {
|
return [=](const QString &s) {
|
||||||
auto containsString = [&](const QString &str) { return createMatcher(str, true)(s); };
|
auto containsString = [&s, &createWordBoundaryMatcher](const QString &str) {
|
||||||
|
return createWordBoundaryMatcher(str)(s);
|
||||||
|
};
|
||||||
return std::any_of(target.begin(), target.end(), containsString);
|
return std::any_of(target.begin(), target.end(), containsString);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue