ladybird/Libraries/LibJS/Runtime/Intl/MathematicalValue.h
Andreas Kling 7025dd1fa7 Libraries: Parse JS strings from UTF-16
Thread UTF-16 string input through JSON, script parsing, Date parsing,
Intl option parsing, Temporal parsing, and the helper library boundaries
that feed those parsers. Preserve ASCII fast paths where the source data
is known to be ASCII.
2026-06-22 19:51:25 +02:00

76 lines
1.7 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/Utf16String.h>
#include <AK/Variant.h>
#include <LibCrypto/BigInt/SignedBigInteger.h>
#include <LibJS/Export.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 JS_API MathematicalValue {
public:
enum class Symbol {
PositiveInfinity,
NegativeInfinity,
NegativeZero,
NotANumber,
};
MathematicalValue() = default;
explicit MathematicalValue(double value)
: m_value(value_from_number(value))
{
}
explicit MathematicalValue(Utf16String 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(Utf16String::from_utf8(MUST(value.as_bigint().big_integer().to_base(10)))))
{
}
bool is_number() const;
double as_number() const;
bool is_string() const;
Utf16String 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, Utf16String, Symbol>;
static ValueType value_from_number(double number);
ValueType m_value { 0.0 };
};
}