diff --git a/src/common/bcp47.cpp b/src/common/bcp47.cpp index 4eb330173..9ea74922d 100644 --- a/src/common/bcp47.cpp +++ b/src/common/bcp47.cpp @@ -786,68 +786,62 @@ language_c::find_best_match(std::vector const &potential_matches) language_c & language_c::canonicalize_preferred_values() { auto &preferred_values = mtx::iana::language_subtag_registry::g_preferred_values; - auto idx = std::find_if(preferred_values.begin(), preferred_values.end(), - [this](auto const &pair) { - return matches(pair.first); - }); - if (idx == preferred_values.end()) - return *this; + for (auto const &[match, preferred] : preferred_values) { + if (!matches(match)) + continue; - // mxdebug(fmt::format("found one! I am {0} first is {1} second {2}\n", dump(), idx->first.dump(), idx->second.dump())); + if (!preferred.m_language.empty()) { + if (!match.m_language.empty()) + m_language.clear(); - auto const &[match, preferred] = *idx; + if (!match.m_extended_language_subtags.empty()) + m_extended_language_subtags.clear(); - if (!preferred.m_language.empty()) { - if (!match.m_language.empty()) - m_language.clear(); + if (!match.m_script.empty()) + m_script.clear(); - if (!match.m_extended_language_subtags.empty()) - m_extended_language_subtags.clear(); + if (!match.m_region.empty()) + m_region.clear(); - if (!match.m_script.empty()) - m_script.clear(); + if (!match.m_variants.empty()) + m_variants.clear(); - if (!match.m_region.empty()) - m_region.clear(); + if (!match.m_extensions.empty()) + m_extensions.clear(); - if (!match.m_variants.empty()) - m_variants.clear(); + if (!match.m_private_use.empty()) + m_private_use.clear(); - if (!match.m_extensions.empty()) - m_extensions.clear(); + if (!match.m_grandfathered.empty()) + m_grandfathered.clear(); + } - if (!match.m_private_use.empty()) - m_private_use.clear(); + if (!preferred.m_language.empty()) + m_language = preferred.m_language; - if (!match.m_grandfathered.empty()) - m_grandfathered.clear(); + if (!preferred.m_extended_language_subtags.empty()) + m_extended_language_subtags = preferred.m_extended_language_subtags; + + if (!preferred.m_script.empty()) + m_script = preferred.m_script; + + if (!preferred.m_region.empty()) + m_region = preferred.m_region; + + if (!preferred.m_variants.empty()) + m_variants = preferred.m_variants; + + if (!preferred.m_extensions.empty()) + m_extensions = preferred.m_extensions; + + if (!preferred.m_private_use.empty()) + m_private_use = preferred.m_private_use; + + if (!preferred.m_grandfathered.empty()) + m_grandfathered = preferred.m_grandfathered; } - if (!preferred.m_language.empty()) - m_language = preferred.m_language; - - if (!preferred.m_extended_language_subtags.empty()) - m_extended_language_subtags = preferred.m_extended_language_subtags; - - if (!preferred.m_script.empty()) - m_script = preferred.m_script; - - if (!preferred.m_region.empty()) - m_region = preferred.m_region; - - if (!preferred.m_variants.empty()) - m_variants = preferred.m_variants; - - if (!preferred.m_extensions.empty()) - m_extensions = preferred.m_extensions; - - if (!preferred.m_private_use.empty()) - m_private_use = preferred.m_private_use; - - if (!preferred.m_grandfathered.empty()) - m_grandfathered = preferred.m_grandfathered; - return *this; } diff --git a/tests/unit/common/bcp47.cpp b/tests/unit/common/bcp47.cpp index 946cffbdc..19f382a43 100644 --- a/tests/unit/common/bcp47.cpp +++ b/tests/unit/common/bcp47.cpp @@ -448,6 +448,8 @@ TEST(BCP47LanguageTags, ToCanonicalForm) { EXPECT_EQ("hak"s, language_c::parse("zh-hakka"s).to_canonical_form().format()); EXPECT_EQ("hak"s, language_c::parse("i-hak"s).to_canonical_form().format()); EXPECT_EQ("yue-jyutping"s, language_c::parse("zh-yue-jyutping"s).to_canonical_form().format()); + EXPECT_EQ("zh-MM"s, language_c::parse("zh-BU"s).to_canonical_form().format()); + EXPECT_EQ("cmn-MM"s, language_c::parse("zh-cmn-BU"s).to_canonical_form().format()); } }