summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2022-02-15 14:59:26 -0500
committerTim Flynn <trflynn89@pm.me>2022-02-16 07:23:07 -0500
commit70ede2825ed7c63d940787f8d349796ad2784f58 (patch)
treeb5f04b687388e62959865fb1fc1de86cecea8a30
parent71d86261c36ecc91bb169a587038e056e0929412 (diff)
downloadserenity-70ede2825ed7c63d940787f8d349796ad2784f58.zip
LibUnicode: Use BCP 47 data to filter valid calendar names
-rw-r--r--Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeDateTimeFormat.cpp33
-rw-r--r--Userland/Libraries/LibUnicode/DateTimeFormat.cpp1
-rw-r--r--Userland/Libraries/LibUnicode/DateTimeFormat.h2
-rw-r--r--Userland/Libraries/LibUnicode/Forward.h1
4 files changed, 25 insertions, 12 deletions
diff --git a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeDateTimeFormat.cpp b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeDateTimeFormat.cpp
index b6f3124d60..b156069124 100644
--- a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeDateTimeFormat.cpp
+++ b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeDateTimeFormat.cpp
@@ -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);
diff --git a/Userland/Libraries/LibUnicode/DateTimeFormat.cpp b/Userland/Libraries/LibUnicode/DateTimeFormat.cpp
index 6ec47d101d..63dbb72708 100644
--- a/Userland/Libraries/LibUnicode/DateTimeFormat.cpp
+++ b/Userland/Libraries/LibUnicode/DateTimeFormat.cpp
@@ -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 {}; }
diff --git a/Userland/Libraries/LibUnicode/DateTimeFormat.h b/Userland/Libraries/LibUnicode/DateTimeFormat.h
index 92343fa5ef..7ab2b46bca 100644
--- a/Userland/Libraries/LibUnicode/DateTimeFormat.h
+++ b/Userland/Libraries/LibUnicode/DateTimeFormat.h
@@ -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);
diff --git a/Userland/Libraries/LibUnicode/Forward.h b/Userland/Libraries/LibUnicode/Forward.h
index d801c5e70b..7dffc51a73 100644
--- a/Userland/Libraries/LibUnicode/Forward.h
+++ b/Userland/Libraries/LibUnicode/Forward.h
@@ -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;