diff options
author | Timothy Flynn <trflynn89@pm.me> | 2023-01-27 11:12:01 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-01-27 18:00:17 +0000 |
commit | 5e29e04122bdabc87e1cde52cfbd3cf082e6b793 (patch) | |
tree | c2d5e46f7861fd80556cc467acf9be9725146d30 /Meta | |
parent | b2097f4059625a6bc8c757c5f6cb3b3fb04644a7 (diff) | |
download | serenity-5e29e04122bdabc87e1cde52cfbd3cf082e6b793.zip |
LibJS+LibLocale: Propagate errors from find_regional_values_for_locale
This had quite the footprint.
Diffstat (limited to 'Meta')
3 files changed, 58 insertions, 58 deletions
diff --git a/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateDateTimeFormatData.cpp b/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateDateTimeFormatData.cpp index 83d5655e31..a178176a3d 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateDateTimeFormatData.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateDateTimeFormatData.cpp @@ -2093,7 +2093,7 @@ Optional<@return_type@> get_regional_@lookup_type@(StringView region) append_regional_lookup("Weekday"sv, "weekend_end"sv); generator.append(R"~~~( -static CalendarData const* find_calendar_data(StringView locale, StringView calendar) +static ErrorOr<CalendarData const*> find_calendar_data(StringView locale, StringView calendar) { auto locale_value = locale_from_string(locale); if (!locale_value.has_value()) @@ -2120,7 +2120,7 @@ static CalendarData const* find_calendar_data(StringView locale, StringView cale if (auto const* calendar_data = lookup_calendar(calendar)) return calendar_data; - auto default_calendar = get_preferred_keyword_value_for_locale(locale, "ca"sv); + auto default_calendar = TRY(get_preferred_keyword_value_for_locale(locale, "ca"sv)); if (!default_calendar.has_value()) return nullptr; @@ -2129,7 +2129,7 @@ static CalendarData const* find_calendar_data(StringView locale, StringView cale ErrorOr<Optional<CalendarFormat>> get_calendar_date_format(StringView locale, StringView calendar) { - if (auto const* data = find_calendar_data(locale, calendar); data != nullptr) { + if (auto const* data = TRY(find_calendar_data(locale, calendar)); data != nullptr) { auto const& formats = s_calendar_formats.at(data->date_formats); return TRY(formats.to_unicode_calendar_format()); } @@ -2138,7 +2138,7 @@ ErrorOr<Optional<CalendarFormat>> get_calendar_date_format(StringView locale, St ErrorOr<Optional<CalendarFormat>> get_calendar_time_format(StringView locale, StringView calendar) { - if (auto const* data = find_calendar_data(locale, calendar); data != nullptr) { + if (auto const* data = TRY(find_calendar_data(locale, calendar)); data != nullptr) { auto const& formats = s_calendar_formats.at(data->time_formats); return TRY(formats.to_unicode_calendar_format()); } @@ -2147,7 +2147,7 @@ ErrorOr<Optional<CalendarFormat>> get_calendar_time_format(StringView locale, St ErrorOr<Optional<CalendarFormat>> get_calendar_date_time_format(StringView locale, StringView calendar) { - if (auto const* data = find_calendar_data(locale, calendar); data != nullptr) { + if (auto const* data = TRY(find_calendar_data(locale, calendar)); data != nullptr) { auto const& formats = s_calendar_formats.at(data->date_time_formats); return TRY(formats.to_unicode_calendar_format()); } @@ -2158,7 +2158,7 @@ ErrorOr<Vector<CalendarPattern>> get_calendar_available_formats(StringView local { Vector<CalendarPattern> result {}; - if (auto const* data = find_calendar_data(locale, calendar); data != nullptr) { + if (auto const* data = TRY(find_calendar_data(locale, calendar)); data != nullptr) { auto const& available_formats = s_calendar_pattern_lists.at(data->available_formats); TRY(result.try_ensure_capacity(available_formats.size())); @@ -2171,7 +2171,7 @@ ErrorOr<Vector<CalendarPattern>> get_calendar_available_formats(StringView local ErrorOr<Optional<CalendarRangePattern>> get_calendar_default_range_format(StringView locale, StringView calendar) { - if (auto const* data = find_calendar_data(locale, calendar); data != nullptr) { + if (auto const* data = TRY(find_calendar_data(locale, calendar)); data != nullptr) { auto const& pattern = s_calendar_range_patterns[data->default_range_format]; return TRY(pattern.to_unicode_calendar_range_pattern()); } @@ -2183,7 +2183,7 @@ ErrorOr<Vector<CalendarRangePattern>> get_calendar_range_formats(StringView loca { Vector<CalendarRangePattern> result {}; - if (auto const* data = find_calendar_data(locale, calendar); data != nullptr) { + if (auto const* data = TRY(find_calendar_data(locale, calendar)); data != nullptr) { auto const& range_formats = s_calendar_range_pattern_lists.at(data->range_formats); for (auto format : range_formats) { @@ -2201,7 +2201,7 @@ ErrorOr<Vector<CalendarRangePattern>> get_calendar_range12_formats(StringView lo { Vector<CalendarRangePattern> result {}; - if (auto const* data = find_calendar_data(locale, calendar); data != nullptr) { + if (auto const* data = TRY(find_calendar_data(locale, calendar)); data != nullptr) { auto const& range12_formats = s_calendar_range_pattern_lists.at(data->range12_formats); for (auto format : range12_formats) { @@ -2215,9 +2215,9 @@ ErrorOr<Vector<CalendarRangePattern>> get_calendar_range12_formats(StringView lo return result; } -static Span<@string_index_type@ const> find_calendar_symbols(StringView locale, StringView calendar, CalendarSymbol symbol, CalendarPatternStyle style) +static ErrorOr<Span<@string_index_type@ const>> find_calendar_symbols(StringView locale, StringView calendar, CalendarSymbol symbol, CalendarPatternStyle style) { - if (auto const* data = find_calendar_data(locale, calendar); data != nullptr) { + if (auto const* data = TRY(find_calendar_data(locale, calendar)); data != nullptr) { auto const& symbols_list = s_calendar_symbol_lists[data->symbols]; auto symbol_index = to_underlying(symbol); @@ -2243,62 +2243,62 @@ static Span<@string_index_type@ const> find_calendar_symbols(StringView locale, return s_symbol_lists.at(symbol_list_index); } - return {}; + return Span<@string_index_type@ const> {}; } -Optional<StringView> get_calendar_era_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Era value) +ErrorOr<Optional<StringView>> get_calendar_era_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Era value) { - auto symbols = find_calendar_symbols(locale, calendar, CalendarSymbol::Era, style); + auto symbols = TRY(find_calendar_symbols(locale, calendar, CalendarSymbol::Era, style)); if (auto value_index = to_underlying(value); value_index < symbols.size()) { if (auto symbol_index = symbols.at(value_index); symbol_index != 0) - return decode_string(symbol_index); + return Optional<StringView> { decode_string(symbol_index) }; } - return {}; + return OptionalNone {}; } -Optional<StringView> get_calendar_month_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Month value) +ErrorOr<Optional<StringView>> get_calendar_month_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Month value) { - auto symbols = find_calendar_symbols(locale, calendar, CalendarSymbol::Month, style); + auto symbols = TRY(find_calendar_symbols(locale, calendar, CalendarSymbol::Month, style)); if (auto value_index = to_underlying(value); value_index < symbols.size()) { if (auto symbol_index = symbols.at(value_index); symbol_index != 0) - return decode_string(symbol_index); + return Optional<StringView> { decode_string(symbol_index) }; } - return {}; + return OptionalNone {}; } -Optional<StringView> get_calendar_weekday_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Weekday value) +ErrorOr<Optional<StringView>> get_calendar_weekday_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Weekday value) { - auto symbols = find_calendar_symbols(locale, calendar, CalendarSymbol::Weekday, style); + auto symbols = TRY(find_calendar_symbols(locale, calendar, CalendarSymbol::Weekday, style)); if (auto value_index = to_underlying(value); value_index < symbols.size()) { if (auto symbol_index = symbols.at(value_index); symbol_index != 0) - return decode_string(symbol_index); + return Optional<StringView> { decode_string(symbol_index) }; } - return {}; + return OptionalNone {}; } -Optional<StringView> get_calendar_day_period_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, DayPeriod value) +ErrorOr<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); + auto symbols = TRY(find_calendar_symbols(locale, calendar, CalendarSymbol::DayPeriod, style)); if (auto value_index = to_underlying(value); value_index < symbols.size()) { if (auto symbol_index = symbols.at(value_index); symbol_index != 0) - return decode_string(symbol_index); + return Optional<StringView> { decode_string(symbol_index) }; } - return {}; + return OptionalNone {}; } -Optional<StringView> get_calendar_day_period_symbol_for_hour(StringView locale, StringView calendar, CalendarPatternStyle style, u8 hour) +ErrorOr<Optional<StringView>> get_calendar_day_period_symbol_for_hour(StringView locale, StringView calendar, CalendarPatternStyle style, u8 hour) { auto locale_value = locale_from_string(locale); if (!locale_value.has_value()) - return {}; + return OptionalNone {}; auto locale_index = to_underlying(*locale_value) - 1; // Subtract 1 because 0 == Locale::None. diff --git a/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateLocaleData.cpp b/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateLocaleData.cpp index 6ef5fee618..59d4bef2fc 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateLocaleData.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateLocaleData.cpp @@ -1554,45 +1554,45 @@ static Span<@string_index_type@ const> find_keyword_indices(StringView locale, S return s_keyword_lists.at(keywords_index); } -Optional<StringView> get_preferred_keyword_value_for_locale(StringView locale, StringView key) +ErrorOr<Optional<StringView>> get_preferred_keyword_value_for_locale(StringView locale, StringView key) { // Hour cycle keywords are region-based rather than locale-based, so they need to be handled specially. // FIXME: Calendar keywords are also region-based, and will need to be handled here when we support non-Gregorian calendars: // https://github.com/unicode-org/cldr-json/blob/main/cldr-json/cldr-core/supplemental/calendarPreferenceData.json if (key == "hc"sv) { - auto hour_cycles = get_locale_hour_cycles(locale); + auto hour_cycles = TRY(get_locale_hour_cycles(locale)); if (hour_cycles.is_empty()) - return {}; + return OptionalNone {}; - return hour_cycle_to_string(hour_cycles[0]); + return Optional<StringView> { hour_cycle_to_string(hour_cycles[0]) }; } // FIXME: Generate locale-preferred collation data when available in the CLDR. if (key == "co"sv) { auto collations = get_available_collation_types(); if (collations.is_empty()) - return {}; + return OptionalNone {}; - return collations[0]; + return Optional<StringView> { collations[0] }; } auto keyword_indices = find_keyword_indices(locale, key); if (keyword_indices.is_empty()) - return {}; + return OptionalNone {}; - return decode_string(keyword_indices[0]); + return Optional<StringView> { decode_string(keyword_indices[0]) }; } -Vector<StringView> get_keywords_for_locale(StringView locale, StringView key) +ErrorOr<Vector<StringView>> get_keywords_for_locale(StringView locale, StringView key) { // Hour cycle keywords are region-based rather than locale-based, so they need to be handled specially. // FIXME: Calendar keywords are also region-based, and will need to be handled here when we support non-Gregorian calendars: // https://github.com/unicode-org/cldr-json/blob/main/cldr-json/cldr-core/supplemental/calendarPreferenceData.json if (key == "hc"sv) { - auto hour_cycles = get_locale_hour_cycles(locale); + auto hour_cycles = TRY(get_locale_hour_cycles(locale)); Vector<StringView> values; - values.ensure_capacity(hour_cycles.size()); + TRY(values.try_ensure_capacity(hour_cycles.size())); for (auto hour_cycle : hour_cycles) values.unchecked_append(hour_cycle_to_string(hour_cycle)); @@ -1607,7 +1607,7 @@ Vector<StringView> get_keywords_for_locale(StringView locale, StringView key) auto keyword_indices = find_keyword_indices(locale, key); Vector<StringView> keywords; - keywords.ensure_capacity(keyword_indices.size()); + TRY(keywords.try_ensure_capacity(keyword_indices.size())); for (auto keyword : keyword_indices) keywords.unchecked_append(decode_string(keyword)); diff --git a/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateNumberFormatData.cpp b/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateNumberFormatData.cpp index 5fe97008b8..1d34ee26e4 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateNumberFormatData.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateNumberFormatData.cpp @@ -929,7 +929,7 @@ Optional<Span<u32 const>> get_digits_for_number_system(StringView system) return s_number_systems_digits[number_system_index]; } -static NumberSystemData const* find_number_system(StringView locale, StringView system) +static ErrorOr<NumberSystemData const*> find_number_system(StringView locale, StringView system) { auto locale_value = locale_from_string(locale); if (!locale_value.has_value()) @@ -959,44 +959,44 @@ static NumberSystemData const* find_number_system(StringView locale, StringView if (auto const* number_system = lookup_number_system(system)) return number_system; - auto default_number_system = get_preferred_keyword_value_for_locale(locale, "nu"sv); + auto default_number_system = TRY(get_preferred_keyword_value_for_locale(locale, "nu"sv)); if (!default_number_system.has_value()) return nullptr; return lookup_number_system(*default_number_system); } -Optional<StringView> get_number_system_symbol(StringView locale, StringView system, NumericSymbol symbol) +ErrorOr<Optional<StringView>> get_number_system_symbol(StringView locale, StringView system, NumericSymbol symbol) { - if (auto const* number_system = find_number_system(locale, system); number_system != nullptr) { + if (auto const* number_system = TRY(find_number_system(locale, system)); number_system != nullptr) { auto symbols = s_numeric_symbol_lists.at(number_system->symbols); auto symbol_index = to_underlying(symbol); if (symbol_index >= symbols.size()) - return {}; + return OptionalNone {}; - return decode_string(symbols[symbol_index]); + return Optional<StringView> { decode_string(symbols[symbol_index]) }; } - return {}; + return OptionalNone {}; } -Optional<NumberGroupings> get_number_system_groupings(StringView locale, StringView system) +ErrorOr<Optional<NumberGroupings>> get_number_system_groupings(StringView locale, StringView system) { auto locale_value = locale_from_string(locale); if (!locale_value.has_value()) - return {}; + return OptionalNone {}; u8 minimum_grouping_digits = s_minimum_grouping_digits[to_underlying(*locale_value) - 1]; - if (auto const* number_system = find_number_system(locale, system); number_system != nullptr) + if (auto const* number_system = TRY(find_number_system(locale, system)); number_system != nullptr) return NumberGroupings { minimum_grouping_digits, number_system->primary_grouping_size, number_system->secondary_grouping_size }; - return {}; + return OptionalNone {}; } -Optional<NumberFormat> get_standard_number_system_format(StringView locale, StringView system, StandardNumberFormatType type) +ErrorOr<Optional<NumberFormat>> get_standard_number_system_format(StringView locale, StringView system, StandardNumberFormatType type) { - if (auto const* number_system = find_number_system(locale, system); number_system != nullptr) { + if (auto const* number_system = TRY(find_number_system(locale, system)); number_system != nullptr) { @number_format_index_type@ format_index = 0; switch (type) { @@ -1020,14 +1020,14 @@ Optional<NumberFormat> get_standard_number_system_format(StringView locale, Stri return s_number_formats[format_index].to_unicode_number_format(); } - return {}; + return OptionalNone {}; } -Vector<NumberFormat> get_compact_number_system_formats(StringView locale, StringView system, CompactNumberFormatType type) +ErrorOr<Vector<NumberFormat>> get_compact_number_system_formats(StringView locale, StringView system, CompactNumberFormatType type) { Vector<NumberFormat> formats; - if (auto const* number_system = find_number_system(locale, system); number_system != nullptr) { + if (auto const* number_system = TRY(find_number_system(locale, system)); number_system != nullptr) { @number_format_list_index_type@ number_format_list_index { 0 }; switch (type) { |