1#ifndef YAZE_APP_EDITOR_MESSAGE_MESSAGE_LIST_QUERY_H_
2#define YAZE_APP_EDITOR_MESSAGE_MESSAGE_LIST_QUERY_H_
25 return static_cast<char>(std::tolower(c));
29 std::string_view needle) {
33 if (needle.size() > haystack.size()) {
36 for (
size_t i = 0; i + needle.size() <= haystack.size(); ++i) {
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]))) {
53 if (query.size() >= 2 && query[0] ==
'0' &&
54 (query[1] ==
'x' || query[1] ==
'X')) {
55 return query.substr(2);
57 if (!query.empty() && (query[0] ==
'$' || query[0] ==
'#')) {
58 return query.substr(1);
64 std::string_view query) {
66 if (query.empty() || width <= 0 || width >= 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];
77 std::string_view(buf,
static_cast<size_t>(width)), query);
82 const int written = std::snprintf(buf,
sizeof(buf),
"%d", value);
87 std::string_view(buf,
static_cast<size_t>(written)), query);
94 std::string_view contents,
bool expanded,
96 std::string_view query) {
118 const int id_width = display_id > 0xFFFF ? 6 : 4;
126 if (index >= text.size()) {
129 return std::isalnum(
static_cast<unsigned char>(text[index])) != 0;
134 std::string_view needle,
138 if (needle.empty() || start > text.size()) {
141 for (
size_t i = start; i + needle.size() <= text.size(); ++i) {
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));
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) {
172 std::string_view needle,
176 if (needle.empty() || before == 0) {
179 std::optional<size_t> last;
183 if (before != std::string::npos && *found >= before) {
188 if (from > text.size()) {
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") {
203 const size_t colon = token.find(
':');
204 if (colon == std::string_view::npos) {
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";
214 size_t max_chars = 96) {
215 if (max_chars == 0) {
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);
227 if (token ==
"1" || token ==
"2" || token ==
"3" || token ==
"V" ||
229 pending_space = pending_space || !line.empty();
236 const char c = text[i];
237 if (std::isspace(
static_cast<unsigned char>(c))) {
238 pending_space = pending_space || !line.empty();
243 pending_space =
false;
247 if (line.size() > max_chars) {
248 size_t end = max_chars;
249 while (end > 0 && (
static_cast<unsigned char>(line[end]) & 0xC0) == 0x80) {
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)
277 if (text[i] ==
'\n' || text[i] ==
'\r') {
278 if (line_letters > 0)
281 }
else if (std::isalnum(
static_cast<unsigned char>(text[i]))) {
286 if (line_letters > 0)
288 return nonempty_lines >= 2 && total_letters >= 20;
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)