LibJS+LibUnicode: Port Intl.NumberFormat to UTF-16 strings

This commit is contained in:
Timothy Flynn 2025-07-23 14:48:32 -04:00 committed by Andreas Kling
commit e637e148d4
Notes: github-actions[bot] 2025-07-24 08:41:25 +00:00
5 changed files with 19 additions and 18 deletions

View file

@ -407,7 +407,7 @@ Vector<DurationFormatPart> format_numeric_hours(VM& vm, DurationFormat const& du
for (auto& part : hours_parts) {
// a. Append the Record { [[Type]]: part.[[Type]], [[Value]]: part.[[Value]], [[Unit]]: "hour" } to result.
result.unchecked_append({ .type = part.type, .value = move(part.value), .unit = "hour"sv });
result.unchecked_append({ .type = part.type, .value = part.value.to_utf8_but_should_be_ported_to_utf16(), .unit = "hour"sv });
}
// 13. Return result.
@ -472,7 +472,7 @@ Vector<DurationFormatPart> format_numeric_minutes(VM& vm, DurationFormat const&
for (auto& part : minutes_parts) {
// a. Append the Record { [[Type]]: part.[[Type]], [[Value]]: part.[[Value]], [[Unit]]: "minute" } to result.
result.unchecked_append({ .type = part.type, .value = move(part.value), .unit = "minute"sv });
result.unchecked_append({ .type = part.type, .value = part.value.to_utf8_but_should_be_ported_to_utf16(), .unit = "minute"sv });
}
// 14. Return result.
@ -560,7 +560,7 @@ Vector<DurationFormatPart> format_numeric_seconds(VM& vm, DurationFormat const&
for (auto& part : seconds_parts) {
// a. Append the Record { [[Type]]: part.[[Type]], [[Value]]: part.[[Value]], [[Unit]]: "second" } to result.
result.unchecked_append({ .type = part.type, .value = move(part.value), .unit = "second"sv });
result.unchecked_append({ .type = part.type, .value = part.value.to_utf8_but_should_be_ported_to_utf16(), .unit = "second"sv });
}
// 18. Return result.
@ -922,7 +922,7 @@ Vector<DurationFormatPart> partition_duration_format_pattern(VM& vm, DurationFor
for (auto& part : parts) {
// a. Append the Record { [[Type]]: part.[[Type]], [[Value]]: part.[[Value]], [[Unit]]: numberFormatUnit } to list.
list.unchecked_append({ .type = part.type, .value = move(part.value), .unit = number_format_unit.as_string() });
list.unchecked_append({ .type = part.type, .value = part.value.to_utf8_but_should_be_ported_to_utf16(), .unit = number_format_unit.as_string() });
}
// 11. Append list to result.

View file

@ -152,7 +152,7 @@ Vector<Unicode::NumberFormat::Partition> partition_number_pattern(NumberFormat c
}
// 16.5.6 FormatNumeric ( numberFormat, x ), https://tc39.es/ecma402/#sec-formatnumber
String format_numeric(NumberFormat const& number_format, MathematicalValue const& number)
Utf16String format_numeric(NumberFormat const& number_format, MathematicalValue const& number)
{
// 1. Let parts be ? PartitionNumberPattern(numberFormat, x).
// 2. Let result be the empty String.
@ -258,7 +258,7 @@ ThrowCompletionOr<Vector<Unicode::NumberFormat::Partition>> partition_number_ran
}
// 16.5.22 FormatNumericRange ( numberFormat, x, y ), https://tc39.es/ecma402/#sec-formatnumericrange
ThrowCompletionOr<String> format_numeric_range(VM& vm, NumberFormat const& number_format, MathematicalValue const& start, MathematicalValue const& end)
ThrowCompletionOr<Utf16String> format_numeric_range(VM& vm, NumberFormat const& number_format, MathematicalValue const& start, MathematicalValue const& end)
{
// 1. Let parts be ? PartitionNumberRangePattern(numberFormat, x, y).
{

View file

@ -182,11 +182,11 @@ private:
int currency_digits(StringView currency);
Vector<Unicode::NumberFormat::Partition> partition_number_pattern(NumberFormat const&, MathematicalValue const& number);
String format_numeric(NumberFormat const&, MathematicalValue const& number);
Utf16String format_numeric(NumberFormat const&, MathematicalValue const& number);
GC::Ref<Array> format_numeric_to_parts(VM&, NumberFormat const&, MathematicalValue const& number);
ThrowCompletionOr<MathematicalValue> to_intl_mathematical_value(VM&, Value value);
ThrowCompletionOr<Vector<Unicode::NumberFormat::Partition>> partition_number_range_pattern(VM&, NumberFormat const&, MathematicalValue const& start, MathematicalValue const& end);
ThrowCompletionOr<String> format_numeric_range(VM&, NumberFormat const&, MathematicalValue const& start, MathematicalValue const& end);
ThrowCompletionOr<Utf16String> format_numeric_range(VM&, NumberFormat const&, MathematicalValue const& start, MathematicalValue const& end);
ThrowCompletionOr<GC::Ref<Array>> format_numeric_range_to_parts(VM&, NumberFormat const&, MathematicalValue const& start, MathematicalValue const& end);
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2024, Tim Flynn <trflynn89@ladybird.org>
* Copyright (c) 2021-2025, Tim Flynn <trflynn89@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -601,7 +601,7 @@ public:
virtual ~NumberFormatImpl() override = default;
virtual String format(Value const& value) const override
virtual Utf16String format(Value const& value) const override
{
UErrorCode status = U_ZERO_ERROR;
@ -613,7 +613,7 @@ public:
if (icu_failure(status))
return {};
return icu_string_to_string(result);
return icu_string_to_utf16_string(result);
}
virtual Vector<Partition> format_to_parts(Value const& value) const override
@ -625,7 +625,7 @@ public:
return format_to_parts_impl(formatted, value, value);
}
virtual String format_range(Value const& start, Value const& end) const override
virtual Utf16String format_range(Value const& start, Value const& end) const override
{
UErrorCode status = U_ZERO_ERROR;
@ -637,7 +637,7 @@ public:
if (icu_failure(status))
return {};
return icu_string_to_string(result);
return icu_string_to_utf16_string(result);
}
virtual Vector<Partition> format_range_to_parts(Value const& start, Value const& end) const override
@ -830,7 +830,7 @@ private:
auto value = formatted_number.tempSubStringBetween(range.start, range.end);
Partition partition;
partition.value = icu_string_to_string(value);
partition.value = icu_string_to_utf16_string(value);
apply_to_partition(partition, range.field, range.start);
result.unchecked_append(move(partition));

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2024, Tim Flynn <trflynn89@serenityos.org>
* Copyright (c) 2021-2025, Tim Flynn <trflynn89@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -9,6 +9,7 @@
#include <AK/Optional.h>
#include <AK/String.h>
#include <AK/StringView.h>
#include <AK/Utf16String.h>
#include <AK/Variant.h>
#include <AK/Vector.h>
#include <LibUnicode/Forward.h>
@ -149,16 +150,16 @@ public:
struct Partition {
StringView type;
String value;
Utf16String value;
StringView source;
};
using Value = Variant<double, String>;
virtual String format(Value const&) const = 0;
virtual Utf16String format(Value const&) const = 0;
virtual Vector<Partition> format_to_parts(Value const&) const = 0;
virtual String format_range(Value const&, Value const&) const = 0;
virtual Utf16String format_range(Value const&, Value const&) const = 0;
virtual Vector<Partition> format_range_to_parts(Value const&, Value const&) const = 0;
virtual void create_plural_rules(PluralForm) = 0;