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

This commit is contained in:
Timothy Flynn 2025-07-23 14:50:46 -04:00 committed by Andreas Kling
commit 6fe0e13474
Notes: github-actions[bot] 2025-07-24 08:41:19 +00:00
4 changed files with 21 additions and 21 deletions

View file

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

View file

@ -82,11 +82,11 @@ public:
void set_numbering_system(String numbering_system) { m_numbering_system = move(numbering_system); } void set_numbering_system(String numbering_system) { m_numbering_system = move(numbering_system); }
String const& numbering_system() const { return m_numbering_system; } String const& numbering_system() const { return m_numbering_system; }
void set_hour_minute_separator(String hour_minute_separator) { m_hour_minute_separator = move(hour_minute_separator); } void set_hour_minute_separator(Utf16String hour_minute_separator) { m_hour_minute_separator = move(hour_minute_separator); }
String const& hour_minute_separator() const { return m_hour_minute_separator; } Utf16String const& hour_minute_separator() const { return m_hour_minute_separator; }
void set_minute_second_separator(String minute_second_separator) { m_minute_second_separator = move(minute_second_separator); } void set_minute_second_separator(Utf16String minute_second_separator) { m_minute_second_separator = move(minute_second_separator); }
String const& minute_second_separator() const { return m_minute_second_separator; } Utf16String const& minute_second_separator() const { return m_minute_second_separator; }
void set_style(StringView style) { m_style = style_from_string(style); } void set_style(StringView style) { m_style = style_from_string(style); }
Style style() const { return m_style; } Style style() const { return m_style; }
@ -129,10 +129,10 @@ public:
private: private:
explicit DurationFormat(Object& prototype); explicit DurationFormat(Object& prototype);
String m_locale; // [[Locale]] String m_locale; // [[Locale]]
String m_numbering_system; // [[NumberingSystem]] String m_numbering_system; // [[NumberingSystem]]
String m_hour_minute_separator; // [[HourMinutesSeparator]] Utf16String m_hour_minute_separator; // [[HourMinutesSeparator]]
String m_minute_second_separator; // [[MinutesSecondsSeparator]] Utf16String m_minute_second_separator; // [[MinutesSecondsSeparator]]
Style m_style { Style::Long }; // [[Style]] Style m_style { Style::Long }; // [[Style]]
DurationUnitOptions m_years_options; // [[YearsOptions]] DurationUnitOptions m_years_options; // [[YearsOptions]]
@ -178,7 +178,7 @@ static constexpr auto duration_instances_components = to_array<DurationInstanceC
struct DurationFormatPart { struct DurationFormatPart {
StringView type; StringView type;
String value; Utf16String value;
StringView unit; StringView unit;
}; };

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org> * Copyright (c) 2024-2025, Tim Flynn <trflynn89@ladybird.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -72,12 +72,12 @@ DigitalFormat digital_format(StringView locale)
digital_format.uses_two_digit_hours = hours.length() == 2; digital_format.uses_two_digit_hours = hours.length() == 2;
auto hours_minutes_separator = lexer.consume_while(is_not_ascii_digit); auto hours_minutes_separator = lexer.consume_while(is_not_ascii_digit);
digital_format.hours_minutes_separator = MUST(String::from_utf8(hours_minutes_separator)); digital_format.hours_minutes_separator = Utf16String::from_utf8(hours_minutes_separator);
lexer.ignore_while(is_ascii_digit); lexer.ignore_while(is_ascii_digit);
auto minutes_seconds_separator = lexer.consume_while(is_not_ascii_digit); auto minutes_seconds_separator = lexer.consume_while(is_not_ascii_digit);
digital_format.minutes_seconds_separator = MUST(String::from_utf8(minutes_seconds_separator)); digital_format.minutes_seconds_separator = Utf16String::from_utf8(minutes_seconds_separator);
locale_data->set_digital_format(move(digital_format)); locale_data->set_digital_format(move(digital_format));
return *locale_data->digital_format(); return *locale_data->digital_format();

View file

@ -1,18 +1,18 @@
/* /*
* Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org> * Copyright (c) 2024-2025, Tim Flynn <trflynn89@ladybird.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#pragma once #pragma once
#include <AK/String.h> #include <AK/Utf16String.h>
namespace Unicode { namespace Unicode {
struct DigitalFormat { struct DigitalFormat {
String hours_minutes_separator { ":"_string }; Utf16String hours_minutes_separator { ":"_utf16 };
String minutes_seconds_separator { ":"_string }; Utf16String minutes_seconds_separator { ":"_utf16 };
bool uses_two_digit_hours { false }; bool uses_two_digit_hours { false };
}; };