From 89da8de4cab589885fcaf8940b6e9022c637e24c Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 2 Feb 2023 19:54:47 -0500 Subject: [PATCH] LibJS+LibLocale: Propagate OOM from CLDR NumberFormat Vector operations --- .../LibLocale/GenerateNumberFormatData.cpp | 20 +++++++++---------- .../LibJS/Runtime/Intl/NumberFormat.cpp | 2 +- Userland/Libraries/LibLocale/NumberFormat.cpp | 2 +- Userland/Libraries/LibLocale/NumberFormat.h | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateNumberFormatData.cpp b/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateNumberFormatData.cpp index 082a894c90..1ecd5b9521 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateNumberFormatData.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateNumberFormatData.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022, Tim Flynn + * Copyright (c) 2021-2023, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ @@ -798,7 +798,7 @@ namespace Locale { generator.append(R"~~~( struct NumberFormatImpl { - NumberFormat to_unicode_number_format() const { + ErrorOr to_unicode_number_format() const { NumberFormat number_format {}; number_format.magnitude = magnitude; @@ -808,9 +808,9 @@ struct NumberFormatImpl { number_format.positive_format = decode_string(positive_format); number_format.negative_format = decode_string(negative_format); - number_format.identifiers.ensure_capacity(identifiers.size()); + TRY(number_format.identifiers.try_ensure_capacity(identifiers.size())); for (@string_index_type@ identifier : identifiers) - number_format.identifiers.append(decode_string(identifier)); + number_format.identifiers.unchecked_append(decode_string(identifier)); return number_format; } @@ -1017,7 +1017,7 @@ ErrorOr> get_standard_number_system_format(StringView loc break; } - return s_number_formats[format_index].to_unicode_number_format(); + return TRY(s_number_formats[format_index].to_unicode_number_format()); } return OptionalNone {}; @@ -1046,10 +1046,10 @@ ErrorOr> get_compact_number_system_formats(StringView local } auto number_formats = s_number_format_lists.at(number_format_list_index); - formats.ensure_capacity(number_formats.size()); + TRY(formats.try_ensure_capacity(number_formats.size())); for (auto number_format : number_formats) - formats.append(s_number_formats[number_format].to_unicode_number_format()); + formats.unchecked_append(TRY(s_number_formats[number_format].to_unicode_number_format())); } return formats; @@ -1074,7 +1074,7 @@ static Unit const* find_units(StringView locale, StringView unit) return nullptr; } -Vector get_unit_formats(StringView locale, StringView unit, Style style) +ErrorOr> get_unit_formats(StringView locale, StringView unit, Style style) { Vector formats; @@ -1096,10 +1096,10 @@ Vector get_unit_formats(StringView locale, StringView unit, Style } auto number_formats = s_number_format_lists.at(number_format_list_index); - formats.ensure_capacity(number_formats.size()); + TRY(formats.try_ensure_capacity(number_formats.size())); for (auto number_format : number_formats) - formats.append(s_number_formats[number_format].to_unicode_number_format()); + formats.unchecked_append(TRY(s_number_formats[number_format].to_unicode_number_format())); } return formats; diff --git a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp index 0ab18a721a..137f214454 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp @@ -1317,7 +1317,7 @@ ThrowCompletionOr>> get_number_format_patte // i. Let unit be "fallback". // e. Let patterns be patterns.[[]]. // f. Let patterns be patterns.[[]]. - auto formats = ::Locale::get_unit_formats(number_format.data_locale(), number_format.unit(), number_format.unit_display()); + auto formats = TRY_OR_THROW_OOM(vm, ::Locale::get_unit_formats(number_format.data_locale(), number_format.unit(), number_format.unit_display())); auto plurality = MUST_OR_THROW_OOM(resolve_plural(vm, number_format, ::Locale::PluralForm::Cardinal, number.to_value(vm))); if (auto it = formats.find_if([&](auto& p) { return p.plurality == plurality.plural_category; }); it != formats.end()) diff --git a/Userland/Libraries/LibLocale/NumberFormat.cpp b/Userland/Libraries/LibLocale/NumberFormat.cpp index 812137541a..5de4905020 100644 --- a/Userland/Libraries/LibLocale/NumberFormat.cpp +++ b/Userland/Libraries/LibLocale/NumberFormat.cpp @@ -20,7 +20,7 @@ ErrorOr> __attribute__((weak)) get_number_system_symbol(Str ErrorOr> __attribute__((weak)) get_number_system_groupings(StringView, StringView) { return OptionalNone {}; } ErrorOr> __attribute__((weak)) get_standard_number_system_format(StringView, StringView, StandardNumberFormatType) { return OptionalNone {}; } ErrorOr> __attribute__((weak)) get_compact_number_system_formats(StringView, StringView, CompactNumberFormatType) { return Vector {}; } -Vector __attribute__((weak)) get_unit_formats(StringView, StringView, Style) { return {}; } +ErrorOr> __attribute__((weak)) get_unit_formats(StringView, StringView, Style) { return Vector {}; } Optional> __attribute__((weak)) get_digits_for_number_system(StringView) { diff --git a/Userland/Libraries/LibLocale/NumberFormat.h b/Userland/Libraries/LibLocale/NumberFormat.h index 566af5500f..2c40532dfc 100644 --- a/Userland/Libraries/LibLocale/NumberFormat.h +++ b/Userland/Libraries/LibLocale/NumberFormat.h @@ -69,7 +69,7 @@ ErrorOr replace_digits_for_number_system(StringView system, StringView n ErrorOr> get_standard_number_system_format(StringView locale, StringView system, StandardNumberFormatType type); ErrorOr> get_compact_number_system_formats(StringView locale, StringView system, CompactNumberFormatType type); -Vector get_unit_formats(StringView locale, StringView unit, Style style); +ErrorOr> get_unit_formats(StringView locale, StringView unit, Style style); ErrorOr> augment_currency_format_pattern(StringView currency_display, StringView base_pattern); ErrorOr> augment_range_pattern(StringView range_separator, StringView lower, StringView upper);