LibUnicode: Use BCP 47 data to filter valid calendar names

This commit is contained in:
Timothy Flynn 2022-02-15 14:59:26 -05:00 committed by Tim Flynn
parent 71d86261c3
commit 70ede2825e
Notes: sideshowbarker 2024-07-17 18:43:16 +09:00
4 changed files with 25 additions and 12 deletions

View file

@ -569,10 +569,6 @@ struct UnicodeLocaleData {
Vector<String> time_zones { "UTC"sv };
Vector<String> calendars;
Vector<Alias> calendar_aliases {
// FIXME: Aliases should come from BCP47. See: https://unicode-org.atlassian.net/browse/CLDR-15158
{ "gregorian"sv, "gregory"sv },
};
};
static Optional<Unicode::DayPeriod> day_period_from_string(StringView day_period)
@ -1633,7 +1629,7 @@ static ErrorOr<void> generate_unicode_locale_header(Core::Stream::BufferedFile&
namespace Unicode {
)~~~");
generate_enum(generator, format_identifier, "Calendar"sv, {}, locale_data.calendars, locale_data.calendar_aliases);
generate_enum(generator, format_identifier, "Calendar"sv, {}, locale_data.calendars);
generate_enum(generator, format_identifier, "HourCycleRegion"sv, {}, locale_data.hour_cycle_regions);
generator.append(R"~~~(
@ -1672,6 +1668,7 @@ static ErrorOr<void> generate_unicode_locale_implementation(Core::Stream::Buffer
#include <LibUnicode/DateTimeFormat.h>
#include <LibUnicode/Locale.h>
#include <LibUnicode/UnicodeDateTimeFormat.h>
#include <LibUnicode/UnicodeLocale.h>
namespace Unicode {
)~~~");
@ -1929,10 +1926,26 @@ static constexpr Array<@type@, @size@> @name@ { {)~~~");
generate_value_from_string(generator, "{}_from_string"sv, enum_title, enum_snake, move(hashes));
};
append_from_string("Calendar"sv, "calendar"sv, locale_data.calendars, locale_data.calendar_aliases);
append_from_string("HourCycleRegion"sv, "hour_cycle_region"sv, locale_data.hour_cycle_regions);
generator.append(R"~~~(
static Optional<Calendar> keyword_to_calendar(KeywordCalendar keyword)
{
switch (keyword) {)~~~");
for (auto const& calendar : locale_data.calendars) {
generator.set("name"sv, format_identifier({}, calendar));
generator.append(R"~~~(
case KeywordCalendar::@name@:
return Calendar::@name@;)~~~");
}
generator.append(R"~~~(
default:
return {};
}
}
Vector<HourCycle> get_regional_hour_cycles(StringView region)
{
auto region_value = hour_cycle_region_from_string(region);
@ -1959,9 +1972,13 @@ static CalendarData const* find_calendar_data(StringView locale, StringView cale
if (!locale_value.has_value())
return nullptr;
auto calendar_value = calendar_from_string(calendar);
auto calendar_keyword = keyword_ca_from_string(calendar);
if (!calendar_keyword.has_value())
return {};
auto calendar_value = keyword_to_calendar(*calendar_keyword);
if (!calendar_value.has_value())
return nullptr;
return {};
auto locale_index = to_underlying(*locale_value) - 1; // Subtract 1 because 0 == Locale::None.
size_t calendar_index = to_underlying(*calendar_value);

View file

@ -91,7 +91,6 @@ StringView calendar_pattern_style_to_string(CalendarPatternStyle style)
}
}
Optional<Calendar> __attribute__((weak)) calendar_from_string(StringView) { return {}; }
Optional<HourCycleRegion> __attribute__((weak)) hour_cycle_region_from_string(StringView) { return {}; }
Vector<HourCycle> __attribute__((weak)) get_regional_hour_cycles(StringView) { return {}; }

View file

@ -186,9 +186,7 @@ StringView hour_cycle_to_string(HourCycle hour_cycle);
CalendarPatternStyle calendar_pattern_style_from_string(StringView style);
StringView calendar_pattern_style_to_string(CalendarPatternStyle style);
Optional<Calendar> calendar_from_string(StringView calendar);
Optional<HourCycleRegion> hour_cycle_region_from_string(StringView hour_cycle_region);
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);

View file

@ -11,7 +11,6 @@
namespace Unicode {
enum class Block : u16;
enum class Calendar : u8;
enum class CalendarFormatType : u8;
enum class CalendarPatternStyle : u8;
enum class CalendarSymbol : u8;