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

This commit is contained in:
Timothy Flynn 2025-07-23 15:16:22 -04:00 committed by Andreas Kling
parent abcb2d42bc
commit 173bb67004
4 changed files with 10 additions and 9 deletions

View File

@ -87,7 +87,7 @@ ThrowCompletionOr<Vector<Unicode::RelativeTimeFormat::Partition>> partition_rela
}
// 18.5.4 FormatRelativeTime ( relativeTimeFormat, value, unit ), https://tc39.es/ecma402/#sec-FormatRelativeTime
ThrowCompletionOr<String> format_relative_time(VM& vm, RelativeTimeFormat& relative_time_format, double value, StringView unit)
ThrowCompletionOr<Utf16String> format_relative_time(VM& vm, RelativeTimeFormat& relative_time_format, double value, StringView unit)
{
// 1. Let parts be ? PartitionRelativeTimePattern(relativeTimeFormat, value, unit).
auto time_unit = TRY([&]() -> ThrowCompletionOr<Unicode::TimeUnit> {

View File

@ -56,7 +56,7 @@ private:
ThrowCompletionOr<Unicode::TimeUnit> singular_relative_time_unit(VM&, StringView unit);
ThrowCompletionOr<Vector<Unicode::RelativeTimeFormat::Partition>> partition_relative_time_pattern(VM&, RelativeTimeFormat&, double value, StringView unit);
ThrowCompletionOr<String> format_relative_time(VM&, RelativeTimeFormat&, double value, StringView unit);
ThrowCompletionOr<Utf16String> format_relative_time(VM&, RelativeTimeFormat&, double value, StringView unit);
ThrowCompletionOr<GC::Ref<Array>> format_relative_time_to_parts(VM&, RelativeTimeFormat&, double value, StringView unit);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2024, Tim Flynn <trflynn89@serenityos.org>
* Copyright (c) 2022-2025, Tim Flynn <trflynn89@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -142,7 +142,7 @@ public:
virtual ~RelativeTimeFormatImpl() override = default;
virtual String format(double time, TimeUnit unit, NumericDisplay numeric_display) const override
virtual Utf16String format(double time, TimeUnit unit, NumericDisplay numeric_display) const override
{
UErrorCode status = U_ZERO_ERROR;
@ -152,7 +152,7 @@ public:
if (icu_failure(status))
return {};
return icu_string_to_string(formatted_time);
return icu_string_to_utf16_string(formatted_time);
}
virtual Vector<Partition> format_to_parts(double time, TimeUnit unit, NumericDisplay numeric_display) const override
@ -172,7 +172,7 @@ public:
auto create_partition = [&](i32 field, i32 begin, i32 end, bool is_unit) {
Partition partition;
partition.type = icu_relative_time_format_field_to_string(field);
partition.value = icu_string_to_string(formatted_time.tempSubStringBetween(begin, end));
partition.value = icu_string_to_utf16_string(formatted_time.tempSubStringBetween(begin, end));
if (is_unit)
partition.unit = unit_string;
result.append(move(partition));

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2024, Tim Flynn <trflynn89@serenityos.org>
* Copyright (c) 2022-2025, Tim Flynn <trflynn89@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -10,6 +10,7 @@
#include <AK/Optional.h>
#include <AK/String.h>
#include <AK/StringView.h>
#include <AK/Utf16String.h>
#include <AK/Vector.h>
#include <LibUnicode/Forward.h>
@ -43,11 +44,11 @@ public:
struct Partition {
StringView type;
String value;
Utf16String value;
StringView unit;
};
virtual String format(double, TimeUnit, NumericDisplay) const = 0;
virtual Utf16String format(double, TimeUnit, NumericDisplay) const = 0;
virtual Vector<Partition> format_to_parts(double, TimeUnit, NumericDisplay) const = 0;
protected: