From 6d9b77975701dba3c35e33a9c42c3be779988a6c Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Fri, 8 Jul 2022 11:52:18 -0400 Subject: [PATCH] LibJS: Add an overload of ResolvePlural for use without PluralRules The NumberFormat spec casually indicates the need for a PluralRules object without explicity saying so, with text such as: "which may depend on x in languages having different plural forms." Other implementations actually do create a PluralRules object to resolve those cases with ResolvePlural. However, ResolvePlural doesn't need much from PluralRules to operate, so this can be abstracted out for use in NumberFormat without the need to allocate a PluralRules instance. --- Userland/Libraries/LibJS/Runtime/Intl/PluralRules.cpp | 11 ++++++++--- Userland/Libraries/LibJS/Runtime/Intl/PluralRules.h | 1 + 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Intl/PluralRules.cpp b/Userland/Libraries/LibJS/Runtime/Intl/PluralRules.cpp index 5eaafa6c95..0abb1ed2bd 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/PluralRules.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/PluralRules.cpp @@ -95,6 +95,12 @@ Unicode::PluralCategory plural_rule_select(StringView locale, Unicode::PluralFor // 16.5.3 ResolvePlural ( pluralRules, n ), https://tc39.es/ecma402/#sec-resolveplural Unicode::PluralCategory resolve_plural(GlobalObject& global_object, PluralRules const& plural_rules, Value number) +{ + return resolve_plural(global_object, plural_rules, plural_rules.type(), number); +} + +// Non-standard overload of ResolvePlural to allow using the AO without an Intl.PluralRules object. +Unicode::PluralCategory resolve_plural(GlobalObject& global_object, NumberFormatBase const& number_format, Unicode::PluralForm type, Value number) { // 1. Assert: Type(pluralRules) is Object. // 2. Assert: pluralRules has an [[InitializedPluralRules]] internal slot. @@ -107,13 +113,12 @@ Unicode::PluralCategory resolve_plural(GlobalObject& global_object, PluralRules } // 5. Let locale be pluralRules.[[Locale]]. - auto const& locale = plural_rules.locale(); + auto const& locale = number_format.locale(); // 6. Let type be pluralRules.[[Type]]. - auto type = plural_rules.type(); // 7. Let res be ! FormatNumericToString(pluralRules, n). - auto result = format_numeric_to_string(global_object, plural_rules, number); + auto result = format_numeric_to_string(global_object, number_format, number); // 8. Let s be res.[[FormattedString]]. auto const& string = result.formatted_string; diff --git a/Userland/Libraries/LibJS/Runtime/Intl/PluralRules.h b/Userland/Libraries/LibJS/Runtime/Intl/PluralRules.h index 32614e28e5..1c11b5087a 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/PluralRules.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/PluralRules.h @@ -32,5 +32,6 @@ private: Unicode::PluralOperands get_operands(String const& string); Unicode::PluralCategory plural_rule_select(StringView locale, Unicode::PluralForm type, Value number, Unicode::PluralOperands operands); Unicode::PluralCategory resolve_plural(GlobalObject& global_object, PluralRules const& plural_rules, Value number); +Unicode::PluralCategory resolve_plural(GlobalObject& global_object, NumberFormatBase const& number_format, Unicode::PluralForm type, Value number); }