summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2023-02-02 21:34:01 -0500
committerLinus Groh <mail@linusgroh.de>2023-02-08 18:32:37 +0000
commit340434ce0967f0d38540c6d4eaa6fe0eb548415c (patch)
tree0b3e6a998b9b515a88ee45b26c655f0730a4dcd1
parent89da8de4cab589885fcaf8940b6e9022c637e24c (diff)
downloadserenity-340434ce0967f0d38540c6d4eaa6fe0eb548415c.zip
LibLocale: Propagate OOM from CLDR DateTime Vector and String operations
-rw-r--r--Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateDateTimeFormatData.cpp8
-rw-r--r--Userland/Libraries/LibLocale/DateTimeFormat.cpp22
-rw-r--r--Userland/Libraries/LibLocale/DateTimeFormat.h2
3 files changed, 19 insertions, 13 deletions
diff --git a/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateDateTimeFormatData.cpp b/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateDateTimeFormatData.cpp
index a45b370506..c9da83a0d4 100644
--- a/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateDateTimeFormatData.cpp
+++ b/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateDateTimeFormatData.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2021-2022, Tim Flynn <trflynn89@serenityos.org>
+ * Copyright (c) 2021-2023, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -2047,11 +2047,11 @@ static Optional<Calendar> keyword_to_calendar(KeywordCalendar keyword)
}
}
-Vector<HourCycle> get_regional_hour_cycles(StringView region)
+ErrorOr<Vector<HourCycle>> get_regional_hour_cycles(StringView region)
{
auto region_value = hour_cycle_region_from_string(region);
if (!region_value.has_value())
- return {};
+ return Vector<HourCycle> {};
auto region_index = to_underlying(*region_value);
@@ -2059,7 +2059,7 @@ Vector<HourCycle> get_regional_hour_cycles(StringView region)
auto const& regional_hour_cycles = s_hour_cycle_lists.at(regional_hour_cycles_index);
Vector<HourCycle> hour_cycles;
- hour_cycles.ensure_capacity(regional_hour_cycles.size());
+ TRY(hour_cycles.try_ensure_capacity(regional_hour_cycles.size()));
for (auto hour_cycle : regional_hour_cycles)
hour_cycles.unchecked_append(static_cast<HourCycle>(hour_cycle));
diff --git a/Userland/Libraries/LibLocale/DateTimeFormat.cpp b/Userland/Libraries/LibLocale/DateTimeFormat.cpp
index 5aea11725d..6c9ba3f2aa 100644
--- a/Userland/Libraries/LibLocale/DateTimeFormat.cpp
+++ b/Userland/Libraries/LibLocale/DateTimeFormat.cpp
@@ -92,9 +92,9 @@ StringView calendar_pattern_style_to_string(CalendarPatternStyle style)
}
Optional<HourCycleRegion> __attribute__((weak)) hour_cycle_region_from_string(StringView) { return {}; }
-Vector<HourCycle> __attribute__((weak)) get_regional_hour_cycles(StringView) { return {}; }
+ErrorOr<Vector<HourCycle>> __attribute__((weak)) get_regional_hour_cycles(StringView) { return Vector<HourCycle> {}; }
-template<typename T, typename GetRegionalValues>
+template<typename T, FallibleFunction<StringView> GetRegionalValues>
static ErrorOr<T> find_regional_values_for_locale(StringView locale, GetRegionalValues&& get_regional_values)
{
auto has_value = [](auto const& container) {
@@ -104,7 +104,7 @@ static ErrorOr<T> find_regional_values_for_locale(StringView locale, GetRegional
return !container.is_empty();
};
- if (auto regional_values = get_regional_values(locale); has_value(regional_values))
+ if (auto regional_values = TRY(get_regional_values(locale)); has_value(regional_values))
return regional_values;
auto return_default_values = [&]() { return get_regional_values("001"sv); };
@@ -118,12 +118,18 @@ static ErrorOr<T> find_regional_values_for_locale(StringView locale, GetRegional
if (!language.has_value() || !language->region.has_value())
return return_default_values();
- if (auto regional_values = get_regional_values(*language->region); has_value(regional_values))
+ if (auto regional_values = TRY(get_regional_values(*language->region)); has_value(regional_values))
return regional_values;
return return_default_values();
}
+template<typename T, typename GetRegionalValues>
+static ErrorOr<T> find_regional_values_for_locale(StringView locale, GetRegionalValues&& get_regional_values)
+{
+ return find_regional_values_for_locale<T>(locale, [&](auto region) -> ErrorOr<T> { return get_regional_values(region); });
+}
+
// https://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
ErrorOr<Vector<HourCycle>> get_locale_hour_cycles(StringView locale)
{
@@ -187,22 +193,22 @@ ErrorOr<String> combine_skeletons(StringView first, StringView second)
StringBuilder builder;
- auto append_from_skeleton = [&](auto skeleton, auto ch) {
+ auto append_from_skeleton = [&](auto skeleton, auto ch) -> ErrorOr<bool> {
auto first_index = skeleton.find(ch);
if (!first_index.has_value())
return false;
auto last_index = skeleton.find_last(ch);
- builder.append(skeleton.substring_view(*first_index, *last_index - *first_index + 1));
+ TRY(builder.try_append(skeleton.substring_view(*first_index, *last_index - *first_index + 1)));
return true;
};
for (auto fields : field_order) {
for (auto ch : fields) {
- if (append_from_skeleton(first, ch))
+ if (TRY(append_from_skeleton(first, ch)))
break;
- if (append_from_skeleton(second, ch))
+ if (TRY(append_from_skeleton(second, ch)))
break;
}
}
diff --git a/Userland/Libraries/LibLocale/DateTimeFormat.h b/Userland/Libraries/LibLocale/DateTimeFormat.h
index 63198c01fb..194eb3f550 100644
--- a/Userland/Libraries/LibLocale/DateTimeFormat.h
+++ b/Userland/Libraries/LibLocale/DateTimeFormat.h
@@ -189,7 +189,7 @@ CalendarPatternStyle calendar_pattern_style_from_string(StringView style);
StringView calendar_pattern_style_to_string(CalendarPatternStyle style);
Optional<HourCycleRegion> hour_cycle_region_from_string(StringView hour_cycle_region);
-Vector<HourCycle> get_regional_hour_cycles(StringView region);
+ErrorOr<Vector<HourCycle>> get_regional_hour_cycles(StringView region);
ErrorOr<Vector<HourCycle>> get_locale_hour_cycles(StringView locale);
ErrorOr<Optional<HourCycle>> get_default_regional_hour_cycle(StringView locale);