OpenTTD Source 20241224-master-gf74b0cf984
StringFilter Struct Reference

String filter and state. More...

#include <stringfilter_type.h>

Inheritance diagram for StringFilter:
PickerFilterData

Data Structures

struct  WordState
 State of a single filter word. More...
 

Public Member Functions

 StringFilter (const bool *case_sensitive=nullptr, bool locale_aware=true)
 Constructor for filter.
 
void SetFilterTerm (const char *str)
 Set the term to filter on.
 
void SetFilterTerm (const std::string &str)
 Set the term to filter on.
 
bool IsEmpty () const
 Check whether any filter words were entered.
 
void ResetState ()
 Reset the matching state to process a new item.
 
void AddLine (const char *str)
 Pass another text line from the current item to the filter.
 
void AddLine (const std::string &str)
 Pass another text line from the current item to the filter.
 
void AddLine (StringID str)
 Pass another text line from the current item to the filter.
 
bool GetState () const
 Get the matching state of the current item.
 

Private Attributes

const char * filter_buffer
 Parsed filter string. Words separated by 0.
 
std::vector< WordStateword_index
 Word index and filter state.
 
uint word_matches
 Summary of filter state: Number of words matched.
 
const bool * case_sensitive
 Match case-sensitively (usually a static variable).
 
bool locale_aware
 Match words using the current locale.
 

Detailed Description

String filter and state.

The filter takes a stringterm and parses it into words separated by whitespace. The whitespace-separation can be avoided by quoting words in the searchterm using " or '. The quotation characters can be nested or concatenated in a unix-shell style.

When filtering an item, all words are checked for matches, and the filter matches if every word matched. So, effectively this is a AND search for all entered words.

Once the filter is set up using SetFilterTerm, multiple items can be filtered consecutively.

  1. For every item first call ResetState() which resets the matching-state.
  2. Pass all lines of the item via AddLine() to the filter.
  3. Check the matching-result for the item via GetState().

Definition at line 30 of file stringfilter_type.h.

Constructor & Destructor Documentation

◆ StringFilter()

StringFilter::StringFilter ( const bool *  case_sensitive = nullptr,
bool  locale_aware = true 
)
inline

Constructor for filter.

Parameters
case_sensitivePointer to a (usually static) variable controlling the case-sensitivity. nullptr means always case-insensitive.

Definition at line 50 of file stringfilter_type.h.

◆ ~StringFilter()

StringFilter::~StringFilter ( )
inline

Definition at line 51 of file stringfilter_type.h.

Member Function Documentation

◆ AddLine() [1/3]

void StringFilter::AddLine ( const char *  str)

Pass another text line from the current item to the filter.

You can call this multiple times for a single item, if the filter shall apply to multiple things. Before processing the next item you have to call ResetState().

Parameters
strAnother line from the item.

Definition at line 114 of file stringfilter.cpp.

References case_sensitive, locale_aware, StrNaturalContains(), StrNaturalContainsIgnoreCase(), word_index, and word_matches.

Referenced by AddLine(), AddLine(), IndustryDirectoryWindow::BuildSortIndustriesList(), ClassTagNameFilter(), BuildVehicleWindow::FilterByText(), ScriptDebugWindow::OnInvalidateData(), SignList::SignNameFilter(), NetworkContentListWindow::TagNameFilter(), NewGRFWindow::TagNameFilter(), TypeTagNameFilter(), SettingEntry::UpdateFilterState(), and SettingsPage::UpdateFilterState().

◆ AddLine() [2/3]

void StringFilter::AddLine ( const std::string &  str)

Pass another text line from the current item to the filter.

You can call this multiple times for a single item, if the filter shall apply to multiple things. Before processing the next item you have to call ResetState().

Parameters
strAnother line from the item.

Definition at line 144 of file stringfilter.cpp.

References AddLine().

◆ AddLine() [3/3]

void StringFilter::AddLine ( StringID  str)

Pass another text line from the current item to the filter.

You can call this multiple times for a single item, if the filter shall apply to multiple things. Before processing the next item you have to call ResetState().

Parameters
strAnother line from the item.

Definition at line 157 of file stringfilter.cpp.

References AddLine(), and GetString().

◆ GetState()

◆ IsEmpty()

◆ ResetState()

◆ SetFilterTerm() [1/2]

◆ SetFilterTerm() [2/2]

void StringFilter::SetFilterTerm ( const std::string &  str)

Set the term to filter on.

Parameters
strFilter term

Definition at line 90 of file stringfilter.cpp.

References SetFilterTerm().

Field Documentation

◆ case_sensitive

const bool* StringFilter::case_sensitive
private

Match case-sensitively (usually a static variable).

Definition at line 42 of file stringfilter_type.h.

Referenced by AddLine().

◆ filter_buffer

const char* StringFilter::filter_buffer
private

Parsed filter string. Words separated by 0.

Definition at line 38 of file stringfilter_type.h.

Referenced by SetFilterTerm().

◆ locale_aware

bool StringFilter::locale_aware
private

Match words using the current locale.

Definition at line 43 of file stringfilter_type.h.

Referenced by AddLine().

◆ word_index

std::vector<WordState> StringFilter::word_index
private

Word index and filter state.

Definition at line 39 of file stringfilter_type.h.

Referenced by AddLine(), ResetState(), and SetFilterTerm().

◆ word_matches

uint StringFilter::word_matches
private

Summary of filter state: Number of words matched.

Definition at line 40 of file stringfilter_type.h.

Referenced by AddLine(), ResetState(), and SetFilterTerm().


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