Made searches case insensitive.

This commit is contained in:
tastytea 2019-05-22 13:30:42 +02:00
parent f94d6a2229
commit 162ff88163
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
4 changed files with 27 additions and 9 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.2)
project(remwharead
VERSION 0.3.0
VERSION 0.3.1
LANGUAGES CXX
)

View File

@ -47,10 +47,12 @@ are date and time representations according to ISO 8601
Example: `--span 2019-01-01,2019-02-10T12:30`.
*-s* _expression_, *--search-tags* _expression_::
Search in tags. Format: _tag1 AND tag2 OR tag3_. See _SEARCH EXPRESSIONS_.
Search in tags. Format: _tag1 AND tag2 OR tag3_. See _SEARCH EXPRESSIONS_. Case
insensitive.
*-S* _expression_, *--search-all* _expression_::
Search in tags, title, description and full text.
Search in tags, title, description and full text. See _SEARCH EXPESSIONS_. Case
insensitive.
*-N*, *--no-archive*::
Do not archive URI.

View File

@ -16,12 +16,15 @@
#include <regex>
#include <algorithm>
#include <locale>
#include <codecvt>
#include "search.hpp"
using std::regex;
using std::regex_search;
using std::smatch;
using std::find;
using std::find_if;
const vector<vector<string>> parse_expression(string expression)
{
@ -46,10 +49,10 @@ const vector<vector<string>> parse_expression(string expression)
vector<string> terms;
while (regex_search(sub, match, re_and))
{
terms.push_back(match[1].str());
terms.push_back(to_lowercase(match[1].str()));
sub = match.suffix().str();
}
terms.push_back(sub);
terms.push_back(to_lowercase(sub));
searchlist.push_back(terms);
}
}
@ -57,6 +60,16 @@ const vector<vector<string>> parse_expression(string expression)
return searchlist;
}
const string to_lowercase(const string &str)
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::wstring in = converter.from_bytes(str);
std::wstring out;
std::transform(in.begin(), in.end(), std::back_inserter(out), ::towlower);
return converter.to_bytes(out);
}
const vector<Database::entry>
search_tags(const vector<Database::entry> &entries, string expression)
{
@ -70,7 +83,9 @@ search_tags(const vector<Database::entry> &entries, string expression)
bool matched = true;
for (const string &tag : tags_or)
{
const auto it = find(entry.tags.begin(), entry.tags.end(), tag);
const auto it = find_if(entry.tags.begin(), entry.tags.end(),
[&tag](const string &s)
{ return to_lowercase(s) == tag; });
if (it == entry.tags.end())
{
matched = false;
@ -110,17 +125,17 @@ search_all(const vector<Database::entry> &entries, string expression)
for (const string &term : terms_or)
{
if (entry.title.find(term) == std::string::npos)
if (to_lowercase(entry.title).find(term) == string::npos)
{
matched_title = false;
}
if (entry.description.find(term) == std::string::npos)
if (to_lowercase(entry.description).find(term) == string::npos)
{
matched_description = false;
}
if (entry.fulltext.find(term) == std::string::npos)
if (to_lowercase(entry.fulltext).find(term) == string::npos)
{
matched_fulltext = false;
}

View File

@ -25,6 +25,7 @@ using std::vector;
using std::string;
const vector<vector<string>> parse_expression(string expression);
const string to_lowercase(const string &str);
//! Seach database entries for tags.
const vector<Database::entry>