Replaced std::regex with Poco::RegularExpression in searches.

This commit is contained in:
tastytea 2019-09-20 17:49:03 +02:00
parent bb8be8e47e
commit 247f49296e
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
1 changed files with 26 additions and 21 deletions

View File

@ -14,7 +14,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <regex>
#include <algorithm> #include <algorithm>
#include <locale> #include <locale>
#include <list> #include <list>
@ -22,18 +21,17 @@
#include <utility> #include <utility>
#include <iterator> #include <iterator>
#include <Poco/UTF8String.h> #include <Poco/UTF8String.h>
#include <Poco/RegularExpression.h>
#include "search.hpp" #include "search.hpp"
namespace remwharead namespace remwharead
{ {
using std::list; using std::list;
using std::regex;
using std::regex_search;
using std::smatch;
using std::find; using std::find;
using std::find_if; using std::find_if;
using std::thread; using std::thread;
using std::move; using std::move;
using RegEx = Poco::RegularExpression;
Search::Search(const list<Database::entry> &entries) Search::Search(const list<Database::entry> &entries)
:_entries(entries) :_entries(entries)
@ -43,30 +41,37 @@ namespace remwharead
const const
{ {
vector<vector<string>> searchlist; vector<vector<string>> searchlist;
const regex re_or("(.+?) (OR|\\|\\|) "); const RegEx re_or("(.+?) (OR|\\|\\|) ");
const regex re_and("(.+?) (AND|&&) "); const RegEx re_and("(.+?) (AND|&&) ");
smatch match; RegEx::MatchVec matches;
string::size_type pos = 0;
vector<string> subexpressions; vector<string> subexpressions;
{ // Split expression at OR. { // Split expression at OR.
while (regex_search(expression, match, re_or)) while (re_or.match(expression, pos, matches) != 0)
{ {
subexpressions.push_back(match[1].str()); const string &subexpr = expression.substr(matches[1].offset,
expression = match.suffix().str(); matches[1].length);
subexpressions.push_back(subexpr);
pos = matches[0].offset + matches[0].length;
} }
subexpressions.push_back(expression); subexpressions.push_back(expression.substr(pos));
} }
{ {
for (string sub : subexpressions) for (string sub : subexpressions)
{ // Split each OR-slice at AND. { // Split each OR-slice at AND.
vector<string> terms; vector<string> terms;
while (regex_search(sub, match, re_and)) pos = 0;
while (re_and.match(sub, pos, matches) != 0)
{ {
terms.push_back(to_lowercase(match[1].str())); const string &term = sub.substr(matches[1].offset,
sub = match.suffix().str(); matches[1].length);
terms.push_back(to_lowercase(term));
pos = matches[0].offset + matches[0].length;
} }
terms.push_back(to_lowercase(sub)); terms.push_back(to_lowercase(sub.substr(pos)));
searchlist.push_back(terms); searchlist.push_back(terms);
} }
} }
@ -100,8 +105,8 @@ namespace remwharead
s = to_lowercase(s); s = to_lowercase(s);
if (is_re) if (is_re)
{ {
const regex re("^" + tag + "$"); const RegEx re("^" + tag + "$");
return regex_search(s, re); return (re == s);
} }
else else
{ {
@ -154,19 +159,19 @@ namespace remwharead
// Set matched_* to false if term is not found. // Set matched_* to false if term is not found.
if (is_re) if (is_re)
{ {
const regex re(term); const RegEx re(term);
if(!regex_search(title, re)) if (!(re == title))
{ {
matched_title = false; matched_title = false;
} }
if(!regex_search(description, re)) if (!(re == description))
{ {
matched_description = false; matched_description = false;
} }
if(!regex_search(fulltext, re)) if (!(re == fulltext))
{ {
matched_fulltext = false; matched_fulltext = false;
} }