From a3c565b2435919c701f1db8095ca372794734579 Mon Sep 17 00:00:00 2001 From: George Murray Date: Thu, 24 Sep 2020 07:31:10 -0700 Subject: [PATCH] Add to_search_normalized to search for characters with accents --- library/MiscUtils.cpp | 39 +++++++++++++++++++++++++++++++++++++ library/include/MiscUtils.h | 1 + plugins/search.cpp | 4 ++-- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/library/MiscUtils.cpp b/library/MiscUtils.cpp index 84dddbbd6..018ddd6c3 100644 --- a/library/MiscUtils.cpp +++ b/library/MiscUtils.cpp @@ -128,6 +128,45 @@ std::string toLower(const std::string &str) return rv; } +static const char *normalized_table[256] = { + //.0 .1 .2 .3 .4 .5 .6 .7 .8 .9 .A .B .C .D .E .F + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, // 0. + NULL, NULL, NULL, NULL, NULL, "S", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, // 1. + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, // 2. + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, // 3. + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, // 4. + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, // 5. + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, // 6. + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, // 7. + "C", "u", "e", "a", "a", "a", "a", "c", "e", "e", "e", "i", "i", "i", "A", "A", // 8. + "E", "ae", "Ae", "o", "o", "o", "u", "u", "y", "O", "U", "c", "L", "Y", NULL, "f", // 9. + "a", "i", "o", "u", "n", "N", "a", "o", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, // A. + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, // B. + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, // C. + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, // D. + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, // E. + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, // F. +}; + +std::string to_search_normalized(const std::string &str) +{ + std::stringbuf result; + for (char c : str) + { + const char *mapped = normalized_table[(uint8_t)c]; + if (mapped == NULL) + result.sputc(tolower(c)); + else + while (*mapped != '\0') + { + result.sputc(tolower(c)); + ++mapped; + } + } + + return result.str(); +} + bool word_wrap(std::vector *out, const std::string &str, size_t line_length) { out->clear(); diff --git a/library/include/MiscUtils.h b/library/include/MiscUtils.h index 35f8be73b..4249abeef 100644 --- a/library/include/MiscUtils.h +++ b/library/include/MiscUtils.h @@ -366,6 +366,7 @@ DFHACK_EXPORT std::string join_strings(const std::string &separator, const std:: DFHACK_EXPORT std::string toUpper(const std::string &str); DFHACK_EXPORT std::string toLower(const std::string &str); +DFHACK_EXPORT std::string to_search_normalized(const std::string &str); DFHACK_EXPORT bool word_wrap(std::vector *out, const std::string &str, diff --git a/plugins/search.cpp b/plugins/search.cpp index 6e0b49e16..f053967d8 100644 --- a/plugins/search.cpp +++ b/plugins/search.cpp @@ -396,7 +396,7 @@ protected: clear_viewscreen_vectors(); - string search_string_l = toLower(search_string); + string search_string_l = to_search_normalized(search_string); for (size_t i = 0; i < saved_list1.size(); i++ ) { if (force_in_search(i)) @@ -409,7 +409,7 @@ protected: continue; T element = saved_list1[i]; - string desc = toLower(get_element_description(element)); + string desc = to_search_normalized(get_element_description(element)); if (desc.find(search_string_l) != string::npos) { add_to_filtered_list(i);