LibUnicode: Dynamically load the generated UnicodeLocale symbols

This commit is contained in:
Timothy Flynn 2021-12-15 19:48:13 -05:00 committed by Brian Gianforcaro
parent 3fd53baa25
commit 09be26b5d2
Notes: sideshowbarker 2024-07-17 22:27:47 +09:00
4 changed files with 119 additions and 147 deletions

View file

@ -727,11 +727,7 @@ static void generate_unicode_locale_header(Core::File& file, UnicodeLocaleData&
generator.append(R"~~~(
#pragma once
#include <AK/Optional.h>
#include <AK/StringView.h>
#include <AK/Types.h>
#include <AK/Vector.h>
#include <LibUnicode/Forward.h>
namespace Unicode {
)~~~");
@ -748,45 +744,6 @@ namespace Unicode {
generate_enum(generator, format_identifier, "ListPatternStyle"sv, {}, locale_data.list_pattern_styles);
generator.append(R"~~~(
namespace Detail {
Optional<Locale> locale_from_string(StringView locale);
Optional<StringView> get_locale_language_mapping(StringView locale, StringView language);
Optional<Language> language_from_string(StringView language);
Optional<StringView> resolve_language_alias(StringView language);
Optional<StringView> get_locale_territory_mapping(StringView locale, StringView territory);
Optional<Territory> territory_from_string(StringView territory);
Optional<StringView> resolve_territory_alias(StringView territory);
Optional<StringView> get_locale_script_tag_mapping(StringView locale, StringView script_tag);
Optional<ScriptTag> script_tag_from_string(StringView script_tag);
Optional<StringView> resolve_script_tag_alias(StringView script_tag);
Optional<StringView> get_locale_long_currency_mapping(StringView locale, StringView currency);
Optional<StringView> get_locale_short_currency_mapping(StringView locale, StringView currency);
Optional<StringView> get_locale_narrow_currency_mapping(StringView locale, StringView currency);
Optional<StringView> get_locale_numeric_currency_mapping(StringView locale, StringView currency);
Optional<Currency> currency_from_string(StringView currency);
Optional<StringView> get_locale_key_mapping(StringView locale, StringView key);
Optional<Key> key_from_string(StringView key);
Optional<ListPatterns> get_locale_list_pattern_mapping(StringView locale, StringView list_pattern_type, StringView list_pattern_style);
Optional<ListPatternType> list_pattern_type_from_string(StringView list_pattern_type);
Optional<ListPatternStyle> list_pattern_style_from_string(StringView list_pattern_style);
Optional<StringView> resolve_variant_alias(StringView variant);
Optional<StringView> resolve_subdivision_alias(StringView 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);
}
}
)~~~");
@ -805,7 +762,10 @@ static void generate_unicode_locale_implementation(Core::File& file, UnicodeLoca
generator.append(R"~~~(
#include <AK/Array.h>
#include <AK/BinarySearch.h>
#include <AK/Optional.h>
#include <AK/Span.h>
#include <AK/StringView.h>
#include <AK/Vector.h>
#include <LibUnicode/Locale.h>
#include <LibUnicode/UnicodeLocale.h>
@ -1072,6 +1032,7 @@ static LanguageMapping const* resolve_likely_subtag(Unicode::LanguageID const& l
generator.set("unique_list", unique_list);
generator.append(R"~~~(
Optional<StringView> get_locale_@enum_snake@_mapping(StringView locale, StringView @enum_snake@) asm("unicode_get_locale_@enum_snake@_mapping");
Optional<StringView> get_locale_@enum_snake@_mapping(StringView locale, StringView @enum_snake@)
{
auto locale_value = locale_from_string(locale);
@ -1107,7 +1068,7 @@ Optional<StringView> get_locale_@enum_snake@_mapping(StringView locale, StringVi
for (auto const& alias : aliases)
hashes.set(alias.alias.hash(), format_identifier(enum_title, alias.alias));
generate_value_from_string(generator, "{}_from_string"sv, enum_title, enum_snake, move(hashes));
generate_value_from_string_for_dynamic_loading(generator, "{}_from_string"sv, enum_title, enum_snake, move(hashes));
};
auto append_alias_search = [&](StringView enum_snake, auto const& aliases) {
@ -1117,31 +1078,31 @@ Optional<StringView> get_locale_@enum_snake@_mapping(StringView locale, StringVi
for (auto const& alias : aliases)
hashes.set(alias.key.hash(), alias.value);
generate_value_from_string(generator, "resolve_{}_alias"sv, s_string_index_type, enum_snake, move(hashes), "StringView"sv, "s_string_list[{}]"sv);
generate_value_from_string_for_dynamic_loading(generator, "resolve_{}_alias"sv, s_string_index_type, enum_snake, move(hashes), "StringView"sv, "s_string_list[{}]"sv);
};
append_from_string("Locale"sv, "locale"sv, locale_data.locales.keys(), locale_data.locale_aliases);
append_mapping_search("language"sv, "language"sv, "s_languages"sv, "s_language_lists"sv);
append_from_string("Language"sv, "language"sv, locale_data.languages);
append_mapping_search("language"sv, "language"sv, "s_languages"sv, "s_language_lists"sv);
append_alias_search("language"sv, locale_data.language_aliases);
append_mapping_search("territory"sv, "territory"sv, "s_territories"sv, "s_territory_lists"sv);
append_from_string("Territory"sv, "territory"sv, locale_data.territories);
append_mapping_search("territory"sv, "territory"sv, "s_territories"sv, "s_territory_lists"sv);
append_alias_search("territory"sv, locale_data.territory_aliases);
append_mapping_search("script_tag"sv, "script_tag"sv, "s_scripts"sv, "s_script_lists"sv);
append_from_string("ScriptTag"sv, "script_tag"sv, locale_data.scripts);
append_mapping_search("script_tag"sv, "script_tag"sv, "s_scripts"sv, "s_script_lists"sv);
append_alias_search("script_tag"sv, locale_data.script_aliases);
append_from_string("Currency"sv, "currency"sv, locale_data.currencies);
append_mapping_search("long_currency"sv, "currency"sv, "s_long_currencies"sv, "s_currency_lists"sv);
append_mapping_search("short_currency"sv, "currency"sv, "s_short_currencies"sv, "s_currency_lists"sv);
append_mapping_search("narrow_currency"sv, "currency"sv, "s_narrow_currencies"sv, "s_currency_lists"sv);
append_mapping_search("numeric_currency"sv, "currency"sv, "s_numeric_currencies"sv, "s_currency_lists"sv);
append_from_string("Currency"sv, "currency"sv, locale_data.currencies);
append_mapping_search("key"sv, "key"sv, "s_keywords"sv, "s_keyword_lists"sv);
append_from_string("Key"sv, "key"sv, locale_data.keywords);
append_mapping_search("key"sv, "key"sv, "s_keywords"sv, "s_keyword_lists"sv);
append_alias_search("variant"sv, locale_data.variant_aliases);
append_alias_search("subdivision"sv, locale_data.subdivision_aliases);
@ -1150,6 +1111,7 @@ Optional<StringView> get_locale_@enum_snake@_mapping(StringView locale, StringVi
append_from_string("ListPatternStyle"sv, "list_pattern_style"sv, locale_data.list_pattern_styles);
generator.append(R"~~~(
Optional<ListPatterns> get_locale_list_pattern_mapping(StringView locale, StringView list_pattern_type, StringView list_pattern_style) asm("unicode_get_locale_list_pattern_mapping");
Optional<ListPatterns> get_locale_list_pattern_mapping(StringView locale, StringView list_pattern_type, StringView list_pattern_style)
{
auto locale_value = locale_from_string(locale);
@ -1185,6 +1147,7 @@ Optional<ListPatterns> get_locale_list_pattern_mapping(StringView locale, String
return {};
}
void resolve_complex_language_aliases(Unicode::LanguageID& language_id) asm("unicode_resolve_complex_language_aliases");
void resolve_complex_language_aliases(Unicode::LanguageID& language_id)
{
for (auto const& map : s_complex_alias) {
@ -1217,6 +1180,7 @@ void resolve_complex_language_aliases(Unicode::LanguageID& language_id)
}
}
Optional<Unicode::LanguageID> add_likely_subtags(Unicode::LanguageID const& language_id) asm("unicode_add_likely_subtags");
Optional<Unicode::LanguageID> add_likely_subtags(Unicode::LanguageID const& language_id)
{
// https://www.unicode.org/reports/tr35/#Likely_Subtags
@ -1243,6 +1207,7 @@ Optional<Unicode::LanguageID> add_likely_subtags(Unicode::LanguageID const& lang
return maximized;
}
Optional<String> resolve_most_likely_territory(Unicode::LanguageID const& language_id) asm("unicode_resolve_most_likely_territory");
Optional<String> resolve_most_likely_territory(Unicode::LanguageID const& language_id)
{
if (auto const* likely_subtag = resolve_likely_subtag(language_id); likely_subtag != nullptr)

View file

@ -11,10 +11,7 @@
#include <LibUnicode/CharacterTypes.h>
#include <LibUnicode/DateTimeFormat.h>
#include <LibUnicode/Locale.h>
#if ENABLE_UNICODE_DATA
# include <LibUnicode/UnicodeLocale.h>
#endif
#include <LibUnicode/UnicodeSymbols.h>
namespace Unicode {
@ -571,7 +568,9 @@ void canonicalize_unicode_extension_values(StringView key, String& value, bool r
static void transform_unicode_locale_id_to_canonical_syntax(LocaleID& locale_id)
{
auto canonicalize_language = [](LanguageID& language_id, bool force_lowercase) {
static auto const& symbols = Detail::Symbols::ensure_loaded();
auto canonicalize_language = [&](LanguageID& language_id, bool force_lowercase) {
language_id.language = language_id.language->to_lowercase();
if (language_id.script.has_value())
language_id.script = language_id.script->to_titlecase();
@ -580,9 +579,7 @@ static void transform_unicode_locale_id_to_canonical_syntax(LocaleID& locale_id)
for (auto& variant : language_id.variants)
variant = variant.to_lowercase();
#if ENABLE_UNICODE_DATA
Detail::resolve_complex_language_aliases(language_id);
#endif
symbols.resolve_complex_language_aliases(language_id);
if (auto alias = resolve_language_alias(*language_id.language); alias.has_value()) {
auto language_alias = parse_unicode_language_id(*alias);
@ -742,71 +739,55 @@ String const& default_locale()
return locale;
}
bool is_locale_available([[maybe_unused]] StringView locale)
bool is_locale_available(StringView locale)
{
#if ENABLE_UNICODE_DATA
return Detail::locale_from_string(locale).has_value();
#else
return false;
#endif
return locale_from_string(locale).has_value();
}
Optional<Locale> locale_from_string([[maybe_unused]] StringView locale)
Optional<Locale> locale_from_string(StringView locale)
{
#if ENABLE_UNICODE_DATA
return Detail::locale_from_string(locale);
#else
return {};
#endif
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.locale_from_string(locale);
}
Optional<StringView> get_locale_language_mapping([[maybe_unused]] StringView locale, [[maybe_unused]] StringView language)
Optional<StringView> get_locale_language_mapping(StringView locale, StringView language)
{
#if ENABLE_UNICODE_DATA
return Detail::get_locale_language_mapping(locale, language);
#else
return {};
#endif
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.get_locale_language_mapping(locale, language);
}
Optional<StringView> get_locale_territory_mapping([[maybe_unused]] StringView locale, [[maybe_unused]] StringView territory)
Optional<StringView> get_locale_territory_mapping(StringView locale, StringView territory)
{
#if ENABLE_UNICODE_DATA
return Detail::get_locale_territory_mapping(locale, territory);
#else
return {};
#endif
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.get_locale_territory_mapping(locale, territory);
}
Optional<StringView> get_locale_script_mapping([[maybe_unused]] StringView locale, [[maybe_unused]] StringView script)
Optional<StringView> get_locale_script_mapping(StringView locale, StringView script)
{
#if ENABLE_UNICODE_DATA
return Detail::get_locale_script_tag_mapping(locale, script);
#else
return {};
#endif
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.get_locale_script_tag_mapping(locale, script);
}
Optional<StringView> get_locale_currency_mapping([[maybe_unused]] StringView locale, [[maybe_unused]] StringView currency, [[maybe_unused]] Style style)
Optional<StringView> get_locale_currency_mapping(StringView locale, StringView currency, Style style)
{
#if ENABLE_UNICODE_DATA
static auto const& symbols = Detail::Symbols::ensure_loaded();
switch (style) {
case Style::Long:
return Detail::get_locale_long_currency_mapping(locale, currency);
return symbols.get_locale_long_currency_mapping(locale, currency);
case Style::Short:
return Detail::get_locale_short_currency_mapping(locale, currency);
return symbols.get_locale_short_currency_mapping(locale, currency);
case Style::Narrow:
return Detail::get_locale_narrow_currency_mapping(locale, currency);
return symbols.get_locale_narrow_currency_mapping(locale, currency);
case Style::Numeric:
return Detail::get_locale_numeric_currency_mapping(locale, currency);
return symbols.get_locale_numeric_currency_mapping(locale, currency);
default:
VERIFY_NOT_REACHED();
}
#endif
return {};
}
Vector<StringView> get_locale_key_mapping([[maybe_unused]] StringView locale, [[maybe_unused]] StringView keyword)
Vector<StringView> get_locale_key_mapping(StringView locale, StringView keyword)
{
#if ENABLE_UNICODE_DATA
if (keyword == "hc"sv) {
auto hour_cycles = get_regional_hour_cycles(locale);
@ -819,78 +800,58 @@ Vector<StringView> get_locale_key_mapping([[maybe_unused]] StringView locale, [[
return values;
}
if (auto values = Detail::get_locale_key_mapping(locale, keyword); values.has_value())
static auto const& symbols = Detail::Symbols::ensure_loaded();
if (auto values = symbols.get_locale_key_mapping(locale, keyword); values.has_value())
return values->split_view(',');
#endif
return {};
}
Optional<ListPatterns> get_locale_list_patterns([[maybe_unused]] StringView locale, [[maybe_unused]] StringView type, [[maybe_unused]] StringView style)
Optional<ListPatterns> get_locale_list_patterns(StringView locale, StringView type, StringView style)
{
#if ENABLE_UNICODE_DATA
return Detail::get_locale_list_pattern_mapping(locale, type, style);
#else
return {};
#endif
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.get_locale_list_pattern_mapping(locale, type, style);
}
Optional<StringView> resolve_language_alias(StringView language)
{
#if ENABLE_UNICODE_DATA
return Detail::resolve_language_alias(language);
#else
return language;
#endif
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.resolve_language_alias(language);
}
Optional<StringView> resolve_territory_alias(StringView territory)
{
#if ENABLE_UNICODE_DATA
return Detail::resolve_territory_alias(territory);
#else
return territory;
#endif
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.resolve_territory_alias(territory);
}
Optional<StringView> resolve_script_tag_alias(StringView script_tag)
{
#if ENABLE_UNICODE_DATA
return Detail::resolve_script_tag_alias(script_tag);
#else
return script_tag;
#endif
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.resolve_script_tag_alias(script_tag);
}
Optional<StringView> resolve_variant_alias(StringView variant)
{
#if ENABLE_UNICODE_DATA
return Detail::resolve_variant_alias(variant);
#else
return variant;
#endif
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.resolve_variant_alias(variant);
}
Optional<StringView> resolve_subdivision_alias(StringView subdivision)
{
#if ENABLE_UNICODE_DATA
return Detail::resolve_subdivision_alias(subdivision);
#else
return subdivision;
#endif
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.resolve_subdivision_alias(subdivision);
}
Optional<LanguageID> add_likely_subtags([[maybe_unused]] LanguageID const& language_id)
Optional<LanguageID> add_likely_subtags(LanguageID const& language_id)
{
#if ENABLE_UNICODE_DATA
return Detail::add_likely_subtags(language_id);
#else
return {};
#endif
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.add_likely_subtags(language_id);
}
Optional<LanguageID> remove_likely_subtags([[maybe_unused]] LanguageID const& language_id)
Optional<LanguageID> remove_likely_subtags(LanguageID const& language_id)
{
#if ENABLE_UNICODE_DATA
// https://www.unicode.org/reports/tr35/#Likely_Subtags
auto return_language_and_variants = [](auto language, auto variants) {
language.variants = move(variants);
@ -929,22 +890,18 @@ Optional<LanguageID> remove_likely_subtags([[maybe_unused]] LanguageID const& la
// 5. If you do not get a match, return max + variants.
return return_language_and_variants(maximized.release_value(), move(variants));
#else
return {};
#endif
}
String resolve_most_likely_territory([[maybe_unused]] LanguageID const& language_id, StringView territory_alias)
String resolve_most_likely_territory(LanguageID const& language_id, StringView territory_alias)
{
static auto const& symbols = Detail::Symbols::ensure_loaded();
auto aliases = territory_alias.split_view(' ');
#if ENABLE_UNICODE_DATA
if (aliases.size() > 1) {
auto territory = Detail::resolve_most_likely_territory(language_id);
auto territory = symbols.resolve_most_likely_territory(language_id);
if (territory.has_value() && aliases.contains_slow(*territory))
return territory.release_value();
}
#endif
return aliases[0].to_string();
}

View file

@ -15,6 +15,7 @@
# endif
#else
# include <AK/Function.h>
# include <LibUnicode/Locale.h>
#endif
namespace Unicode::Detail {
@ -28,7 +29,10 @@ template<typename ReturnType, typename... ParameterTypes>
struct FunctionStub<Function<ReturnType(ParameterTypes...)>> {
static constexpr auto make_stub()
{
return [](ParameterTypes...) -> ReturnType { return {}; };
if constexpr (IsVoid<ReturnType>)
return [](ParameterTypes...) {};
else
return [](ParameterTypes...) -> ReturnType { return {}; };
}
};
@ -87,6 +91,25 @@ Symbols const& Symbols::ensure_loaded()
load_symbol(symbols.code_point_has_script, "unicode_code_point_has_script");
load_symbol(symbols.code_point_has_script_extension, "unicode_code_point_has_script_extension");
load_symbol(symbols.locale_from_string, "unicode_locale_from_string");
load_symbol(symbols.get_locale_language_mapping, "unicode_get_locale_language_mapping");
load_symbol(symbols.get_locale_territory_mapping, "unicode_get_locale_territory_mapping");
load_symbol(symbols.get_locale_script_tag_mapping, "unicode_get_locale_script_tag_mapping");
load_symbol(symbols.get_locale_long_currency_mapping, "unicode_get_locale_long_currency_mapping");
load_symbol(symbols.get_locale_short_currency_mapping, "unicode_get_locale_short_currency_mapping");
load_symbol(symbols.get_locale_narrow_currency_mapping, "unicode_get_locale_narrow_currency_mapping");
load_symbol(symbols.get_locale_numeric_currency_mapping, "unicode_get_locale_numeric_currency_mapping");
load_symbol(symbols.get_locale_key_mapping, "unicode_get_locale_key_mapping");
load_symbol(symbols.get_locale_list_pattern_mapping, "unicode_get_locale_list_pattern_mapping");
load_symbol(symbols.resolve_language_alias, "unicode_resolve_language_alias");
load_symbol(symbols.resolve_territory_alias, "unicode_resolve_territory_alias");
load_symbol(symbols.resolve_script_tag_alias, "unicode_resolve_script_tag_alias");
load_symbol(symbols.resolve_variant_alias, "unicode_resolve_variant_alias");
load_symbol(symbols.resolve_subdivision_alias, "unicode_resolve_subdivision_alias");
load_symbol(symbols.resolve_complex_language_aliases, "unicode_resolve_complex_language_aliases");
load_symbol(symbols.add_likely_subtags, "unicode_add_likely_subtags");
load_symbol(symbols.resolve_most_likely_territory, "unicode_resolve_most_likely_territory");
initialized = true;
return symbols;
}

View file

@ -10,6 +10,7 @@
#include <AK/String.h>
#include <AK/StringView.h>
#include <AK/Types.h>
#include <AK/Vector.h>
#include <LibUnicode/Forward.h>
namespace Unicode::Detail {
@ -37,6 +38,32 @@ struct Symbols {
bool (*code_point_has_script)(u32, Script) { nullptr };
bool (*code_point_has_script_extension)(u32, Script) { nullptr };
// Loaded from UnicodeLocale.cpp:
Optional<Locale> (*locale_from_string)(StringView) { nullptr };
Optional<StringView> (*get_locale_language_mapping)(StringView, StringView) { nullptr };
Optional<StringView> (*get_locale_territory_mapping)(StringView, StringView) { nullptr };
Optional<StringView> (*get_locale_script_tag_mapping)(StringView, StringView) { nullptr };
Optional<StringView> (*get_locale_long_currency_mapping)(StringView, StringView) { nullptr };
Optional<StringView> (*get_locale_short_currency_mapping)(StringView, StringView) { nullptr };
Optional<StringView> (*get_locale_narrow_currency_mapping)(StringView, StringView) { nullptr };
Optional<StringView> (*get_locale_numeric_currency_mapping)(StringView, StringView) { nullptr };
Optional<StringView> (*get_locale_key_mapping)(StringView, StringView) { nullptr };
Optional<ListPatterns> (*get_locale_list_pattern_mapping)(StringView, StringView, StringView) { nullptr };
Optional<StringView> (*resolve_language_alias)(StringView) { nullptr };
Optional<StringView> (*resolve_territory_alias)(StringView) { nullptr };
Optional<StringView> (*resolve_script_tag_alias)(StringView) { nullptr };
Optional<StringView> (*resolve_variant_alias)(StringView) { nullptr };
Optional<StringView> (*resolve_subdivision_alias)(StringView) { nullptr };
void (*resolve_complex_language_aliases)(LanguageID&);
Optional<LanguageID> (*add_likely_subtags)(LanguageID const&);
Optional<String> (*resolve_most_likely_territory)(LanguageID const&);
private:
Symbols() = default;
};