1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:47:45 +00:00

LibLocale+LibJS: Make number format APIs infallible

These APIs only perform small allocations, and are only used by LibJS.
Callers which could only have failed from these APIs are also made to
be infallible here.
This commit is contained in:
Timothy Flynn 2023-08-22 16:12:51 -04:00 committed by Andreas Kling
parent cd526813e6
commit 0914e86691
6 changed files with 56 additions and 57 deletions

View file

@ -18,9 +18,9 @@ namespace Locale {
Optional<StringView> __attribute__((weak)) get_number_system_symbol(StringView, StringView, NumericSymbol) { return {}; }
Optional<NumberGroupings> __attribute__((weak)) get_number_system_groupings(StringView, StringView) { return {}; }
ErrorOr<Optional<NumberFormat>> __attribute__((weak)) get_standard_number_system_format(StringView, StringView, StandardNumberFormatType) { return OptionalNone {}; }
ErrorOr<Vector<NumberFormat>> __attribute__((weak)) get_compact_number_system_formats(StringView, StringView, CompactNumberFormatType) { return Vector<NumberFormat> {}; }
ErrorOr<Vector<NumberFormat>> __attribute__((weak)) get_unit_formats(StringView, StringView, Style) { return Vector<NumberFormat> {}; }
Optional<NumberFormat> __attribute__((weak)) get_standard_number_system_format(StringView, StringView, StandardNumberFormatType) { return {}; }
Vector<NumberFormat> __attribute__((weak)) get_compact_number_system_formats(StringView, StringView, CompactNumberFormatType) { return {}; }
Vector<NumberFormat> __attribute__((weak)) get_unit_formats(StringView, StringView, Style) { return {}; }
Optional<ReadonlySpan<u32>> __attribute__((weak)) get_digits_for_number_system(StringView)
{
@ -29,7 +29,7 @@ Optional<ReadonlySpan<u32>> __attribute__((weak)) get_digits_for_number_system(S
return digits.span();
}
ErrorOr<String> replace_digits_for_number_system(StringView system, StringView number)
String replace_digits_for_number_system(StringView system, StringView number)
{
auto digits = get_digits_for_number_system(system);
if (!digits.has_value())
@ -41,13 +41,13 @@ ErrorOr<String> replace_digits_for_number_system(StringView system, StringView n
for (auto ch : number) {
if (is_ascii_digit(ch)) {
u32 digit = digits->at(parse_ascii_digit(ch));
TRY(builder.try_append_code_point(digit));
builder.append_code_point(digit);
} else {
TRY(builder.try_append(ch));
builder.append(ch);
}
}
return builder.to_string();
return MUST(builder.to_string());
}
#if ENABLE_UNICODE_DATA
@ -64,7 +64,7 @@ static u32 last_code_point(StringView string)
#endif
// https://www.unicode.org/reports/tr35/tr35-numbers.html#Currencies
ErrorOr<Optional<String>> augment_currency_format_pattern([[maybe_unused]] StringView currency_display, [[maybe_unused]] StringView base_pattern)
Optional<String> augment_currency_format_pattern([[maybe_unused]] StringView currency_display, [[maybe_unused]] StringView base_pattern)
{
#if ENABLE_UNICODE_DATA
constexpr auto number_key = "{number}"sv;
@ -87,7 +87,7 @@ ErrorOr<Optional<String>> augment_currency_format_pattern([[maybe_unused]] Strin
u32 first_currency_code_point = *utf8_currency_display.begin();
if (!Unicode::code_point_has_general_category(first_currency_code_point, Unicode::GeneralCategory::Symbol))
currency_key_with_spacing = TRY(String::formatted("{}{}", spacing, currency_key));
currency_key_with_spacing = MUST(String::formatted("{}{}", spacing, currency_key));
}
} else {
u32 last_pattern_code_point = last_code_point(base_pattern.substring_view(0, *number_index));
@ -96,23 +96,23 @@ ErrorOr<Optional<String>> augment_currency_format_pattern([[maybe_unused]] Strin
u32 last_currency_code_point = last_code_point(currency_display);
if (!Unicode::code_point_has_general_category(last_currency_code_point, Unicode::GeneralCategory::Symbol))
currency_key_with_spacing = TRY(String::formatted("{}{}", currency_key, spacing));
currency_key_with_spacing = MUST(String::formatted("{}{}", currency_key, spacing));
}
}
if (currency_key_with_spacing.has_value())
return TRY(TRY(String::from_utf8(base_pattern)).replace(currency_key, *currency_key_with_spacing, ReplaceMode::FirstOnly));
return MUST(MUST(String::from_utf8(base_pattern)).replace(currency_key, *currency_key_with_spacing, ReplaceMode::FirstOnly));
#endif
return OptionalNone {};
return {};
}
// https://unicode.org/reports/tr35/tr35-numbers.html#83-range-pattern-processing
ErrorOr<Optional<String>> augment_range_pattern([[maybe_unused]] StringView range_separator, [[maybe_unused]] StringView lower, [[maybe_unused]] StringView upper)
Optional<String> augment_range_pattern([[maybe_unused]] StringView range_separator, [[maybe_unused]] StringView lower, [[maybe_unused]] StringView upper)
{
#if ENABLE_UNICODE_DATA
auto range_pattern_with_spacing = [&]() {
return String::formatted(" {} ", range_separator);
return MUST(String::formatted(" {} ", range_separator));
};
Utf8View utf8_range_separator { range_separator };
@ -124,7 +124,7 @@ ErrorOr<Optional<String>> augment_range_pattern([[maybe_unused]] StringView rang
// 2. If the range pattern does not contain a character having the White_Space binary Unicode property after the {0} or before the {1} placeholders.
for (auto it = utf8_range_separator.begin(); it != utf8_range_separator.end(); ++it) {
if (Unicode::code_point_has_property(*it, Unicode::Property::White_Space))
return OptionalNone {};
return {};
}
// 1. If the lower string ends with a character other than a digit, or if the upper string begins with a character other than a digit.
@ -137,7 +137,7 @@ ErrorOr<Optional<String>> augment_range_pattern([[maybe_unused]] StringView rang
return range_pattern_with_spacing();
#endif
return OptionalNone {};
return {};
}
}