Add advanced search term parser

* Support quoted strings & per-field searching
* Support regex and exact matching
* Simplify search sequence
* Make search widget larger
* Add regex converter to Tools namespace
This commit is contained in:
Jonathan White 2018-03-25 16:24:30 -04:00
parent 4b57fcb563
commit 4b983251cb
No known key found for this signature in database
GPG key ID: 440FC65F2E0C6E01
14 changed files with 303 additions and 68 deletions

View file

@ -26,6 +26,8 @@
#include <QImageReader>
#include <QLocale>
#include <QStringList>
#include <QRegularExpression>
#include <QElapsedTimer>
#include <cctype>
@ -199,4 +201,31 @@ void wait(int ms)
}
}
// Escape common regex symbols except for *, ?, and |
auto regexEscape = QRegularExpression(R"re(([-[\]{}()+.,\\\/^$#]))re");
QRegularExpression convertToRegex(const QString& string, bool useWildcards, bool exactMatch, bool caseSensitive)
{
QString pattern = string;
// Wildcard support (*, ?, |)
if (useWildcards) {
pattern.replace(regexEscape, "\\\\1");
pattern.replace("*", ".*");
pattern.replace("?", ".");
}
// Exact modifier
if (exactMatch) {
pattern = "^" + pattern + "$";
}
auto regex = QRegularExpression(pattern);
if (!caseSensitive) {
regex.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
}
return regex;
}
} // namespace Tools