2019-05-19 09:42:52 +02:00
|
|
|
/* This file is part of remwharead.
|
|
|
|
* Copyright © 2019 tastytea <tastytea@tastytea.de>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <regex>
|
|
|
|
#include <algorithm>
|
|
|
|
#include "search.hpp"
|
|
|
|
|
|
|
|
using std::regex;
|
|
|
|
using std::regex_search;
|
|
|
|
using std::smatch;
|
|
|
|
using std::find;
|
|
|
|
|
2019-05-19 10:55:45 +02:00
|
|
|
const vector<vector<string>> parse_expression(string expression)
|
2019-05-19 09:42:52 +02:00
|
|
|
{
|
|
|
|
vector<vector<string>> searchlist;
|
|
|
|
const regex re_or("(.+?) (OR|\\|\\|) ");
|
|
|
|
const regex re_and("(.+?) (AND|&&) ");
|
|
|
|
smatch match;
|
|
|
|
|
|
|
|
vector<string> subexpressions;
|
|
|
|
{ // Split expression at OR.
|
|
|
|
while (regex_search(expression, match, re_or))
|
|
|
|
{
|
|
|
|
subexpressions.push_back(match[1].str());
|
|
|
|
expression = match.suffix().str();
|
|
|
|
}
|
|
|
|
subexpressions.push_back(expression);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
for (string sub : subexpressions)
|
|
|
|
{ // Split each OR-slice at AND.
|
2019-05-19 10:55:45 +02:00
|
|
|
vector<string> terms;
|
2019-05-19 09:42:52 +02:00
|
|
|
while (regex_search(sub, match, re_and))
|
|
|
|
{
|
2019-05-19 10:55:45 +02:00
|
|
|
terms.push_back(match[1].str());
|
2019-05-19 09:42:52 +02:00
|
|
|
sub = match.suffix().str();
|
|
|
|
}
|
2019-05-19 10:55:45 +02:00
|
|
|
terms.push_back(sub);
|
|
|
|
searchlist.push_back(terms);
|
2019-05-19 09:42:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-19 10:55:45 +02:00
|
|
|
return searchlist;
|
|
|
|
}
|
|
|
|
|
|
|
|
const vector<Database::entry>
|
|
|
|
search_tags(const vector<Database::entry> &entries, string expression)
|
|
|
|
{
|
|
|
|
vector<vector<string>> searchlist = parse_expression(expression);
|
|
|
|
vector<Database::entry> result;
|
|
|
|
|
2019-05-19 09:42:52 +02:00
|
|
|
for (const vector<string> &tags_or : searchlist)
|
|
|
|
{
|
|
|
|
for (const Database::entry &entry : entries)
|
|
|
|
{ // Add entry to result if all tags in an OR-slice match.
|
|
|
|
bool matched = true;
|
|
|
|
for (const string &tag : tags_or)
|
|
|
|
{
|
|
|
|
const auto it = find(entry.tags.begin(), entry.tags.end(), tag);
|
|
|
|
if (it == entry.tags.end())
|
|
|
|
{
|
|
|
|
matched = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (matched == true)
|
|
|
|
{
|
|
|
|
result.push_back(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2019-05-19 10:55:45 +02:00
|
|
|
|
|
|
|
const vector<Database::entry>
|
|
|
|
search_all(const vector<Database::entry> &entries, string expression)
|
|
|
|
{
|
|
|
|
vector<vector<string>> searchlist = parse_expression(expression);
|
|
|
|
vector<Database::entry> result = search_tags(entries, expression);
|
|
|
|
|
|
|
|
for (const vector<string> &terms_or : searchlist)
|
|
|
|
{
|
|
|
|
for (const Database::entry &entry : entries)
|
|
|
|
{
|
|
|
|
// Add entry to result if all terms in an OR-slice match title,
|
|
|
|
// description or full text.
|
|
|
|
bool matched_title = true;
|
|
|
|
bool matched_description = true;
|
|
|
|
bool matched_fulltext = true;
|
|
|
|
|
2019-05-19 12:47:38 +02:00
|
|
|
const auto it = find(result.begin(), result.end(), entry);
|
|
|
|
if (it != result.end())
|
|
|
|
{ // Skip if already in result list.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-05-19 10:55:45 +02:00
|
|
|
for (const string &term : terms_or)
|
|
|
|
{
|
|
|
|
if (entry.title.find(term) == std::string::npos)
|
|
|
|
{
|
|
|
|
matched_title = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry.description.find(term) == std::string::npos)
|
|
|
|
{
|
|
|
|
matched_description = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry.fulltext.find(term) == std::string::npos)
|
|
|
|
{
|
|
|
|
matched_fulltext = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (matched_title == true
|
|
|
|
|| matched_description == true
|
|
|
|
|| matched_fulltext == true)
|
|
|
|
{
|
|
|
|
result.push_back(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|