yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
message_list_query.h
Go to the documentation of this file.
1#ifndef YAZE_APP_EDITOR_MESSAGE_MESSAGE_LIST_QUERY_H_
2#define YAZE_APP_EDITOR_MESSAGE_MESSAGE_LIST_QUERY_H_
3
4#include <algorithm>
5#include <cctype>
6#include <cstddef>
7#include <cstdint>
8#include <cstdio>
9#include <optional>
10#include <string>
11#include <string_view>
12
13namespace yaze {
14namespace editor {
15
16// Which bank the message list is showing. Independent of find/replace
17// case-sensitivity: the list filter is always a case-insensitive browse.
19 kAll = 0,
20 kVanilla = 1,
21 kExpanded = 2,
22};
23
24inline char AsciiLower(unsigned char c) {
25 return static_cast<char>(std::tolower(c));
26}
27
28inline bool ContainsCaseInsensitive(std::string_view haystack,
29 std::string_view needle) {
30 if (needle.empty()) {
31 return true;
32 }
33 if (needle.size() > haystack.size()) {
34 return false;
35 }
36 for (size_t i = 0; i + needle.size() <= haystack.size(); ++i) {
37 bool match = true;
38 for (size_t j = 0; j < needle.size(); ++j) {
39 if (AsciiLower(static_cast<unsigned char>(haystack[i + j])) !=
40 AsciiLower(static_cast<unsigned char>(needle[j]))) {
41 match = false;
42 break;
43 }
44 }
45 if (match) {
46 return true;
47 }
48 }
49 return false;
50}
51
52inline std::string_view StripHexPrefix(std::string_view query) {
53 if (query.size() >= 2 && query[0] == '0' &&
54 (query[1] == 'x' || query[1] == 'X')) {
55 return query.substr(2);
56 }
57 if (!query.empty() && (query[0] == '$' || query[0] == '#')) {
58 return query.substr(1);
59 }
60 return query;
61}
62
63inline bool HexDigitsContain(uint32_t value, int width,
64 std::string_view query) {
65 query = StripHexPrefix(query);
66 if (query.empty() || width <= 0 || width >= 16) {
67 return false;
68 }
69 char buf[16];
70 static const char* digits = "0123456789ABCDEF";
71 uint32_t remaining = value;
72 for (int i = width - 1; i >= 0; --i) {
73 buf[i] = digits[remaining & 0xF];
74 remaining >>= 4;
75 }
77 std::string_view(buf, static_cast<size_t>(width)), query);
78}
79
80inline bool DecimalContains(int value, std::string_view query) {
81 char buf[16];
82 const int written = std::snprintf(buf, sizeof(buf), "%d", value);
83 if (written <= 0) {
84 return false;
85 }
87 std::string_view(buf, static_cast<size_t>(written)), query);
88}
89
90inline std::string MessageListPreview(std::string_view text, size_t max_chars);
91
92// True when a list row should stay visible for the current bank + text filter.
93inline bool MessageListQueryMatches(int display_id, uint32_t address,
94 std::string_view contents, bool expanded,
96 std::string_view query) {
97 if (bank == MessageBankFilter::kVanilla && expanded) {
98 return false;
99 }
100 if (bank == MessageBankFilter::kExpanded && !expanded) {
101 return false;
102 }
103 if (query.empty()) {
104 return true;
105 }
106 if (ContainsCaseInsensitive(contents, query)) {
107 return true;
108 }
109 // Also search the same normalized prose shown in the list, without its
110 // display truncation. Raw matching above keeps control-token searches.
111 if (ContainsCaseInsensitive(MessageListPreview(contents, contents.size()),
112 query)) {
113 return true;
114 }
115 if (DecimalContains(display_id, query)) {
116 return true;
117 }
118 const int id_width = display_id > 0xFFFF ? 6 : 4;
119 if (HexDigitsContain(static_cast<uint32_t>(display_id), id_width, query)) {
120 return true;
121 }
122 return HexDigitsContain(address, 6, query);
123}
124
125inline bool IsWordChar(std::string_view text, size_t index) {
126 if (index >= text.size()) {
127 return false;
128 }
129 return std::isalnum(static_cast<unsigned char>(text[index])) != 0;
130}
131
132// Next match at or after `start`. Empty needle never matches.
133inline std::optional<size_t> FindNextTextMatch(std::string_view text,
134 std::string_view needle,
135 size_t start,
136 bool case_sensitive,
137 bool whole_word) {
138 if (needle.empty() || start > text.size()) {
139 return std::nullopt;
140 }
141 for (size_t i = start; i + needle.size() <= text.size(); ++i) {
142 bool match = true;
143 for (size_t j = 0; j < needle.size(); ++j) {
144 unsigned char left = static_cast<unsigned char>(text[i + j]);
145 unsigned char right = static_cast<unsigned char>(needle[j]);
146 if (!case_sensitive) {
147 left = static_cast<unsigned char>(AsciiLower(left));
148 right = static_cast<unsigned char>(AsciiLower(right));
149 }
150 if (left != right) {
151 match = false;
152 break;
153 }
154 }
155 if (!match) {
156 continue;
157 }
158 if (whole_word) {
159 const bool start_ok = (i == 0) || !IsWordChar(text, i - 1);
160 const bool end_ok = !IsWordChar(text, i + needle.size());
161 if (!start_ok || !end_ok) {
162 continue;
163 }
164 }
165 return i;
166 }
167 return std::nullopt;
168}
169
170// Last match strictly before `before`. `npos` takes the last match in `text`.
171inline std::optional<size_t> FindPreviousTextMatch(std::string_view text,
172 std::string_view needle,
173 size_t before,
174 bool case_sensitive,
175 bool whole_word) {
176 if (needle.empty() || before == 0) {
177 return std::nullopt;
178 }
179 std::optional<size_t> last;
180 size_t from = 0;
181 while (auto found = FindNextTextMatch(text, needle, from, case_sensitive,
182 whole_word)) {
183 if (before != std::string::npos && *found >= before) {
184 break;
185 }
186 last = found;
187 from = *found + 1;
188 if (from > text.size()) {
189 break;
190 }
191 }
192 return last;
193}
194
195// Formatting commands do not contribute visible text to a list preview.
196// Keep placeholders such as [L] and button glyphs: they represent game text.
197inline bool IsMessagePreviewControl(std::string_view token) {
198 if (token == "1" || token == "2" || token == "3" || token == "V" ||
199 token == "K" || token == "CH3" || token == "CH2" || token == "CH2L" ||
200 token == "CH2I" || token == "CHI" || token == "IMG" || token == "BANK") {
201 return true;
202 }
203 const size_t colon = token.find(':');
204 if (colon == std::string_view::npos) {
205 return false;
206 }
207 const auto name = token.substr(0, colon);
208 return name == "S" || name == "W" || name == "P" || name == "SPD" ||
209 name == "C" || name == "WT" || name == "SFX";
210}
211
212// A compact single line of visible text, with formatting commands removed.
213inline std::string MessageListPreview(std::string_view text,
214 size_t max_chars = 96) {
215 if (max_chars == 0) {
216 return {};
217 }
218 std::string line;
219 line.reserve(std::min(text.size(), max_chars));
220 bool pending_space = false;
221 for (size_t i = 0; i < text.size(); ++i) {
222 if (text[i] == '[') {
223 const size_t end = text.find(']', i + 1);
224 if (end != std::string_view::npos) {
225 const auto token = text.substr(i + 1, end - i - 1);
226 if (IsMessagePreviewControl(token)) {
227 if (token == "1" || token == "2" || token == "3" || token == "V" ||
228 token == "K") {
229 pending_space = pending_space || !line.empty();
230 }
231 i = end;
232 continue;
233 }
234 }
235 }
236 const char c = text[i];
237 if (std::isspace(static_cast<unsigned char>(c))) {
238 pending_space = pending_space || !line.empty();
239 continue;
240 }
241 if (pending_space) {
242 line.push_back(' ');
243 pending_space = false;
244 }
245 line.push_back(c);
246 }
247 if (line.size() > max_chars) {
248 size_t end = max_chars;
249 while (end > 0 && (static_cast<unsigned char>(line[end]) & 0xC0) == 0x80) {
250 --end;
251 }
252 line.resize(end);
253 line.append("...");
254 }
255 return line;
256}
257
258// Prefer an actual multi-line dialogue over menu fragments on first open.
259inline bool HasMultilineMessageProse(std::string_view text) {
260 int nonempty_lines = 0;
261 int line_letters = 0;
262 int total_letters = 0;
263 for (size_t i = 0; i < text.size(); ++i) {
264 if (text[i] == '[') {
265 const size_t end = text.find(']', i + 1);
266 if (end != std::string_view::npos) {
267 const auto token = text.substr(i + 1, end - i - 1);
268 if (token == "2" || token == "3" || token == "V") {
269 if (line_letters > 0)
270 ++nonempty_lines;
271 line_letters = 0;
272 }
273 i = end;
274 continue;
275 }
276 }
277 if (text[i] == '\n' || text[i] == '\r') {
278 if (line_letters > 0)
279 ++nonempty_lines;
280 line_letters = 0;
281 } else if (std::isalnum(static_cast<unsigned char>(text[i]))) {
282 ++line_letters;
283 ++total_letters;
284 }
285 }
286 if (line_letters > 0)
287 ++nonempty_lines;
288 return nonempty_lines >= 2 && total_letters >= 20;
289}
290
291} // namespace editor
292} // namespace yaze
293
294#endif // YAZE_APP_EDITOR_MESSAGE_MESSAGE_LIST_QUERY_H_
bool IsWordChar(std::string_view text, size_t index)
std::string_view StripHexPrefix(std::string_view query)
bool ContainsCaseInsensitive(std::string_view haystack, std::string_view needle)
char AsciiLower(unsigned char c)
bool HasMultilineMessageProse(std::string_view text)
bool DecimalContains(int value, std::string_view query)
std::string MessageListPreview(std::string_view text, size_t max_chars)
bool IsMessagePreviewControl(std::string_view token)
bool HexDigitsContain(uint32_t value, int width, std::string_view query)
std::optional< size_t > FindNextTextMatch(std::string_view text, std::string_view needle, size_t start, bool case_sensitive, bool whole_word)
std::optional< size_t > FindPreviousTextMatch(std::string_view text, std::string_view needle, size_t before, bool case_sensitive, bool whole_word)
bool MessageListQueryMatches(int display_id, uint32_t address, std::string_view contents, bool expanded, MessageBankFilter bank, std::string_view query)