mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-08 05:27:14 +09:00
LibUnicode: Use ICU to convert Unicode keywords to their BCP 47 value
We were manually doing this for the calendar keyword, and would need to do so for the collation keyword as well. I wasn't aware of this API originally, so let's start using it.
This commit is contained in:
parent
486602e796
commit
21cff645a2
Notes:
github-actions[bot]
2025-06-02 21:05:02 +00:00
Author: https://github.com/trflynn89
Commit: 21cff645a2
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4970
3 changed files with 18 additions and 21 deletions
|
@ -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
|
||||
*/
|
||||
|
@ -16,6 +16,7 @@
|
|||
#include <unicode/locid.h>
|
||||
#include <unicode/strenum.h>
|
||||
#include <unicode/stringpiece.h>
|
||||
#include <unicode/uloc.h>
|
||||
#include <unicode/unistr.h>
|
||||
#include <unicode/utypes.h>
|
||||
#include <unicode/uversion.h>
|
||||
|
@ -102,7 +103,7 @@ String icu_string_to_string(icu::UnicodeString const& string);
|
|||
String icu_string_to_string(UChar const*, i32 length);
|
||||
|
||||
template<typename Filter>
|
||||
Vector<String> icu_string_enumeration_to_list(OwnPtr<icu::StringEnumeration> enumeration, Filter&& filter)
|
||||
Vector<String> icu_string_enumeration_to_list(OwnPtr<icu::StringEnumeration> enumeration, char const* bcp47_keyword, Filter&& filter)
|
||||
{
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
Vector<String> result;
|
||||
|
@ -112,23 +113,28 @@ Vector<String> icu_string_enumeration_to_list(OwnPtr<icu::StringEnumeration> enu
|
|||
|
||||
while (true) {
|
||||
i32 length = 0;
|
||||
auto const* keyword = enumeration->next(&length, status);
|
||||
auto const* value = enumeration->next(&length, status);
|
||||
|
||||
if (icu_failure(status) || keyword == nullptr)
|
||||
if (icu_failure(status) || value == nullptr)
|
||||
break;
|
||||
|
||||
if (!filter(keyword))
|
||||
if (!filter(value))
|
||||
continue;
|
||||
|
||||
result.append(MUST(String::from_utf8({ keyword, static_cast<size_t>(length) })));
|
||||
if (bcp47_keyword) {
|
||||
if (auto const* bcp47_value = uloc_toUnicodeLocaleType(bcp47_keyword, value))
|
||||
result.append(MUST(String::from_utf8({ bcp47_value, strlen(bcp47_value) })));
|
||||
} else {
|
||||
result.append(MUST(String::from_utf8({ value, static_cast<size_t>(length) })));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE Vector<String> icu_string_enumeration_to_list(OwnPtr<icu::StringEnumeration> enumeration)
|
||||
ALWAYS_INLINE Vector<String> icu_string_enumeration_to_list(OwnPtr<icu::StringEnumeration> enumeration, char const* bcp47_keyword)
|
||||
{
|
||||
return icu_string_enumeration_to_list(move(enumeration), [](char const*) { return true; });
|
||||
return icu_string_enumeration_to_list(move(enumeration), bcp47_keyword, [](char const*) { return true; });
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -107,7 +107,7 @@ static Vector<String> icu_available_time_zones(Optional<ByteString> const& regio
|
|||
if (icu_failure(status))
|
||||
return { "UTC"_string };
|
||||
|
||||
auto time_zones = icu_string_enumeration_to_list(move(time_zone_enumerator), [](char const* zone) {
|
||||
auto time_zones = icu_string_enumeration_to_list(move(time_zone_enumerator), nullptr, [](char const* zone) {
|
||||
return !is_legacy_non_iana_time_zone({ zone, strlen(zone) });
|
||||
});
|
||||
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
|
@ -59,16 +59,7 @@ Vector<String> available_calendars(StringView locale)
|
|||
if (icu_failure(status))
|
||||
return {};
|
||||
|
||||
auto calendars = icu_string_enumeration_to_list(move(keywords));
|
||||
|
||||
for (auto& calendar : calendars) {
|
||||
if (calendar == "gregorian"sv)
|
||||
calendar = "gregory"_string;
|
||||
else if (calendar == "ethiopic-amete-alem"sv)
|
||||
calendar = "ethioaa"_string;
|
||||
}
|
||||
|
||||
return calendars;
|
||||
return icu_string_enumeration_to_list(move(keywords), "ca");
|
||||
}
|
||||
|
||||
Vector<String> const& available_currencies()
|
||||
|
@ -162,7 +153,7 @@ Vector<String> const& available_number_systems()
|
|||
if (icu_failure(status))
|
||||
return {};
|
||||
|
||||
auto number_systems = icu_string_enumeration_to_list(move(keywords), [&](char const* keyword) {
|
||||
auto number_systems = icu_string_enumeration_to_list(move(keywords), "nu", [&](char const* keyword) {
|
||||
auto system = adopt_own_if_nonnull(icu::NumberingSystem::createInstanceByName(keyword, status));
|
||||
if (icu_failure(status))
|
||||
return false;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue