1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:17:44 +00:00

LibUnicode: Convert UnicodeDateTimeFormat to link with weak symbols

This commit is contained in:
Timothy Flynn 2022-01-04 11:29:42 -05:00 committed by Linus Groh
parent 98709d9be1
commit ba4cdf34f8
6 changed files with 85 additions and 167 deletions

View file

@ -8,7 +8,6 @@
#include <AK/StringBuilder.h>
#include <LibUnicode/DateTimeFormat.h>
#include <LibUnicode/Locale.h>
#include <LibUnicode/UnicodeSymbols.h>
namespace Unicode {
@ -90,15 +89,18 @@ 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(StringView locale)
{
static auto const& symbols = Detail::Symbols::ensure_loaded();
Optional<Calendar> __attribute__((weak)) calendar_from_string(StringView) { return {}; }
Optional<HourCycleRegion> __attribute__((weak)) hour_cycle_region_from_string(StringView) { return {}; }
Optional<TimeZone> __attribute__((weak)) time_zone_from_string(StringView) { return {}; }
Vector<HourCycle> __attribute__((weak)) get_regional_hour_cycles(StringView) { return {}; }
if (auto hour_cycles = symbols.get_regional_hour_cycles(locale); !hour_cycles.is_empty())
// https://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
Vector<Unicode::HourCycle> get_locale_hour_cycles(StringView locale)
{
if (auto hour_cycles = get_regional_hour_cycles(locale); !hour_cycles.is_empty())
return hour_cycles;
auto return_default_hour_cycles = [&]() { return symbols.get_regional_hour_cycles("001"sv); };
auto return_default_hour_cycles = [&]() { return get_regional_hour_cycles("001"sv); };
auto language = parse_unicode_language_id(locale);
if (!language.has_value())
@ -109,7 +111,7 @@ Vector<Unicode::HourCycle> get_regional_hour_cycles(StringView locale)
if (!language.has_value() || !language->region.has_value())
return return_default_hour_cycles();
if (auto hour_cycles = symbols.get_regional_hour_cycles(*language->region); !hour_cycles.is_empty())
if (auto hour_cycles = get_regional_hour_cycles(*language->region); !hour_cycles.is_empty())
return hour_cycles;
return return_default_hour_cycles();
@ -117,7 +119,7 @@ Vector<Unicode::HourCycle> get_regional_hour_cycles(StringView locale)
Optional<Unicode::HourCycle> get_default_regional_hour_cycle(StringView locale)
{
if (auto hour_cycles = get_regional_hour_cycles(locale); !hour_cycles.is_empty())
if (auto hour_cycles = get_locale_hour_cycles(locale); !hour_cycles.is_empty())
return hour_cycles.first();
return {};
}
@ -163,80 +165,33 @@ String combine_skeletons(StringView first, StringView second)
return builder.build();
}
Optional<CalendarFormat> __attribute__((weak)) get_calendar_date_format(StringView, StringView) { return {}; }
Optional<CalendarFormat> __attribute__((weak)) get_calendar_time_format(StringView, StringView) { return {}; }
Optional<CalendarFormat> __attribute__((weak)) get_calendar_date_time_format(StringView, StringView) { return {}; }
Optional<CalendarFormat> get_calendar_format(StringView locale, StringView calendar, CalendarFormatType type)
{
static auto const& symbols = Detail::Symbols::ensure_loaded();
switch (type) {
case CalendarFormatType::Date:
return symbols.get_calendar_date_format(locale, calendar);
return get_calendar_date_format(locale, calendar);
case CalendarFormatType::Time:
return symbols.get_calendar_time_format(locale, calendar);
return get_calendar_time_format(locale, calendar);
case CalendarFormatType::DateTime:
return symbols.get_calendar_date_time_format(locale, calendar);
return get_calendar_date_time_format(locale, calendar);
default:
VERIFY_NOT_REACHED();
}
}
Vector<CalendarPattern> get_calendar_available_formats(StringView locale, StringView calendar)
{
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.get_calendar_available_formats(locale, calendar);
}
Optional<Unicode::CalendarRangePattern> get_calendar_default_range_format(StringView locale, StringView calendar)
{
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.get_calendar_default_range_format(locale, calendar);
}
Vector<Unicode::CalendarRangePattern> get_calendar_range_formats(StringView locale, StringView calendar, StringView skeleton)
{
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.get_calendar_range_formats(locale, calendar, skeleton);
}
Vector<Unicode::CalendarRangePattern> get_calendar_range12_formats(StringView locale, StringView calendar, StringView skeleton)
{
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.get_calendar_range12_formats(locale, calendar, skeleton);
}
Optional<StringView> get_calendar_era_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Unicode::Era value)
{
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.get_calendar_era_symbol(locale, calendar, style, value);
}
Optional<StringView> get_calendar_month_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Unicode::Month value)
{
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.get_calendar_month_symbol(locale, calendar, style, value);
}
Optional<StringView> get_calendar_weekday_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Unicode::Weekday value)
{
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(StringView locale, StringView calendar, CalendarPatternStyle style, Unicode::DayPeriod value)
{
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(StringView locale, StringView calendar, CalendarPatternStyle style, u8 hour)
{
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(StringView locale, StringView time_zone, CalendarPatternStyle style)
{
static auto const& symbols = Detail::Symbols::ensure_loaded();
return symbols.get_time_zone_name(locale, time_zone, style);
}
Vector<CalendarPattern> __attribute__((weak)) get_calendar_available_formats(StringView, StringView) { return {}; }
Optional<CalendarRangePattern> __attribute__((weak)) get_calendar_default_range_format(StringView, StringView) { return {}; }
Vector<CalendarRangePattern> __attribute__((weak)) get_calendar_range_formats(StringView, StringView, StringView) { return {}; }
Vector<CalendarRangePattern> __attribute__((weak)) get_calendar_range12_formats(StringView, StringView, StringView) { return {}; }
Optional<StringView> __attribute__((weak)) get_calendar_era_symbol(StringView, StringView, CalendarPatternStyle, Era) { return {}; }
Optional<StringView> __attribute__((weak)) get_calendar_month_symbol(StringView, StringView, CalendarPatternStyle, Month) { return {}; }
Optional<StringView> __attribute__((weak)) get_calendar_weekday_symbol(StringView, StringView, CalendarPatternStyle, Weekday) { return {}; }
Optional<StringView> __attribute__((weak)) get_calendar_day_period_symbol(StringView, StringView, CalendarPatternStyle, DayPeriod) { return {}; }
Optional<StringView> __attribute__((weak)) get_calendar_day_period_symbol_for_hour(StringView, StringView, CalendarPatternStyle, u8) { return {}; }
Optional<StringView> __attribute__((weak)) get_time_zone_name(StringView, StringView, CalendarPatternStyle) { return {}; }
}

View file

@ -168,21 +168,35 @@ enum class CalendarSymbol : u8 {
HourCycle hour_cycle_from_string(StringView hour_cycle);
StringView hour_cycle_to_string(HourCycle hour_cycle);
CalendarPatternStyle calendar_pattern_style_from_string(StringView style);
StringView calendar_pattern_style_to_string(CalendarPatternStyle style);
Vector<Unicode::HourCycle> get_regional_hour_cycles(StringView locale);
Optional<Calendar> calendar_from_string(StringView calendar);
Optional<HourCycleRegion> hour_cycle_region_from_string(StringView hour_cycle_region);
Optional<TimeZone> time_zone_from_string(StringView time_zone);
Vector<HourCycle> get_regional_hour_cycles(StringView region);
Vector<Unicode::HourCycle> get_locale_hour_cycles(StringView locale);
Optional<Unicode::HourCycle> get_default_regional_hour_cycle(StringView locale);
String combine_skeletons(StringView first, StringView second);
Optional<CalendarFormat> get_calendar_date_format(StringView locale, StringView calendar);
Optional<CalendarFormat> get_calendar_time_format(StringView locale, StringView calendar);
Optional<CalendarFormat> get_calendar_date_time_format(StringView locale, StringView calendar);
Optional<CalendarFormat> get_calendar_format(StringView locale, StringView calendar, CalendarFormatType type);
Vector<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<StringView> get_time_zone_name(StringView locale, StringView time_zone, CalendarPatternStyle style);
}

View file

@ -789,7 +789,7 @@ Optional<StringView> get_locale_currency_mapping(StringView locale, StringView c
Vector<StringView> get_locale_key_mapping(StringView locale, StringView keyword)
{
if (keyword == "hc"sv) {
auto hour_cycles = get_regional_hour_cycles(locale);
auto hour_cycles = get_locale_hour_cycles(locale);
Vector<StringView> values;
values.ensure_capacity(hour_cycles.size());

View file

@ -15,7 +15,6 @@
# endif
#else
# include <AK/Function.h>
# include <LibUnicode/DateTimeFormat.h>
# include <LibUnicode/Locale.h>
#endif
@ -111,21 +110,6 @@ Symbols const& Symbols::ensure_loaded()
load_symbol(symbols.add_likely_subtags, "unicode_add_likely_subtags");
load_symbol(symbols.resolve_most_likely_territory, "unicode_resolve_most_likely_territory");
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

@ -64,27 +64,6 @@ struct Symbols {
Optional<LanguageID> (*add_likely_subtags)(LanguageID const&);
Optional<String> (*resolve_most_likely_territory)(LanguageID const&);
// 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;
};