Fix ICU iterators on leading/trailing whitespace
[openttd/fttd.git] / src / stringfilter_type.h
blobf78b133cb4f5e7bd9d63adef0bd07abda71818f0
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD 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 2.
6 * OpenTTD 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.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file stringfilter_type.h Searching and filtering using a stringterm. */
12 #ifndef STRINGFILTER_TYPE_H
13 #define STRINGFILTER_TYPE_H
15 #include "core/smallvec_type.hpp"
16 #include "strings_type.h"
18 /**
19 * String filter and state.
21 * The filter takes a stringterm and parses it into words separated by whitespace.
22 * The whitespace-separation can be avoided by quoting words in the searchterm using " or '.
23 * The quotation characters can be nested or concatenated in a unix-shell style.
25 * When filtering an item, all words are checked for matches, and the filter matches if every word
26 * matched. So, effectively this is a AND search for all entered words.
28 * Once the filter is set up using SetFilterTerm, multiple items can be filtered consecutively.
29 * 1. For every item first call ResetState() which resets the matching-state.
30 * 2. Pass all lines of the item via AddLine() to the filter.
31 * 3. Check the matching-result for the item via GetState().
33 struct StringFilter {
34 private:
35 /** State of a single filter word */
36 struct WordState {
37 const char *start; ///< Word to filter for.
38 bool match; ///< Already matched?
41 const char *filter_buffer; ///< Parsed filter string. Words separated by 0.
42 SmallVector<WordState, 4> word_index; ///< Word index and filter state.
43 uint word_matches; ///< Summary of filter state: Number of words matched.
45 const bool *case_sensitive; ///< Match case-sensitively (usually a static variable).
47 public:
48 /**
49 * Constructor for filter.
50 * @param case_sensitive Pointer to a (usually static) variable controlling the case-sensitivity. NULL means always case-insensitive.
52 StringFilter(const bool *case_sensitive = NULL) : filter_buffer(NULL), word_matches(0), case_sensitive(case_sensitive) {}
53 ~StringFilter() { free(this->filter_buffer); }
55 void SetFilterTerm(const char *str);
57 /**
58 * Check whether any filter words were entered.
59 * @return true if no words were entered.
61 bool IsEmpty() const { return this->word_index.Length() == 0; }
63 void ResetState();
64 void AddLine(const char *str);
65 void AddLine(StringID str);
67 /**
68 * Get the matching state of the current item.
69 * @return true if matched.
71 bool GetState() const { return this->word_matches == this->word_index.Length(); }
74 #endif /* STRINGFILTER_TYPE_H */