remwharead  0.6.3
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 vector< Database::entry > &entries)
 Defines the entries to search. More...
 
const vector< Database::entrysearch_tags (string expression, const bool is_re) const
 Search in tags of database entries. More...
 
const vector< 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 vector< Database::entry > &  entries)
explicit

Defines the entries to search.

Since
0.7.0
32  :_entries(entries)
33  {}

Member Function Documentation

◆ search_all()

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

◆ search_tags()

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

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