ladybird/Userland/Libraries/LibJS/Runtime/Intl/MathematicalValue.h
Timothy Flynn ebdb92eef6 LibUnicode+Everywhere: Merge LibLocale back into LibUnicode
LibLocale was split off from LibUnicode a couple years ago to reduce the
number of applications on SerenityOS that depend on CLDR data. Now that
we use ICU, both LibUnicode and LibLocale are actually linking in this
data. And since vcpkg gives us static libraries, both libraries are over
30MB in size.

This patch reverts the separation and merges LibLocale into LibUnicode
again. We now have just one library that includes the ICU data.

Further, this will let LibUnicode share the locale cache that previously
would only exist in LibLocale.
2024-06-23 19:52:45 +02:00

74 lines
1.6 KiB
C++

/*
* Copyright (c) 2022-2024, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/String.h>
#include <AK/Variant.h>
#include <LibCrypto/BigInt/SignedBigInteger.h>
#include <LibJS/Runtime/BigInt.h>
#include <LibJS/Runtime/Value.h>
#include <LibUnicode/NumberFormat.h>
namespace JS::Intl {
// https://tc39.es/ecma402/#intl-mathematical-value
class MathematicalValue {
public:
enum class Symbol {
PositiveInfinity,
NegativeInfinity,
NegativeZero,
NotANumber,
};
MathematicalValue() = default;
explicit MathematicalValue(double value)
: m_value(value_from_number(value))
{
}
explicit MathematicalValue(String value)
: m_value(move(value))
{
}
explicit MathematicalValue(Symbol symbol)
: m_value(symbol)
{
}
MathematicalValue(Value value)
: m_value(value.is_number()
? value_from_number(value.as_double())
: ValueType(MUST(value.as_bigint().big_integer().to_base(10))))
{
}
bool is_number() const;
double as_number() const;
bool is_string() const;
String const& as_string() const;
bool is_mathematical_value() const;
bool is_positive_infinity() const;
bool is_negative_infinity() const;
bool is_negative_zero() const;
bool is_nan() const;
Unicode::NumberFormat::Value to_value() const;
private:
using ValueType = Variant<double, String, Symbol>;
static ValueType value_from_number(double number);
ValueType m_value { 0.0 };
};
}