Commit 25a18b81 authored by twanvl's avatar twanvl

* find_i function for case insensitive searching

* card::contains uses case insensitive find
* Added card::contains_words for quick search, which searches for each word separately
parent dc60a3e3
...@@ -57,10 +57,24 @@ String Card::identification() const { ...@@ -57,10 +57,24 @@ String Card::identification() const {
bool Card::contains(String const& query) const { bool Card::contains(String const& query) const {
FOR_EACH_CONST(v, data) { FOR_EACH_CONST(v, data) {
if (v->toString().find(query) != String::npos) return true; if (find_i(v->toString(),query) != String::npos) return true;
} }
return false; return false;
} }
bool Card::contains_words(String const& query) const {
// iterate over the words
for (size_t i = 0 ; i < query.size() ; ) {
size_t end = query.find_first_of(_(" "),i);
if (end == i) {
i++;
} else {
end = min(end,query.size());
if (!contains(query.substr(i,end-i))) return false;
i = end;
}
}
return true;
}
IndexMap<FieldP, ValueP>& Card::extraDataFor(const StyleSheet& stylesheet) { IndexMap<FieldP, ValueP>& Card::extraDataFor(const StyleSheet& stylesheet) {
return extra_data.get(stylesheet.name(), stylesheet.extra_card_fields); return extra_data.get(stylesheet.name(), stylesheet.extra_card_fields);
......
...@@ -63,6 +63,8 @@ class Card : public IntrusivePtrVirtualBase { ...@@ -63,6 +63,8 @@ class Card : public IntrusivePtrVirtualBase {
String identification() const; String identification() const;
/// Does any field contains the given query string? /// Does any field contains the given query string?
bool contains(String const& query) const; bool contains(String const& query) const;
/// Does this card contain each of the words in the query string?
bool contains_words(String const& query) const;
/// Find a value in the data by name and type /// Find a value in the data by name and type
template <typename T> T& value(const String& name) { template <typename T> T& value(const String& name) {
......
...@@ -45,6 +45,6 @@ void CardListFilter::getItems(const vector<CardP>& cards, vector<VoidP>& out) co ...@@ -45,6 +45,6 @@ void CardListFilter::getItems(const vector<CardP>& cards, vector<VoidP>& out) co
} }
bool QueryCardListFilter::keep(const CardP& card) const { bool QueryCardListFilter::keep(const CardP& card) const {
return card->contains(query); return card->contains_words(query);
} }
...@@ -413,6 +413,14 @@ bool cannocial_name_compare(const String& as, const Char* b) { ...@@ -413,6 +413,14 @@ bool cannocial_name_compare(const String& as, const Char* b) {
} }
} }
size_t find_i(const String& heystack, const String& needle) {
if (needle.empty()) return 0;
for (size_t i = 0 ; i + needle.size() <= heystack.size() ; ++i) {
if (is_substr_i(heystack, i, needle)) return true;
}
return String::npos;
}
// ----------------------------------------------------------------------------- : Regular expressions // ----------------------------------------------------------------------------- : Regular expressions
/// Escape a single character for use in regular expressions /// Escape a single character for use in regular expressions
......
...@@ -199,6 +199,9 @@ bool is_substr_i(const String& str, size_t pos, const Char* cmp); ...@@ -199,6 +199,9 @@ bool is_substr_i(const String& str, size_t pos, const Char* cmp);
/// Return whether str contains the string cmp at position pos, case insensitive compare /// Return whether str contains the string cmp at position pos, case insensitive compare
bool is_substr_i(const String& str, size_t pos, const String& cmp); bool is_substr_i(const String& str, size_t pos, const String& cmp);
/// Case insensitive string search, returns String::npos if not found
size_t find_i(const String& heystack, const String& needle);
/// Compare two strings for equality, b may contain '_' where a contains ' ' /// Compare two strings for equality, b may contain '_' where a contains ' '
bool cannocial_name_compare(const String& a, const Char* b); bool cannocial_name_compare(const String& a, const Char* b);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment