remwharead  0.7.1
Public Member Functions | List of all members
remwharead::Search Class Reference

Search in database entries. More...

#include <remwharead/search.hpp>

Public Member Functions

 Search (const list< Database::entry > &entries)
 Defines the entries to search. More...
 
const list< Database::entrysearch_tags (string expression, const bool is_re) const
 Search in tags of database entries. More...
 
const list< Database::entrysearch_all (string expression, const bool is_re) const
 Search in full text of database entries. More...
 

Detailed Description

Search in database entries.

Since
0.7.0

Constructor & Destructor Documentation

◆ Search()

remwharead::Search::Search ( const list< Database::entry > &  entries)
explicit

Defines the entries to search.

Since
0.7.0
34  :_entries(entries)
35  {}

Member Function Documentation

◆ search_all()

const list< DB::entry > remwharead::Search::search_all ( string  expression,
const bool  is_re 
) const

Search in full text of database entries.

Searches in tags, title, description and full text.

Parameters
entriesVector of Database::entry to search.
expressionSearch expression.
is_reIs it a regular expression?
Returns
Vector of matching Database::entry.
Since
0.7.0
123  {
124  vector<vector<string>> searchlist = parse_expression(expression);
125  list<DB::entry> result = search_tags(expression, is_re);
126 
127  for (const vector<string> &terms_or : searchlist)
128  {
129  for (const DB::entry &entry : _entries)
130  {
131  // Add entry to result if all terms in an OR-slice match title,
132  // description or full text.
133  bool matched_title = true;
134  bool matched_description = true;
135  bool matched_fulltext = true;
136 
137  const auto it = find(result.begin(), result.end(), entry);
138  if (it != result.end())
139  { // Skip if already in result list.
140  continue;
141  }
142 
143  for (const string &term : terms_or)
144  {
145  const string title = to_lowercase(entry.title);
146  const string description = to_lowercase(entry.description);
147  const string fulltext = to_lowercase(entry.fulltext);
148 
149  // Set matched_* to false if term is not found.
150  if (is_re)
151  {
152  const regex re(term);
153 
154  if(!regex_search(title, re))
155  {
156  matched_title = false;
157  }
158 
159  if(!regex_search(description, re))
160  {
161  matched_description = false;
162  }
163 
164  if(!regex_search(fulltext, re))
165  {
166  matched_fulltext = false;
167  }
168  }
169  else
170  {
171  if (title.find(term) == string::npos)
172  {
173  matched_title = false;
174  }
175 
176  if (description.find(term) == string::npos)
177  {
178  matched_description = false;
179  }
180 
181  if (fulltext.find(term) == string::npos)
182  {
183  matched_fulltext = false;
184  }
185  }
186  }
187  if (matched_title == true
188  || matched_description == true
189  || matched_fulltext == true)
190  {
191  result.push_back(entry);
192  }
193  }
194  }
195 
196  return result;
197  }
struct remwharead::Database::entry entry
Describes a database entry.
const list< Database::entry > search_tags(string expression, const bool is_re) const
Search in tags of database entries.
Definition: search.cpp:77

◆ search_tags()

const list< DB::entry > remwharead::Search::search_tags ( string  expression,
const bool  is_re 
) const

Search in tags of database entries.

Only matches whole tags, Pill does not match Pillow.

Parameters
expressionSearch expression.
is_reIs it a regular expression?
Returns
Vector of matching Database::entry.
Since
0.7.0
79  {
80  vector<vector<string>> searchlist = parse_expression(expression);
81  list<DB::entry> result;
82 
83  for (const vector<string> &tags_or : searchlist)
84  {
85  for (const DB::entry &entry : _entries)
86  { // Add entry to result if all tags in an OR-slice match.
87  bool matched = true;
88 
89  for (const string &tag : tags_or)
90  {
91  const auto it = find_if(
92  entry.tags.begin(), entry.tags.end(),
93  [&, is_re](string s)
94  {
95  s = to_lowercase(s);
96  if (is_re)
97  {
98  const regex re("^" + tag + "$");
99  return regex_search(s, re);
100  }
101  else
102  {
103  return (s == tag);
104  }
105  });
106  if (it == entry.tags.end())
107  {
108  matched = false;
109  }
110  }
111  if (matched == true)
112  {
113  result.push_back(entry);
114  }
115  }
116  }
117 
118  return result;
119  }
struct remwharead::Database::entry entry
Describes a database entry.

The documentation for this class was generated from the following files: