1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:07:34 +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

@ -796,7 +796,7 @@ namespace Locale {
generator.append(R"~~~( generator.append(R"~~~(
struct NumberFormatImpl { struct NumberFormatImpl {
ErrorOr<NumberFormat> to_unicode_number_format() const { NumberFormat to_unicode_number_format() const {
NumberFormat number_format {}; NumberFormat number_format {};
number_format.magnitude = magnitude; number_format.magnitude = magnitude;
@ -806,7 +806,7 @@ struct NumberFormatImpl {
number_format.positive_format = decode_string(positive_format); number_format.positive_format = decode_string(positive_format);
number_format.negative_format = decode_string(negative_format); number_format.negative_format = decode_string(negative_format);
TRY(number_format.identifiers.try_ensure_capacity(identifiers.size())); number_format.identifiers.ensure_capacity(identifiers.size());
for (@string_index_type@ identifier : identifiers) for (@string_index_type@ identifier : identifiers)
number_format.identifiers.unchecked_append(decode_string(identifier)); number_format.identifiers.unchecked_append(decode_string(identifier));
@ -992,7 +992,7 @@ Optional<NumberGroupings> get_number_system_groupings(StringView locale, StringV
return {}; return {};
} }
ErrorOr<Optional<NumberFormat>> get_standard_number_system_format(StringView locale, StringView system, StandardNumberFormatType type) 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 = find_number_system(locale, system); number_system != nullptr) {
@number_format_index_type@ format_index = 0; @number_format_index_type@ format_index = 0;
@ -1015,13 +1015,13 @@ ErrorOr<Optional<NumberFormat>> get_standard_number_system_format(StringView loc
break; break;
} }
return TRY(s_number_formats[format_index].to_unicode_number_format()); return s_number_formats[format_index].to_unicode_number_format();
} }
return OptionalNone {}; return {};
} }
ErrorOr<Vector<NumberFormat>> get_compact_number_system_formats(StringView locale, StringView system, CompactNumberFormatType type) Vector<NumberFormat> get_compact_number_system_formats(StringView locale, StringView system, CompactNumberFormatType type)
{ {
Vector<NumberFormat> formats; Vector<NumberFormat> formats;
@ -1044,10 +1044,10 @@ ErrorOr<Vector<NumberFormat>> get_compact_number_system_formats(StringView local
} }
auto number_formats = s_number_format_lists.at(number_format_list_index); auto number_formats = s_number_format_lists.at(number_format_list_index);
TRY(formats.try_ensure_capacity(number_formats.size())); formats.ensure_capacity(number_formats.size());
for (auto number_format : number_formats) for (auto number_format : number_formats)
formats.unchecked_append(TRY(s_number_formats[number_format].to_unicode_number_format())); formats.unchecked_append(s_number_formats[number_format].to_unicode_number_format());
} }
return formats; return formats;
@ -1072,7 +1072,7 @@ static Unit const* find_units(StringView locale, StringView unit)
return nullptr; return nullptr;
} }
ErrorOr<Vector<NumberFormat>> get_unit_formats(StringView locale, StringView unit, Style style) Vector<NumberFormat> get_unit_formats(StringView locale, StringView unit, Style style)
{ {
Vector<NumberFormat> formats; Vector<NumberFormat> formats;
@ -1094,10 +1094,10 @@ ErrorOr<Vector<NumberFormat>> get_unit_formats(StringView locale, StringView uni
} }
auto number_formats = s_number_format_lists.at(number_format_list_index); auto number_formats = s_number_format_lists.at(number_format_list_index);
TRY(formats.try_ensure_capacity(number_formats.size())); formats.ensure_capacity(number_formats.size());
for (auto number_format : number_formats) for (auto number_format : number_formats)
formats.unchecked_append(TRY(s_number_formats[number_format].to_unicode_number_format())); formats.unchecked_append(s_number_formats[number_format].to_unicode_number_format());
} }
return formats; return formats;

View file

