LibUnicode: Dynamically load the generated UnicodeDateTimeFormat symbols

This commit is contained in:
Timothy Flynn 2021-12-15 23:19:17 -05:00 committed by Brian Gianforcaro
parent a1f0ca59ae
commit 15e1498419
Notes: sideshowbarker 2024-07-17 22:27:38 +09:00
4 changed files with 106 additions and 121 deletions

View File

@ -1500,9 +1500,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 <LibUnicode/Forward.h>
#include <AK/Types.h>
namespace Unicode {
)~~~");
@ -1512,33 +1510,6 @@ namespace Unicode {
generate_enum(generator, format_identifier, "TimeZone"sv, {}, locale_data.time_zones);
generator.append(R"~~~(
namespace Detail {
Optional<Calendar> calendar_from_string(StringView calendar);
Optional<HourCycleRegion> hour_cycle_region_from_string(StringView hour_cycle_region);
Vector<Unicode::HourCycle> get_regional_hour_cycles(StringView region);
Optional<Unicode::CalendarFormat> get_calendar_date_format(StringView locale, StringView calendar);
Optional<Unicode::CalendarFormat> get_calendar_time_format(StringView locale, StringView calendar);
Optional<Unicode::CalendarFormat> get_calendar_date_time_format(StringView locale, StringView calendar);
Vector<Unicode::CalendarPattern> get_calendar_available_formats(StringView locale, StringView calendar);
Optional<Unicode::CalendarRangePattern> get_calendar_default_range_format(StringView locale, StringView calendar);
Vector<Unicode::CalendarRangePattern> get_calendar_range_formats(StringView locale, StringView calendar, StringView skeleton);
Vector<Unicode::CalendarRangePattern> get_calendar_range12_formats(StringView locale, StringView calendar, StringView skeleton);
Optional<StringView> get_calendar_era_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Unicode::Era value);
Optional<StringView> get_calendar_month_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Unicode::Month value);
Optional<StringView> get_calendar_weekday_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Unicode::Weekday value);
Optional<StringView> get_calendar_day_period_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Unicode::DayPeriod value);
Optional<StringView> get_calendar_day_period_symbol_for_hour(StringView locale, StringView calendar, CalendarPatternStyle style, u8 hour);
Optional<TimeZone> time_zone_from_string(StringView time_zone);
Optional<StringView> get_time_zone_name(StringView locale, StringView time_zone, CalendarPatternStyle style);
}
}
)~~~");
@ -1567,6 +1538,8 @@ 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/StringView.h>
#include <LibUnicode/DateTimeFormat.h>
#include <LibUnicode/Locale.h>
#include <LibUnicode/UnicodeDateTimeFormat.h>
@ -1578,7 +1551,7 @@ namespace Unicode::Detail {
generator.append(R"~~~(
template <typename SourceType, typename TargetType>
void convert_calendar_fields(SourceType const& source, TargetType& target)
static void convert_calendar_fields(SourceType const& source, TargetType& target)
{
if (source.era != -1)
target.era = static_cast<Unicode::CalendarPatternStyle>(source.era);
@ -1800,6 +1773,7 @@ static constexpr Array<@type@, @size@> @name@ { {)~~~");
append_from_string("TimeZone"sv, "time_zone"sv, locale_data.time_zones);
generator.append(R"~~~(
Vector<Unicode::HourCycle> get_regional_hour_cycles(StringView region) asm("unicode_get_regional_hour_cycles");
Vector<Unicode::HourCycle> get_regional_hour_cycles(StringView region)
{
auto region_value = hour_cycle_region_from_string(region);
@ -1839,6 +1813,7 @@ static CalendarData const* find_calendar_data(StringView locale, StringView cale
return &s_calendars[calendar_index];
}
Optional<Unicode::CalendarFormat> get_calendar_date_format(StringView locale, StringView calendar) asm("unicode_get_calendar_date_format");
Optional<Unicode::CalendarFormat> get_calendar_date_format(StringView locale, StringView calendar)
{
if (auto const* data = find_calendar_data(locale, calendar); data != nullptr) {
@ -1848,6 +1823,7 @@ Optional<Unicode::CalendarFormat> get_calendar_date_format(StringView locale, St
return {};
}
Optional<Unicode::CalendarFormat> get_calendar_time_format(StringView locale, StringView calendar) asm("unicode_get_calendar_time_format");
Optional<Unicode::CalendarFormat> get_calendar_time_format(StringView locale, StringView calendar)
{
if (auto const* data = find_calendar_data(locale, calendar); data != nullptr) {
@ -1857,6 +1833,7 @@ Optional<Unicode::CalendarFormat> get_calendar_time_format(StringView locale, St
return {};
}
Optional<Unicode::CalendarFormat> get_calendar_date_time_format(StringView locale, StringView calendar) asm("unicode_get_calendar_date_time_format");
Optional<Unicode::CalendarFormat> get_calendar_date_time_format(StringView locale, StringView calendar)
{
if (auto const* data = find_calendar_data(locale, calendar); data != nullptr) {
@ -1866,6 +1843,7 @@ Optional<Unicode::CalendarFormat> get_calendar_date_time_format(StringView local
return {};
}
Vector<Unicode::CalendarPattern> get_calendar_available_formats(StringView locale, StringView calendar) asm("unicode_get_calendar_available_formats");
Vector<Unicode::CalendarPattern> get_calendar_available_formats(StringView locale, StringView calendar)
{
Vector<Unicode::CalendarPattern> result {};
@ -1881,6 +1859,7 @@ Vector<Unicode::CalendarPattern> get_calendar_available_formats(StringView local
return result;
}
Optional<Unicode::CalendarRangePattern> get_calendar_default_range_format(StringView locale, StringView calendar) asm("unicode_get_calendar_default_range_format");
Optional<Unicode::CalendarRangePattern> get_calendar_default_range_format(StringView locale, StringView calendar)
{
if (auto const* data = find_calendar_data(locale, calendar); data != nullptr) {
@ -1891,7 +1870,9 @@ Optional<Unicode::CalendarRangePattern> get_calendar_default_range_format(String
return {};
}
Vector<Unicode::CalendarRangePattern> get_calendar_range_formats(StringView locale, StringView calendar, StringView skeleton) {
Vector<Unicode::CalendarRangePattern> get_calendar_range_formats(StringView locale, StringView calendar, StringView skeleton) asm("unicode_get_calendar_range_formats");
Vector<Unicode::CalendarRangePattern> get_calendar_range_formats(StringView locale, StringView calendar, StringView skeleton)
{
Vector<Unicode::CalendarRangePattern> result {};
if (auto const* data = find_calendar_data(locale, calendar); data != nullptr) {
@ -1908,7 +1889,9 @@ Vector<Unicode::CalendarRangePattern> get_calendar_range_formats(StringView loca
return result;
}
Vector<Unicode::CalendarRangePattern> get_calendar_range12_formats(StringView locale, StringView calendar, StringView skeleton) {
Vector<Unicode::CalendarRangePattern> get_calendar_range12_formats(StringView locale, StringView calendar, StringView skeleton) asm("unicode_get_calendar_range12_formats");
Vector<Unicode::CalendarRangePattern> get_calendar_range12_formats(StringView locale, StringView calendar, StringView skeleton)
{
Vector<Unicode::CalendarRangePattern> result {};
if (auto const* data = find_calendar_data(locale, calendar); data != nullptr) {
@ -1956,7 +1939,8 @@ static Span<@string_index_type@ const> find_calendar_symbols(StringView locale,
return {};
}
Optional<StringView> get_calendar_era_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Unicode::Era value)
Optional<StringView> get_calendar_era_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Era value) asm("unicode_get_calendar_era_symbol");
Optional<StringView> get_calendar_era_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Era value)
{
auto symbols = find_calendar_symbols(locale, calendar, CalendarSymbol::Era, style);
@ -1966,7 +1950,8 @@ Optional<StringView> get_calendar_era_symbol(StringView locale, StringView calen
return {};
}
Optional<StringView> get_calendar_month_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Unicode::Month value)
Optional<StringView> get_calendar_month_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Month value) asm("unicode_get_calendar_month_symbol");
Optional<StringView> get_calendar_month_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Month value)
{
auto symbols = find_calendar_symbols(locale, calendar, CalendarSymbol::Month, style);
@ -1976,7 +1961,8 @@ Optional<StringView> get_calendar_month_symbol(StringView locale, StringView cal
return {};
}
Optional<StringView> get_calendar_weekday_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Unicode::Weekday value)
Optional<StringView> get_calendar_weekday_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Weekday value) asm("unicode_get_calendar_weekday_symbol");
Optional<StringView> get_calendar_weekday_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Weekday value)
{
auto symbols = find_calendar_symbols(locale, calendar, CalendarSymbol::Weekday, style);
@ -1986,7 +1972,8 @@ Optional<StringView> get_calendar_weekday_symbol(StringView locale, StringView c
return {};
}
Optional<StringView> get_calendar_day_period_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Unicode::DayPeriod value)
Optional<StringView> get_calendar_day_period_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, DayPeriod value) asm("unicode_get_calendar_day_period_symbol");
Optional<StringView> get_calendar_day_period_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, DayPeriod value)
{
auto symbols = find_calendar_symbols(locale, calendar, CalendarSymbol::DayPeriod, style);
@ -1996,6 +1983,7 @@ Optional<StringView> get_calendar_day_period_symbol(StringView locale, StringVie
return {};
}
Optional<StringView> get_calendar_day_period_symbol_for_hour(StringView locale, StringView calendar, CalendarPatternStyle style, u8 hour) asm("unicode_get_calendar_day_period_symbol_for_hour");
Optional<StringView> get_calendar_day_period_symbol_for_hour(StringView locale, StringView calendar, CalendarPatternStyle style, u8 hour)
{
auto locale_value = locale_from_string(locale);
@ -2043,6 +2031,7 @@ static TimeZoneData const* find_time_zone_data(StringView locale, StringView tim
return &s_time_zones[time_zone_index];
}
Optional<StringView> get_time_zone_name(StringView locale, StringView time_zone, CalendarPatternStyle style) asm("unicode_get_time_zone_name");
Optional<StringView> get_time_zone_name(StringView locale, StringView time_zone, CalendarPatternStyle style)
{
if (auto const* data = find_time_zone_data(locale, time_zone); data != nullptr) {

View File

@ -8,10 +8,7 @@
#include <AK/StringBuilder.h>
#include <LibUnicode/DateTimeFormat.h>
#include <LibUnicode/Locale.h>
#if ENABLE_UNICODE_DATA
# include <LibUnicode/UnicodeDateTimeFormat.h>
#endif
#include <LibUnicode/UnicodeSymbols.h>
namespace Unicode {
@ -78,17 +75,14 @@ StringView calendar_pattern_style_to_string(CalendarPatternStyle style)
}
// https://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
Vector<Unicode::HourCycle> get_regional_hour_cycles([[maybe_unused]] StringView locale)
Vector<Unicode::HourCycle> get_regional_hour_cycles(StringView locale)
{
#if ENABLE_UNICODE_DATA
if (auto hour_cycles = Detail::get_regional_hour_cycles(locale); !hour_cycles.is_empty())
static auto const& symbols = Detail::Symbols::ensure_loaded();
if (auto hour_cycles = symbols.get_regional_hour_cycles(locale); !hour_cycles.is_empty())
return hour_cycles;
auto return_default_hour_cycles = []() {
auto hour_cycles = Detail::get_regional_hour_cycles("001"sv);
VERIFY(!hour_cycles.is_empty());
return hour_cycles;
};
auto return_default_hour_cycles = [&]() { return symbols.get_regional_hour_cycles("001"sv); };
auto language = parse_unicode_language_id(locale);
if (!language.has_value())
@ -99,13 +93,10 @@ Vector<Unicode::HourCycle> get_regional_hour_cycles([[maybe_unused]] StringView
if (!language.has_value() || !language->region.has_value())
return return_default_hour_cycles();
if (auto hour_cycles = Detail::get_regional_hour_cycles(*language->region); !hour_cycles.is_empty())
if (auto hour_cycles = symbols.get_regional_hour_cycles(*language->region); !hour_cycles.is_empty())
return hour_cycles;
return return_default_hour_cycles();
#else
return {};
#endif
}
Optional<Unicode::HourCycle> get_default_regional_hour_cycle(StringView locale)
@ -156,112 +147,80 @@ String combine_skeletons(StringView first, StringView second)
return builder.build();
}
Optional<CalendarFormat> get_calendar_format([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar, [[maybe_unused]] CalendarFormatType type)
Optional<CalendarFormat> get_calendar_format(StringView locale, StringView calendar, CalendarFormatType type)
{
#if ENABLE_UNICODE_DATA
static auto const& symbols = Detail::Symbols::ensure_loaded();
switch (type) {
case CalendarFormatType::Date:
return Detail::get_calendar_date_format(locale, calendar);
return symbols.get_calendar_date_format(locale, calendar);
case CalendarFormatType::Time:
return Detail::get_calendar_time_format(locale, calendar);
return symbols.get_calendar_time_format(locale, calendar);
case CalendarFormatType::DateTime:
return Detail::get_calendar_date_time_format(locale, calendar);
return symbols.get_calendar_date_time_format(locale, calendar);
default:
VERIFY_NOT_REACHED();
}
#else
return {};
#endif
}
Vector<CalendarPattern> get_calendar_available_formats([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar)
Vector<CalendarPattern> get_calendar_available_formats(StringView locale, StringView calendar)
{
#if ENABLE_UNICODE_DATA
return Detail::get_calendar_available_formats(locale, calendar);
#else
return {};
#endif
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.get_calendar_available_formats(locale, calendar);
}
Optional<Unicode::CalendarRangePattern> get_calendar_default_range_format([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar)
Optional<Unicode::CalendarRangePattern> get_calendar_default_range_format(StringView locale, StringView calendar)
{
#if ENABLE_UNICODE_DATA
return Detail::get_calendar_default_range_format(locale, calendar);
#else
return {};
#endif
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.get_calendar_default_range_format(locale, calendar);
}
Vector<Unicode::CalendarRangePattern> get_calendar_range_formats([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar, [[maybe_unused]] StringView skeleton)
Vector<Unicode::CalendarRangePattern> get_calendar_range_formats(StringView locale, StringView calendar, StringView skeleton)
{
#if ENABLE_UNICODE_DATA
return Detail::get_calendar_range_formats(locale, calendar, skeleton);
#else
return {};
#endif
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.get_calendar_range_formats(locale, calendar, skeleton);
}
Vector<Unicode::CalendarRangePattern> get_calendar_range12_formats([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar, [[maybe_unused]] StringView skeleton)
Vector<Unicode::CalendarRangePattern> get_calendar_range12_formats(StringView locale, StringView calendar, StringView skeleton)
{
#if ENABLE_UNICODE_DATA
return Detail::get_calendar_range12_formats(locale, calendar, skeleton);
#else
return {};
#endif
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.get_calendar_range12_formats(locale, calendar, skeleton);
}
Optional<StringView> get_calendar_era_symbol([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar, [[maybe_unused]] CalendarPatternStyle style, [[maybe_unused]] Unicode::Era value)
Optional<StringView> get_calendar_era_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Unicode::Era value)
{
#if ENABLE_UNICODE_DATA
return Detail::get_calendar_era_symbol(locale, calendar, style, value);
#else
return {};
#endif
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.get_calendar_era_symbol(locale, calendar, style, value);
}
Optional<StringView> get_calendar_month_symbol([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar, [[maybe_unused]] CalendarPatternStyle style, [[maybe_unused]] Unicode::Month value)
Optional<StringView> get_calendar_month_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Unicode::Month value)
{
#if ENABLE_UNICODE_DATA
return Detail::get_calendar_month_symbol(locale, calendar, style, value);
#else
return {};
#endif
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.get_calendar_month_symbol(locale, calendar, style, value);
}
Optional<StringView> get_calendar_weekday_symbol([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar, [[maybe_unused]] CalendarPatternStyle style, [[maybe_unused]] Unicode::Weekday value)
Optional<StringView> get_calendar_weekday_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Unicode::Weekday value)
{
#if ENABLE_UNICODE_DATA
return Detail::get_calendar_weekday_symbol(locale, calendar, style, value);
#else
return {};
#endif
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.get_calendar_weekday_symbol(locale, calendar, style, value);
}
Optional<StringView> get_calendar_day_period_symbol([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar, [[maybe_unused]] CalendarPatternStyle style, [[maybe_unused]] Unicode::DayPeriod value)
Optional<StringView> get_calendar_day_period_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Unicode::DayPeriod value)
{
#if ENABLE_UNICODE_DATA
return Detail::get_calendar_day_period_symbol(locale, calendar, style, value);
#else
return {};
#endif
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.get_calendar_day_period_symbol(locale, calendar, style, value);
}
Optional<StringView> get_calendar_day_period_symbol_for_hour([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar, [[maybe_unused]] CalendarPatternStyle style, [[maybe_unused]] u8 hour)
Optional<StringView> get_calendar_day_period_symbol_for_hour(StringView locale, StringView calendar, CalendarPatternStyle style, u8 hour)
{
#if ENABLE_UNICODE_DATA
return Detail::get_calendar_day_period_symbol_for_hour(locale, calendar, style, hour);
#else
return {};
#endif
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.get_calendar_day_period_symbol_for_hour(locale, calendar, style, hour);
}
Optional<StringView> get_time_zone_name([[maybe_unused]] StringView locale, [[maybe_unused]] StringView time_zone, [[maybe_unused]] CalendarPatternStyle style)
Optional<StringView> get_time_zone_name(StringView locale, StringView time_zone, CalendarPatternStyle style)
{
#if ENABLE_UNICODE_DATA
return Detail::get_time_zone_name(locale, time_zone, style);
#else
return {};
#endif
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.get_time_zone_name(locale, time_zone, style);
}
}

View File

@ -15,6 +15,7 @@
# endif
#else
# include <AK/Function.h>
# include <LibUnicode/DateTimeFormat.h>
# include <LibUnicode/Locale.h>
# include <LibUnicode/NumberFormat.h>
#endif
@ -117,6 +118,21 @@ Symbols const& Symbols::ensure_loaded()
load_symbol(symbols.get_compact_number_system_formats, "unicode_get_compact_number_system_formats");
load_symbol(symbols.get_unit_formats, "unicode_get_unit_formats");
load_symbol(symbols.get_regional_hour_cycles, "unicode_get_regional_hour_cycles");
load_symbol(symbols.get_calendar_date_format, "unicode_get_calendar_date_format");
load_symbol(symbols.get_calendar_time_format, "unicode_get_calendar_time_format");
load_symbol(symbols.get_calendar_date_time_format, "unicode_get_calendar_date_time_format");
load_symbol(symbols.get_calendar_available_formats, "unicode_get_calendar_available_formats");
load_symbol(symbols.get_calendar_default_range_format, "unicode_get_calendar_default_range_format");
load_symbol(symbols.get_calendar_range_formats, "unicode_get_calendar_range_formats");
load_symbol(symbols.get_calendar_range12_formats, "unicode_get_calendar_range12_formats");
load_symbol(symbols.get_calendar_era_symbol, "unicode_get_calendar_era_symbol");
load_symbol(symbols.get_calendar_month_symbol, "unicode_get_calendar_month_symbol");
load_symbol(symbols.get_calendar_weekday_symbol, "unicode_get_calendar_weekday_symbol");
load_symbol(symbols.get_calendar_day_period_symbol, "unicode_get_calendar_day_period_symbol");
load_symbol(symbols.get_calendar_day_period_symbol_for_hour, "unicode_get_calendar_day_period_symbol_for_hour");
load_symbol(symbols.get_time_zone_name, "unicode_get_time_zone_name");
initialized = true;
return symbols;
}

View File

@ -72,6 +72,27 @@ struct Symbols {
Vector<NumberFormat> (*get_compact_number_system_formats)(StringView, StringView, CompactNumberFormatType);
Vector<NumberFormat> (*get_unit_formats)(StringView, StringView, Style);
// Loaded from UnicodeDateTimeFormat.cpp:
Vector<HourCycle> (*get_regional_hour_cycles)(StringView) { nullptr };
Optional<CalendarFormat> (*get_calendar_date_format)(StringView, StringView) { nullptr };
Optional<CalendarFormat> (*get_calendar_time_format)(StringView, StringView) { nullptr };
Optional<CalendarFormat> (*get_calendar_date_time_format)(StringView, StringView) { nullptr };
Vector<CalendarPattern> (*get_calendar_available_formats)(StringView, StringView) { nullptr };
Optional<CalendarRangePattern> (*get_calendar_default_range_format)(StringView, StringView) { nullptr };
Vector<CalendarRangePattern> (*get_calendar_range_formats)(StringView, StringView, StringView) { nullptr };
Vector<CalendarRangePattern> (*get_calendar_range12_formats)(StringView, StringView, StringView) { nullptr };
Optional<StringView> (*get_calendar_era_symbol)(StringView, StringView, CalendarPatternStyle, Era) { nullptr };
Optional<StringView> (*get_calendar_month_symbol)(StringView, StringView, CalendarPatternStyle, Month) { nullptr };
Optional<StringView> (*get_calendar_weekday_symbol)(StringView, StringView, CalendarPatternStyle, Weekday) { nullptr };
Optional<StringView> (*get_calendar_day_period_symbol)(StringView, StringView, CalendarPatternStyle, DayPeriod) { nullptr };
Optional<StringView> (*get_calendar_day_period_symbol_for_hour)(StringView, StringView, CalendarPatternStyle, u8) { nullptr };
Optional<StringView> (*get_time_zone_name)(StringView, StringView, CalendarPatternStyle) { nullptr };
private:
Symbols() = default;
};