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.
This commit is contained in:
Timothy Flynn 2021-11-15 08:26:55 -05:00 committed by Linus Groh
parent 1f546476d5
commit 80b86d20dc
Notes: sideshowbarker 2024-07-18 01:05:07 +09:00
2 changed files with 13 additions and 2 deletions

View file

@ -11,7 +11,6 @@
#include <LibJS/Runtime/Intl/NumberFormat.h>
#include <LibJS/Runtime/Intl/NumberFormatFunction.h>
#include <LibUnicode/CurrencyCode.h>
#include <LibUnicode/Locale.h>
#include <math.h>
#include <stdlib.h>
@ -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:

View file

@ -10,6 +10,7 @@
#include <AK/String.h>
#include <LibJS/Runtime/Intl/AbstractOperations.h>
#include <LibJS/Runtime/Object.h>
#include <LibUnicode/Locale.h>
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<StringView> m_resolved_currency_display;
// Non-standard. Stores the resolved compact number format based on [[Locale]], [[Notation], [[Style]], and [[CompactDisplay]].
Optional<Unicode::NumberFormat> m_compact_format;
};
struct FormatResult {