@ -761,7 +761,7 @@ ThrowCompletionOr<Vector<PatternPartition>> partition_notation_sub_pattern(VM& v
// 4. Else, // 4. Else,
else { else {
// a. Let notationSubPattern be GetNotationSubPattern(numberFormat, exponent). // a. Let notationSubPattern be GetNotationSubPattern(numberFormat, exponent).
auto notation_sub_pattern = MUST_OR_THROW_OOM(get_notation_sub_pattern(vm, number_format, exponent)); auto notation_sub_pattern = get_notation_sub_pattern(number_format, exponent);
if (!notation_sub_pattern.has_value()) if (!notation_sub_pattern.has_value())
return Vector<PatternPartition> {}; return Vector<PatternPartition> {};
@ -796,7 +796,7 @@ ThrowCompletionOr<Vector<PatternPartition>> partition_notation_sub_pattern(VM& v
// iv. Set position to position + 1. // iv. Set position to position + 1.
// g. Set n to transliterated. // g. Set n to transliterated.
// 2. Else use an implementation dependent algorithm to map n to the appropriate representation of n in the given numbering system. // 2. Else use an implementation dependent algorithm to map n to the appropriate representation of n in the given numbering system.
formatted_string = TRY_OR_THROW_OOM(vm, ::Locale::replace_digits_for_number_system(number_format.numbering_system(), formatted_string)); formatted_string = ::Locale::replace_digits_for_number_system(number_format.numbering_system(), formatted_string);
// 3. Let decimalSepIndex be StringIndexOf(n, ".", 0). // 3. Let decimalSepIndex be StringIndexOf(n, ".", 0).
auto decimal_sep_index = formatted_string.find_byte_offset('.'); auto decimal_sep_index = formatted_string.find_byte_offset('.');
@ -902,7 +902,7 @@ ThrowCompletionOr<Vector<PatternPartition>> partition_notation_sub_pattern(VM& v
// FIXME: The spec does not say to do this, but all of major engines perform this replacement. // FIXME: The spec does not say to do this, but all of major engines perform this replacement.
// Without this, formatting with non-Latin numbering systems will produce non-localized results. // Without this, formatting with non-Latin numbering systems will produce non-localized results.
exponent_result.formatted_string = TRY_OR_THROW_OOM(vm, ::Locale::replace_digits_for_number_system(number_format.numbering_system(), exponent_result.formatted_string)); exponent_result.formatted_string = ::Locale::replace_digits_for_number_system(number_format.numbering_system(), exponent_result.formatted_string);
// 3. Append a new Record { [[Type]]: "exponentInteger", [[Value]]: exponentResult.[[FormattedString]] } as the last element of result. // 3. Append a new Record { [[Type]]: "exponentInteger", [[Value]]: exponentResult.[[FormattedString]] } as the last element of result.
TRY_OR_THROW_OOM(vm, result.try_append({ "exponentInteger"sv, move(exponent_result.formatted_string) })); TRY_OR_THROW_OOM(vm, result.try_append({ "exponentInteger"sv, move(exponent_result.formatted_string) }));
@ -1315,7 +1315,7 @@ ThrowCompletionOr<Optional<Variant<StringView, String>>> get_number_format_patte
// 7. If style is "percent", then // 7. If style is "percent", then
case NumberFormat::Style::Percent: case NumberFormat::Style::Percent:
// a. Let patterns be patterns.[[percent]]. // a. Let patterns be patterns.[[percent]].
patterns = TRY_OR_THROW_OOM(vm, ::Locale::get_standard_number_system_format(number_format.data_locale(), number_format.numbering_system(), ::Locale::StandardNumberFormatType::Percent)); patterns = ::Locale::get_standard_number_system_format(number_format.data_locale(), number_format.numbering_system(), ::Locale::StandardNumberFormatType::Percent);
break; break;
// 8. Else if style is "unit", then // 8. Else if style is "unit", then
@ -1327,7 +1327,7 @@ ThrowCompletionOr<Optional<Variant<StringView, String>>> get_number_format_patte
// i. Let unit be "fallback". // i. Let unit be "fallback".
// e. Let patterns be patterns.[[<unit>]]. // e. Let patterns be patterns.[[<unit>]].
// f. Let patterns be patterns.[[<unitDisplay>]]. // f. Let patterns be patterns.[[<unitDisplay>]].
auto formats = TRY_OR_THROW_OOM(vm, ::Locale::get_unit_formats(number_format.data_locale(), number_format.unit(), number_format.unit_display())); auto formats = ::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))); 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()) if (auto it = formats.find_if([&](auto& p) { return p.plurality == plurality.plural_category; }); it != formats.end())
@ -1350,7 +1350,7 @@ ThrowCompletionOr<Optional<Variant<StringView, String>>> get_number_format_patte
// Handling of other [[CurrencyDisplay]] options will occur after [[SignDisplay]]. // Handling of other [[CurrencyDisplay]] options will occur after [[SignDisplay]].
if (number_format.currency_display() == NumberFormat::CurrencyDisplay::Name) { if (number_format.currency_display() == NumberFormat::CurrencyDisplay::Name) {
auto formats = TRY_OR_THROW_OOM(vm, ::Locale::get_compact_number_system_formats(number_format.data_locale(), number_format.numbering_system(), ::Locale::CompactNumberFormatType::CurrencyUnit)); auto formats = ::Locale::get_compact_number_system_formats(number_format.data_locale(), number_format.numbering_system(), ::Locale::CompactNumberFormatType::CurrencyUnit);
auto plurality = MUST_OR_THROW_OOM(resolve_plural(vm, number_format, ::Locale::PluralForm::Cardinal, number.to_value(vm))); 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()) { if (auto it = formats.find_if([&](auto& p) { return p.plurality == plurality.plural_category; }); it != formats.end()) {
@ -1361,10 +1361,10 @@ ThrowCompletionOr<Optional<Variant<StringView, String>>> get_number_format_patte
switch (number_format.currency_sign()) { switch (number_format.currency_sign()) {
case NumberFormat::CurrencySign::Standard: case NumberFormat::CurrencySign::Standard:
patterns = TRY_OR_THROW_OOM(vm, ::Locale::get_standard_number_system_format(number_format.data_locale(), number_format.numbering_system(), ::Locale::StandardNumberFormatType::Currency)); patterns = ::Locale::get_standard_number_system_format(number_format.data_locale(), number_format.numbering_system(), ::Locale::StandardNumberFormatType::Currency);
break; break;
case NumberFormat::CurrencySign::Accounting: case NumberFormat::CurrencySign::Accounting:
patterns = TRY_OR_THROW_OOM(vm, ::Locale::get_standard_number_system_format(number_format.data_locale(), number_format.numbering_system(), ::Locale::StandardNumberFormatType::Accounting)); patterns = ::Locale::get_standard_number_system_format(number_format.data_locale(), number_format.numbering_system(), ::Locale::StandardNumberFormatType::Accounting);
break; break;
} }
@ -1374,7 +1374,7 @@ ThrowCompletionOr<Optional<Variant<StringView, String>>> get_number_format_patte
case NumberFormat::Style::Decimal: case NumberFormat::Style::Decimal:
// a. Assert: style is "decimal". // a. Assert: style is "decimal".
// b. Let patterns be patterns.[[decimal]]. // b. Let patterns be patterns.[[decimal]].
patterns = TRY_OR_THROW_OOM(vm, ::Locale::get_standard_number_system_format(number_format.data_locale(), number_format.numbering_system(), ::Locale::StandardNumberFormatType::Decimal)); patterns = ::Locale::get_standard_number_system_format(number_format.data_locale(), number_format.numbering_system(), ::Locale::StandardNumberFormatType::Decimal);
break; break;
default: default:
@ -1510,7 +1510,7 @@ ThrowCompletionOr<Optional<Variant<StringView, String>>> get_number_format_patte
// we might need to mutate the format pattern to inject a space between the currency display and // we might need to mutate the format pattern to inject a space between the currency display and
// the currency number. // the currency number.
if (number_format.style() == NumberFormat::Style::Currency) { if (number_format.style() == NumberFormat::Style::Currency) {
auto modified_pattern = TRY_OR_THROW_OOM(vm, ::Locale::augment_currency_format_pattern(number_format.resolve_currency_display(), pattern)); auto modified_pattern = ::Locale::augment_currency_format_pattern(number_format.resolve_currency_display(), pattern);
if (modified_pattern.has_value()) if (modified_pattern.has_value())
return modified_pattern.release_value(); return modified_pattern.release_value();
} }
@ -1520,7 +1520,7 @@ ThrowCompletionOr<Optional<Variant<StringView, String>>> get_number_format_patte
} }
// 15.5.12 GetNotationSubPattern ( numberFormat, exponent ), https://tc39.es/ecma402/#sec-getnotationsubpattern // 15.5.12 GetNotationSubPattern ( numberFormat, exponent ), https://tc39.es/ecma402/#sec-getnotationsubpattern
ThrowCompletionOr<Optional<StringView>> get_notation_sub_pattern(VM& vm, NumberFormat& number_format, int exponent) Optional<StringView> get_notation_sub_pattern(NumberFormat& number_format, int exponent)
{ {
// 1. Let localeData be %NumberFormat%.[[LocaleData]]. // 1. Let localeData be %NumberFormat%.[[LocaleData]].
// 2. Let dataLocale be numberFormat.[[DataLocale]]. // 2. Let dataLocale be numberFormat.[[DataLocale]].
@ -1534,9 +1534,9 @@ ThrowCompletionOr<Optional<StringView>> get_notation_sub_pattern(VM& vm, NumberF
// 7. If notation is "scientific" or notation is "engineering", then // 7. If notation is "scientific" or notation is "engineering", then
if ((notation == NumberFormat::Notation::Scientific) || (notation == NumberFormat::Notation::Engineering)) { if ((notation == NumberFormat::Notation::Scientific) || (notation == NumberFormat::Notation::Engineering)) {
// a. Return notationSubPatterns.[[scientific]]. // a. Return notationSubPatterns.[[scientific]].
auto notation_sub_patterns = TRY_OR_THROW_OOM(vm, ::Locale::get_standard_number_system_format(number_format.data_locale(), number_format.numbering_system(), ::Locale::StandardNumberFormatType::Scientific)); auto notation_sub_patterns = ::Locale::get_standard_number_system_format(number_format.data_locale(), number_format.numbering_system(), ::Locale::StandardNumberFormatType::Scientific);
if (!notation_sub_patterns.has_value()) if (!notation_sub_patterns.has_value())
return OptionalNone {}; return {};
return notation_sub_patterns->zero_format; return notation_sub_patterns->zero_format;
} }
@ -1576,7 +1576,7 @@ ThrowCompletionOr<int> compute_exponent(VM& vm, NumberFormat& number_format, Mat
int magnitude = MUST_OR_THROW_OOM(number.logarithmic_floor(vm)); int magnitude = MUST_OR_THROW_OOM(number.logarithmic_floor(vm));
// 4. Let exponent be ComputeExponentForMagnitude(numberFormat, magnitude). // 4. Let exponent be ComputeExponentForMagnitude(numberFormat, magnitude).
int exponent = MUST_OR_THROW_OOM(compute_exponent_for_magnitude(vm, number_format, magnitude)); int exponent = compute_exponent_for_magnitude(number_format, magnitude);
// 5. Let x be x × 10^(-exponent). // 5. Let x be x × 10^(-exponent).
number = number.multiplied_by_power(-exponent); number = number.multiplied_by_power(-exponent);
@ -1600,11 +1600,11 @@ ThrowCompletionOr<int> compute_exponent(VM& vm, NumberFormat& number_format, Mat
} }
// 10. Return ComputeExponentForMagnitude(numberFormat, magnitude + 1). // 10. Return ComputeExponentForMagnitude(numberFormat, magnitude + 1).
return MUST_OR_THROW_OOM(compute_exponent_for_magnitude(vm, number_format, magnitude + 1)); return compute_exponent_for_magnitude(number_format, magnitude + 1);
} }
// 15.5.14 ComputeExponentForMagnitude ( numberFormat, magnitude ), https://tc39.es/ecma402/#sec-computeexponentformagnitude // 15.5.14 ComputeExponentForMagnitude ( numberFormat, magnitude ), https://tc39.es/ecma402/#sec-computeexponentformagnitude
ThrowCompletionOr<int> compute_exponent_for_magnitude(VM& vm, NumberFormat& number_format, int magnitude) int compute_exponent_for_magnitude(NumberFormat& number_format, int magnitude)
{ {
// 1. Let notation be numberFormat.[[Notation]]. // 1. Let notation be numberFormat.[[Notation]].
switch (number_format.notation()) { switch (number_format.notation()) {
@ -1637,11 +1637,11 @@ ThrowCompletionOr<int> compute_exponent_for_magnitude(VM& vm, NumberFormat& numb
Vector<::Locale::NumberFormat> format_rules; Vector<::Locale::NumberFormat> format_rules;
if (number_format.style() == NumberFormat::Style::Currency) if (number_format.style() == NumberFormat::Style::Currency)
format_rules = TRY_OR_THROW_OOM(vm, ::Locale::get_compact_number_system_formats(number_format.data_locale(), number_format.numbering_system(), ::Locale::CompactNumberFormatType::CurrencyShort)); format_rules = ::Locale::get_compact_number_system_formats(number_format.data_locale(), number_format.numbering_system(), ::Locale::CompactNumberFormatType::CurrencyShort);
else if (number_format.compact_display() == NumberFormat::CompactDisplay::Long) else if (number_format.compact_display() == NumberFormat::CompactDisplay::Long)
format_rules = TRY_OR_THROW_OOM(vm, ::Locale::get_compact_number_system_formats(number_format.data_locale(), number_format.numbering_system(), ::Locale::CompactNumberFormatType::DecimalLong)); format_rules = ::Locale::get_compact_number_system_formats(number_format.data_locale(), number_format.numbering_system(), ::Locale::CompactNumberFormatType::DecimalLong);
else else
format_rules = TRY_OR_THROW_OOM(vm, ::Locale::get_compact_number_system_formats(number_format.data_locale(), number_format.numbering_system(), ::Locale::CompactNumberFormatType::DecimalShort)); format_rules = ::Locale::get_compact_number_system_formats(number_format.data_locale(), number_format.numbering_system(), ::Locale::CompactNumberFormatType::DecimalShort);
::Locale::NumberFormat const* best_number_format = nullptr; ::Locale::NumberFormat const* best_number_format = nullptr;
@ -1855,7 +1855,7 @@ ThrowCompletionOr<Vector<PatternPartitionWithSource>> partition_number_range_pat
// 7. Let rangeSeparator be an ILND String value used to separate two numbers. // 7. Let rangeSeparator be an ILND String value used to separate two numbers.
auto range_separator_symbol = ::Locale::get_number_system_symbol(number_format.data_locale(), number_format.numbering_system(), ::Locale::NumericSymbol::RangeSeparator).value_or("-"sv); auto range_separator_symbol = ::Locale::get_number_system_symbol(number_format.data_locale(), number_format.numbering_system(), ::Locale::NumericSymbol::RangeSeparator).value_or("-"sv);
auto range_separator = TRY_OR_THROW_OOM(vm, ::Locale::augment_range_pattern(range_separator_symbol, result.last().value, end_result[0].value)); auto range_separator = ::Locale::augment_range_pattern(range_separator_symbol, result.last().value, end_result[0].value);
// 8. Append a new Record { [[Type]]: "literal", [[Value]]: rangeSeparator, [[Source]]: "shared" } element to result. // 8. Append a new Record { [[Type]]: "literal", [[Value]]: rangeSeparator, [[Source]]: "shared" } element to result.
PatternPartitionWithSource part; PatternPartitionWithSource part;

View file

@ -296,9 +296,9 @@ ThrowCompletionOr<Array*> format_numeric_to_parts(VM&, NumberFormat&, Mathematic
ThrowCompletionOr<RawFormatResult> to_raw_precision(VM&, MathematicalValue const& number, int min_precision, int max_precision, NumberFormat::UnsignedRoundingMode unsigned_rounding_mode); ThrowCompletionOr<RawFormatResult> to_raw_precision(VM&, MathematicalValue const& number, int min_precision, int max_precision, NumberFormat::UnsignedRoundingMode unsigned_rounding_mode);
ThrowCompletionOr<RawFormatResult> to_raw_fixed(VM&, MathematicalValue const& number, int min_fraction, int max_fraction, int rounding_increment, NumberFormat::UnsignedRoundingMode unsigned_rounding_mode); ThrowCompletionOr<RawFormatResult> to_raw_fixed(VM&, MathematicalValue const& number, int min_fraction, int max_fraction, int rounding_increment, NumberFormat::UnsignedRoundingMode unsigned_rounding_mode);
ThrowCompletionOr<Optional<Variant<StringView, String>>> get_number_format_pattern(VM&, NumberFormat&, MathematicalValue const& number, ::Locale::NumberFormat& found_pattern); ThrowCompletionOr<Optional<Variant<StringView, String>>> get_number_format_pattern(VM&, NumberFormat&, MathematicalValue const& number, ::Locale::NumberFormat& found_pattern);
ThrowCompletionOr<Optional<StringView>> get_notation_sub_pattern(VM&, NumberFormat&, int exponent); Optional<StringView> get_notation_sub_pattern(NumberFormat&, int exponent);
ThrowCompletionOr<int> compute_exponent(VM&, NumberFormat&, MathematicalValue number); ThrowCompletionOr<int> compute_exponent(VM&, NumberFormat&, MathematicalValue number);
ThrowCompletionOr<int> compute_exponent_for_magnitude(VM&, NumberFormat&, int magnitude); int compute_exponent_for_magnitude(NumberFormat&, int magnitude);
ThrowCompletionOr<MathematicalValue> to_intl_mathematical_value(VM&, Value value); ThrowCompletionOr<MathematicalValue> to_intl_mathematical_value(VM&, Value value);
NumberFormat::UnsignedRoundingMode get_unsigned_rounding_mode(NumberFormat::RoundingMode, bool is_negative); NumberFormat::UnsignedRoundingMode get_unsigned_rounding_mode(NumberFormat::RoundingMode, bool is_negative);
RoundingDecision apply_unsigned_rounding_mode(MathematicalValue const& x, MathematicalValue const& r1, MathematicalValue const& r2, NumberFormat::UnsignedRoundingMode unsigned_rounding_mode); RoundingDecision apply_unsigned_rounding_mode(MathematicalValue const& x, MathematicalValue const& r1, MathematicalValue const& r2, NumberFormat::UnsignedRoundingMode unsigned_rounding_mode);

View file

@ -296,7 +296,7 @@ static ErrorOr<Optional<String>> format_time_zone_offset(StringView locale, Cale
} }
// The digits used for hours, minutes and seconds fields in this format are the locale's default decimal digits. // The digits used for hours, minutes and seconds fields in this format are the locale's default decimal digits.
auto result = TRY(replace_digits_for_number_system(*number_system, TRY(builder.to_string()))); auto result = replace_digits_for_number_system(*number_system, TRY(builder.to_string()));
return TRY(String::from_utf8(formats->gmt_format)).replace("{0}"sv, result, ReplaceMode::FirstOnly); return TRY(String::from_utf8(formats->gmt_format)).replace("{0}"sv, result, ReplaceMode::FirstOnly);
} }

View file

@ -18,9 +18,9 @@ namespace Locale {
Optional<StringView> __attribute__((weak)) get_number_system_symbol(StringView, StringView, NumericSymbol) { return {}; } Optional<StringView> __attribute__((weak)) get_number_system_symbol(StringView, StringView, NumericSymbol) { return {}; }
Optional<NumberGroupings> __attribute__((weak)) get_number_system_groupings(StringView, StringView) { 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 {}; } Optional<NumberFormat> __attribute__((weak)) get_standard_number_system_format(StringView, StringView, StandardNumberFormatType) { return {}; }
ErrorOr<Vector<NumberFormat>> __attribute__((weak)) get_compact_number_system_formats(StringView, StringView, CompactNumberFormatType) { return Vector<NumberFormat> {}; } Vector<NumberFormat> __attribute__((weak)) get_compact_number_system_formats(StringView, StringView, CompactNumberFormatType) { return {}; }
ErrorOr<Vector<NumberFormat>> __attribute__((weak)) get_unit_formats(StringView, StringView, Style) { return Vector<NumberFormat> {}; } Vector<NumberFormat> __attribute__((weak)) get_unit_formats(StringView, StringView, Style) { return {}; }
Optional<ReadonlySpan<u32>> __attribute__((weak)) get_digits_for_number_system(StringView) 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(); 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); auto digits = get_digits_for_number_system(system);
if (!digits.has_value()) if (!digits.has_value())
@ -41,13 +41,13 @@ ErrorOr<String> replace_digits_for_number_system(StringView system, StringView n
for (auto ch : number) { for (auto ch : number) {
if (is_ascii_digit(ch)) { if (is_ascii_digit(ch)) {
u32 digit = digits->at(parse_ascii_digit(ch)); u32 digit = digits->at(parse_ascii_digit(ch));
TRY(builder.try_append_code_point(digit)); builder.append_code_point(digit);
} else { } else {
TRY(builder.try_append(ch)); builder.append(ch);
} }
} }
return builder.to_string(); return MUST(builder.to_string());
} }
#if ENABLE_UNICODE_DATA #if ENABLE_UNICODE_DATA
@ -64,7 +64,7 @@ static u32 last_code_point(StringView string)
#endif #endif
// https://www.unicode.org/reports/tr35/tr35-numbers.html#Currencies // 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 #if ENABLE_UNICODE_DATA
constexpr auto number_key = "{number}"sv; 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(); u32 first_currency_code_point = *utf8_currency_display.begin();
if (!Unicode::code_point_has_general_category(first_currency_code_point, Unicode::GeneralCategory::Symbol)) 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 { } else {
u32 last_pattern_code_point = last_code_point(base_pattern.substring_view(0, *number_index)); 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); u32 last_currency_code_point = last_code_point(currency_display);
if (!Unicode::code_point_has_general_category(last_currency_code_point, Unicode::GeneralCategory::Symbol)) 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()) 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 #endif
return OptionalNone {}; return {};
} }
// https://unicode.org/reports/tr35/tr35-numbers.html#83-range-pattern-processing // 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 #if ENABLE_UNICODE_DATA
auto range_pattern_with_spacing = [&]() { auto range_pattern_with_spacing = [&]() {
return String::formatted(" {} ", range_separator); return MUST(String::formatted(" {} ", range_separator));
}; };
Utf8View utf8_range_separator { 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. // 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) { for (auto it = utf8_range_separator.begin(); it != utf8_range_separator.end(); ++it) {
if (Unicode::code_point_has_property(*it, Unicode::Property::White_Space)) 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. // 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(); return range_pattern_with_spacing();
#endif #endif
return OptionalNone {}; return {};
} }
} }

