From 80b86d20dc1dbe68dea845cce61e679bb5885137 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Mon, 15 Nov 2021 08:26:55 -0500 Subject: [PATCH] LibJS: Cache the number format used for compact notation Finding the best number format to use for compact notation involves creating a Vector of all compact formats for the locale and looking for the one that best matches the number's magnitude. ECMA-402 wants this number format to be found multiple times, so cache the result for future use. --- Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp | 7 +++++-- Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.h | 8 ++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp index 05be1bffbf..5c5dcc2a2f 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp @@ -11,7 +11,6 @@ #include #include #include -#include #include #include @@ -1599,7 +1598,11 @@ int compute_exponent_for_magniude(NumberFormat& number_format, int magnitude) best_number_format = &format_rule; } - return best_number_format ? best_number_format->exponent : 0; + if (best_number_format == nullptr) + return 0; + + number_format.set_compact_format(*best_number_format); + return best_number_format->exponent; } default: diff --git a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.h b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.h index 2fded6977f..0f676d6a01 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.h @@ -10,6 +10,7 @@ #include #include #include +#include namespace JS::Intl { @@ -155,6 +156,10 @@ public: NativeFunction* bound_format() const { return m_bound_format; } void set_bound_format(NativeFunction* bound_format) { m_bound_format = bound_format; } + bool has_compact_format() const { return m_compact_format.has_value(); } + void set_compact_format(Unicode::NumberFormat compact_format) { m_compact_format = compact_format; } + Unicode::NumberFormat compact_format() const { return *m_compact_format; } + private: virtual void visit_edges(Visitor&) override; @@ -181,6 +186,9 @@ private: // Non-standard. Stores the resolved currency display string based on [[Locale]], [[Currency]], and [[CurrencyDisplay]]. Optional m_resolved_currency_display; + + // Non-standard. Stores the resolved compact number format based on [[Locale]], [[Notation], [[Style]], and [[CompactDisplay]]. + Optional m_compact_format; }; struct FormatResult {