LibUnicode: Generate an implementation of the Add Likely Subtags method

This commit is contained in:
Timothy Flynn 2021-09-02 18:21:42 -04:00 committed by Linus Groh
parent 28ae63177e
commit e6a2ab1202
Notes: sideshowbarker 2024-07-18 04:47:34 +09:00
3 changed files with 36 additions and 3 deletions

View File

@ -448,6 +448,8 @@ Optional<StringView> resolve_variant_alias(StringView const& variant);
Optional<StringView> resolve_subdivision_alias(StringView const& subdivision);
void resolve_complex_language_aliases(Unicode::LanguageID& language_id);
Optional<Unicode::LanguageID> add_likely_subtags(Unicode::LanguageID const& language_id);
Optional<String> resolve_most_likely_territory(Unicode::LanguageID const& language_id);
}
@ -671,7 +673,7 @@ static constexpr Array<LanguageMapping, @size@> s_@name@ { {
append_complex_mapping("likely_subtags"sv, locale_data.likely_subtags);
generator.append(R"~~~(
static CanonicalLanguageID const* resolve_likely_subtag(Unicode::LanguageID const& language_id)
static LanguageMapping const* resolve_likely_subtag(Unicode::LanguageID const& language_id)
{
// https://unicode.org/reports/tr35/#Likely_Subtags
enum class State {
@ -743,7 +745,7 @@ static CanonicalLanguageID const* resolve_likely_subtag(Unicode::LanguageID cons
if (map.key.region != search_key.region)
continue;
return &map.alias;
return &map;
}
}
@ -893,10 +895,31 @@ void resolve_complex_language_aliases(Unicode::LanguageID& language_id)
}
}
Optional<Unicode::LanguageID> add_likely_subtags(Unicode::LanguageID const& language_id)
{
// https://www.unicode.org/reports/tr35/#Likely_Subtags
auto const* likely_subtag = resolve_likely_subtag(language_id);
if (likely_subtag == nullptr)
return {};
auto maximized = language_id;
auto const& key = likely_subtag->key;
auto const& alias = likely_subtag->alias;
if (maximized.language == "und"sv)
maximized.language = alias.language;
if (!maximized.script.has_value() || (!key.script.is_empty() && !alias.script.is_empty()))
maximized.script = alias.script;
if (!maximized.region.has_value() || (!key.region.is_empty() && !alias.region.is_empty()))
maximized.region = alias.region;
return maximized;
}
Optional<String> resolve_most_likely_territory(Unicode::LanguageID const& language_id)
{
if (auto const* likely_subtag = resolve_likely_subtag(language_id); likely_subtag != nullptr)
return likely_subtag->region;
return likely_subtag->alias.region;
return {};
}

View File

@ -834,6 +834,15 @@ Optional<StringView> resolve_subdivision_alias(StringView subdivision)
#endif
}
Optional<LanguageID> add_likely_subtags([[maybe_unused]] LanguageID const& language_id)
{
#if ENABLE_UNICODE_DATA
return Detail::add_likely_subtags(language_id);
#else
return {};
#endif
}
String resolve_most_likely_territory([[maybe_unused]] LanguageID const& language_id, StringView territory_alias)
{
auto aliases = territory_alias.split_view(' ');

View File

@ -130,6 +130,7 @@ Optional<StringView> resolve_script_tag_alias(StringView script_tag);
Optional<StringView> resolve_variant_alias(StringView variant);
Optional<StringView> resolve_subdivision_alias(StringView subdivision);
Optional<LanguageID> add_likely_subtags(LanguageID const& language_id);
String resolve_most_likely_territory(LanguageID const& language_id, StringView territory_alias);
}