View file

@ -6,7 +6,6 @@
#pragma once #pragma once
#include <AK/Error.h>
#include <AK/Optional.h> #include <AK/Optional.h>
#include <AK/String.h> #include <AK/String.h>
#include <AK/StringView.h> #include <AK/StringView.h>
@ -65,13 +64,13 @@ Optional<StringView> get_number_system_symbol(StringView locale, StringView syst
Optional<NumberGroupings> get_number_system_groupings(StringView locale, StringView system); Optional<NumberGroupings> get_number_system_groupings(StringView locale, StringView system);
Optional<ReadonlySpan<u32>> get_digits_for_number_system(StringView system); Optional<ReadonlySpan<u32>> get_digits_for_number_system(StringView system);
ErrorOr<String> replace_digits_for_number_system(StringView system, StringView number); String replace_digits_for_number_system(StringView system, StringView number);
ErrorOr<Optional<NumberFormat>> get_standard_number_system_format(StringView locale, StringView system, StandardNumberFormatType type); Optional<NumberFormat> get_standard_number_system_format(StringView locale, StringView system, StandardNumberFormatType type);
ErrorOr<Vector<NumberFormat>> get_compact_number_system_formats(StringView locale, StringView system, CompactNumberFormatType type); Vector<NumberFormat> get_compact_number_system_formats(StringView locale, StringView system, CompactNumberFormatType type);
ErrorOr<Vector<NumberFormat>> get_unit_formats(StringView locale, StringView unit, Style style); Vector<NumberFormat> get_unit_formats(StringView locale, StringView unit, Style style);
ErrorOr<Optional<String>> augment_currency_format_pattern(StringView currency_display, StringView base_pattern); Optional<String> augment_currency_format_pattern(StringView currency_display, StringView base_pattern);
ErrorOr<Optional<String>> augment_range_pattern(StringView range_separator, StringView lower, StringView upper); Optional<String> augment_range_pattern(StringView range_separator, StringView lower, StringView upper);
} }