LibUnicode: Port Intl.PluralRules to UTF-16 strings

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

View file

@ -169,6 +169,11 @@ Utf16String icu_string_to_utf16_string(UChar const* string, i32 length)
return Utf16String::from_utf16_without_validation({ string, static_cast<size_t>(length) });
}
Utf16View icu_string_to_utf16_view(icu::UnicodeString const& string)
{
return { string.getBuffer(), static_cast<size_t>(string.length()) };
}
UCharIterator icu_string_iterator(Utf16View const& string)
{
UCharIterator iterator;

View file

@ -116,6 +116,8 @@ String icu_string_to_string(UChar const*, i32 length);
Utf16String icu_string_to_utf16_string(icu::UnicodeString const& string);
Utf16String icu_string_to_utf16_string(UChar const*, i32 length);
Utf16View icu_string_to_utf16_view(icu::UnicodeString const& string);
UCharIterator icu_string_iterator(Utf16View const&);
template<typename Filter>

View file

@ -671,7 +671,7 @@ public:
if (icu_failure(status))
return PluralCategory::Other;
return plural_category_from_string(icu_string_to_string(result));
return plural_category_from_string(icu_string_to_utf16_view(result));
}
virtual PluralCategory select_plural_range(double start, double end) const override
@ -694,7 +694,7 @@ public:
if (icu_failure(status))
return PluralCategory::Other;
return plural_category_from_string(icu_string_to_string(result));
return plural_category_from_string(icu_string_to_utf16_view(result));
}
virtual Vector<PluralCategory> available_plural_categories() const override
@ -710,7 +710,7 @@ public:
while (true) {
i32 length = 0;
auto const* category = keywords->next(&length, status);
auto const* category = keywords->unext(&length, status);
if (icu_failure(status) || category == nullptr)
break;

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
*/
@ -28,7 +28,7 @@ StringView plural_form_to_string(PluralForm plural_form)
VERIFY_NOT_REACHED();
}
PluralCategory plural_category_from_string(StringView category)
PluralCategory plural_category_from_string(Utf16View const& category)
{
if (category == "other"sv)
return PluralCategory::Other;
@ -49,7 +49,7 @@ PluralCategory plural_category_from_string(StringView category)
VERIFY_NOT_REACHED();
}
StringView plural_category_to_string(PluralCategory category)
Utf16View plural_category_to_string(PluralCategory category)
{
switch (category) {
case PluralCategory::Other:

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2024, Tim Flynn <trflynn89@ladybird.org>
* Copyright (c) 2022-2025, Tim Flynn <trflynn89@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -7,6 +7,7 @@
#pragma once
#include <AK/StringView.h>
#include <AK/Utf16View.h>
namespace Unicode {
@ -30,7 +31,7 @@ enum class PluralCategory {
ExactlyZero,
ExactlyOne,
};
PluralCategory plural_category_from_string(StringView);
StringView plural_category_to_string(PluralCategory);
PluralCategory plural_category_from_string(Utf16View const&);
Utf16View plural_category_to_string(PluralCategory);
}