OpenTTD Source: src/stringfilter.cpp Source File

OpenTTD Source 15.0-RC2

stringfilter.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "stdafx.h"
11#include "string_func.h"
12#include "strings_func.h"
13#include "core/utf8.hpp"
14#include "core/string_builder.hpp"
15#include "stringfilter_type.h"
16#include "gfx_func.h"
17
18#include "safeguards.h"
19
20static const char32_t STATE_WHITESPACE = ' ';
21static const char32_t STATE_WORD = 'w';
22static const char32_t STATE_QUOTE1 = '\'';
23static const char32_t STATE_QUOTE2 = '"';
24
29 void StringFilter::SetFilterTerm(std::string_view str)
30{
31 this->word_index.clear();
32 this->word_index.shrink_to_fit();
33 this->word_matches = 0;
34
35 char32_t state = STATE_WHITESPACE;
36 std::string word;
37 StringBuilder builder(word);
38 auto add_word = [this, &word]() {
39 if (!word.empty()) this->word_index.emplace_back(std::move(word), false);
40 word.clear();
41 };
42
43 for (char32_t c : Utf8View(str)) {
44 if (state == STATE_WORD && IsWhitespace(c)) {
45 /* Finish word */
46 add_word();
47 state = STATE_WHITESPACE;
48 continue;
49 }
50
51 if (state == STATE_WHITESPACE) {
52 /* Skip whitespace */
53 if (IsWhitespace(c)) continue;
54 state = STATE_WORD;
55 }
56
57 if (c == STATE_QUOTE1 || c == STATE_QUOTE2) {
58 if (state == c) {
59 /* Stop quoting */
60 state = STATE_WORD;
61 continue;
62 } else if (state == STATE_WORD) {
63 /* Start quoting */
64 state = c;
65 continue;
66 }
67 }
68
69 /* Add to word */
70 builder.PutUtf8(c);
71 }
72
73 /* Add the last word of the string. */
74 add_word();
75}
76
80 void StringFilter::ResetState()
81{
82 this->word_matches = 0;
83 for (WordState &ws : this->word_index) {
84 ws.match = false;
85 }
86}
87
96 void StringFilter::AddLine(std::string_view str)
97{
98 bool match_case = this->case_sensitive != nullptr && *this->case_sensitive;
99 for (WordState &ws : this->word_index) {
100 if (!ws.match) {
101 if (this->locale_aware) {
102 if (match_case ? StrNaturalContains(str, ws.word) : StrNaturalContainsIgnoreCase(str, ws.word)) {
103 ws.match = true;
104 this->word_matches++;
105 }
106 } else {
107 if (match_case ? str.find(ws.word) != str.npos : StrContainsIgnoreCase(str, ws.word)) {
108 ws.match = true;
109 this->word_matches++;
110 }
111 }
112 }
113 }
114}
BaseStringBuilder::PutUtf8
void PutUtf8(char32_t c)
Append UTF.8 char.
Definition string_builder.cpp:113
StringBuilder
Compose data into a growing std::string.
Definition string_builder.hpp:65
Utf8View
Constant span of UTF-8 encoded data.
Definition utf8.hpp:30
gfx_func.h
Functions related to the gfx engine.
safeguards.h
A number of safeguards to prevent using unsafe methods.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
StrNaturalContainsIgnoreCase
bool StrNaturalContainsIgnoreCase(std::string_view str, std::string_view value)
Checks if a string is contained in another string with a locale-aware comparison that is case insensi...
Definition string.cpp:523
StrNaturalContains
bool StrNaturalContains(std::string_view str, std::string_view value)
Checks if a string is contained in another string with a locale-aware comparison that is case sensiti...
Definition string.cpp:496
StrContainsIgnoreCase
bool StrContainsIgnoreCase(std::string_view str, std::string_view value)
Checks if a string is contained in another string, while ignoring the case of the characters.
Definition string.cpp:336
string_builder.hpp
Compose strings from textual and binary data.
string_func.h
Functions related to low-level strings.
IsWhitespace
bool IsWhitespace(char32_t c)
Check whether UNICODE character is whitespace or not, i.e.
Definition string_func.h:143
stringfilter_type.h
Searching and filtering using a stringterm.
strings_func.h
Functions related to OTTD's strings.
StringFilter::WordState
State of a single filter word.
Definition stringfilter_type.h:33
StringFilter::word_matches
uint word_matches
Summary of filter state: Number of words matched.
Definition stringfilter_type.h:39
StringFilter::SetFilterTerm
void SetFilterTerm(std::string_view str)
Set the term to filter on.
Definition stringfilter.cpp:29
StringFilter::word_index
std::vector< WordState > word_index
Word index and filter state.
Definition stringfilter_type.h:38
StringFilter::locale_aware
bool locale_aware
Match words using the current locale.
Definition stringfilter_type.h:42
StringFilter::ResetState
void ResetState()
Reset the matching state to process a new item.
Definition stringfilter.cpp:80
StringFilter::case_sensitive
const bool * case_sensitive
Match case-sensitively (usually a static variable).
Definition stringfilter_type.h:41
utf8.hpp
Handling of UTF-8 encoded data.