BCP47: apply all preferred-value replacements, not just the first matched

Part of the implementation of #3307.
This commit is contained in:
Moritz Bunkus 2022-03-26 12:01:29 +01:00
parent 92ef8628f2
commit bf06ef5f2b
No known key found for this signature in database
GPG Key ID: 74AF00ADF2E32C85
2 changed files with 45 additions and 49 deletions

View File

@ -786,68 +786,62 @@ language_c::find_best_match(std::vector<language_c> 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;
}

View File

@ -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());
}
}