mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 04:27:44 +00:00
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:
parent
1f546476d5
commit
80b86d20dc
2 changed files with 13 additions and 2 deletions
|
@ -11,7 +11,6 @@
|
||||||
#include <LibJS/Runtime/Intl/NumberFormat.h>
|
#include <LibJS/Runtime/Intl/NumberFormat.h>
|
||||||
#include <LibJS/Runtime/Intl/NumberFormatFunction.h>
|
#include <LibJS/Runtime/Intl/NumberFormatFunction.h>
|
||||||
#include <LibUnicode/CurrencyCode.h>
|
#include <LibUnicode/CurrencyCode.h>
|
||||||
#include <LibUnicode/Locale.h>
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
@ -1599,7 +1598,11 @@ int compute_exponent_for_magniude(NumberFormat& number_format, int magnitude)
|
||||||
best_number_format = &format_rule;
|
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:
|
default:
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
#include <LibJS/Runtime/Intl/AbstractOperations.h>
|
#include <LibJS/Runtime/Intl/AbstractOperations.h>
|
||||||
#include <LibJS/Runtime/Object.h>
|
#include <LibJS/Runtime/Object.h>
|
||||||
|
#include <LibUnicode/Locale.h>
|
||||||
|
|
||||||
namespace JS::Intl {
|
namespace JS::Intl {
|
||||||
|
|
||||||
|
@ -155,6 +156,10 @@ public:
|
||||||
NativeFunction* bound_format() const { return m_bound_format; }
|
NativeFunction* bound_format() const { return m_bound_format; }
|
||||||
void set_bound_format(NativeFunction* bound_format) { m_bound_format = 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:
|
private:
|
||||||
virtual void visit_edges(Visitor&) override;
|
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]].
|
// Non-standard. Stores the resolved currency display string based on [[Locale]], [[Currency]], and [[CurrencyDisplay]].
|
||||||
Optional<StringView> m_resolved_currency_display;
|
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 {
|
struct FormatResult {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue