1919#include " EntrySearcher.h"
2020
2121#include " core/Group.h"
22+ #include " core/Tools.h"
2223
23- EntrySearcher::EntrySearcher (bool caseSensitive) :
24- m_caseSensitive(caseSensitive)
24+ EntrySearcher::EntrySearcher (bool caseSensitive)
25+ : m_caseSensitive(caseSensitive)
26+ , m_termParser(R"re( ([-*+]+)?(?:(\w*):)?(?:(?=")"((?:[^"\\]|\\.)*)"|([^ ]*))( |$))re" )
27+ // Group 1 = modifiers, Group 2 = field, Group 3 = quoted string, Group 4 = unquoted string
2528{
2629}
2730
@@ -58,16 +61,21 @@ void EntrySearcher::setCaseSensitive(bool state)
5861 m_caseSensitive = state;
5962}
6063
61- bool EntrySearcher::searchEntryImpl ( const QString& searchString, Entry* entry )
64+ bool EntrySearcher::isCaseSensitive ( )
6265{
63- auto searchTerms = parseSearchTerms (searchString) ;
64- bool found;
66+ return m_caseSensitive ;
67+ }
6568
69+ bool EntrySearcher::searchEntryImpl (const QString& searchString, Entry* entry)
70+ {
6671 // Pre-load in case they are needed
6772 auto attributes = QStringList (entry->attributes ()->keys ());
6873 auto attachments = QStringList (entry->attachments ()->keys ());
6974
70- for (SearchTerm* term : searchTerms) {
75+ bool found;
76+ auto searchTerms = parseSearchTerms (searchString);
77+
78+ for (const auto & term : searchTerms) {
7179 switch (term->field ) {
7280 case Field::Title:
7381 found = term->regex .match (entry->resolvePlaceholder (entry->title ())).hasMatch ();
@@ -106,18 +114,14 @@ bool EntrySearcher::searchEntryImpl(const QString& searchString, Entry* entry)
106114 return true ;
107115}
108116
109- QList<EntrySearcher::SearchTerm* > EntrySearcher::parseSearchTerms (const QString& searchString)
117+ QList<QSharedPointer< EntrySearcher::SearchTerm> > EntrySearcher::parseSearchTerms (const QString& searchString)
110118{
111- auto terms = QList<SearchTerm*>();
112- // Group 1 = modifiers, Group 2 = field, Group 3 = quoted string, Group 4 = unquoted string
113- auto termParser = QRegularExpression (R"re( ([-*+]+)?(?:(\w*):)?(?:(?=")"((?:[^"\\]|\\.)*)"|([^ ]*))( |$))re" );
114- // Escape common regex symbols except for *, ?, and |
115- auto regexEscape = QRegularExpression (R"re( ([-[\]{}()+.,\\\/^$#]))re" );
119+ auto terms = QList<QSharedPointer<SearchTerm> >();
116120
117- auto results = termParser .globalMatch (searchString);
121+ auto results = m_termParser .globalMatch (searchString);
118122 while (results.hasNext ()) {
119123 auto result = results.next ();
120- auto term = new SearchTerm ();
124+ QSharedPointer<SearchTerm> term ( new SearchTerm () );
121125
122126 // Quoted string group
123127 term->word = result.captured (3 );
@@ -129,32 +133,16 @@ QList<EntrySearcher::SearchTerm*> EntrySearcher::parseSearchTerms(const QString&
129133
130134 // If still empty, ignore this match
131135 if (term->word .isEmpty ()) {
132- delete term;
133136 continue ;
134137 }
135138
136- QString regex = term->word ;
137-
138- // Wildcard support (*, ?, |)
139- if (!result.captured (1 ).contains (" *" )) {
140- regex.replace (regexEscape, " \\\\ 1" );
141- regex.replace (" **" , " *" );
142- regex.replace (" *" , " .*" );
143- regex.replace (" ?" , " ." );
144- }
145-
146- term->regex = QRegularExpression (regex);
147- if (!m_caseSensitive) {
148- term->regex .setPatternOptions (QRegularExpression::CaseInsensitiveOption);
149- }
139+ auto mods = result.captured (1 );
150140
151- // Exact modifier
152- if (result.captured (1 ).contains (" +" )) {
153- term->regex .setPattern (" ^" + term->regex .pattern () + " $" );
154- }
141+ // Convert term to regex
142+ term->regex = Tools::convertToRegex (term->word , !mods.contains (" *" ), mods.contains (" +" ), m_caseSensitive);
155143
156144 // Exclude modifier
157- term->exclude = result. captured ( 1 ) .contains (" -" );
145+ term->exclude = mods .contains (" -" );
158146
159147 // Determine the field to search
160148 QString field = result.captured (2 );
@@ -175,7 +163,7 @@ QList<EntrySearcher::SearchTerm*> EntrySearcher::parseSearchTerms(const QString&
175163 } else if (field.startsWith (" attach" , cs)) {
176164 term->field = Field::Attachment;
177165 } else {
178- term->field = Field::All ;
166+ term->field = Field::Undefined ;
179167 }
180168 }
181169
0 commit comments