LibJS+LibUnicode: Add an API to loop over Unicode extensions of one type

This commit is contained in:
Timothy Flynn
2026-03-13 14:36:15 -04:00
committed by Tim Flynn
parent fd0709b6ce
commit 776134ce03
Notes: github-actions[bot] 2026-03-14 12:18:01 +00:00
2 changed files with 19 additions and 9 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2025, Tim Flynn <trflynn89@ladybird.org>
* Copyright (c) 2021-2026, Tim Flynn <trflynn89@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -134,20 +134,18 @@ static LocaleAndKeys make_locale_record(StringView tag, LocaleAndKeys options, R
Vector<Unicode::Keyword> keywords;
// 1. If tag contains a substring that is a Unicode locale extension sequence, then
for (auto& extension : locale_id->extensions) {
if (!extension.has<Unicode::LocaleExtension>())
continue;
locale_id->for_each_extension_of_type<Unicode::LocaleExtension>([&](Unicode::LocaleExtension& components) {
// a. Let extension be the String value consisting of the substring of the Unicode locale extension sequence within tag.
// b. Let components be UnicodeExtensionComponents(extension).
auto& components = extension.get<Unicode::LocaleExtension>();
// c. Let attributes be components.[[Attributes]].
attributes = move(components.attributes);
// d. Let keywords be components.[[Keywords]].
keywords = move(components.keywords);
break;
}
return IterationDecision::Break;
});
// 2. Else,
// a. Let attributes be a new empty List.
// b. Let keywords be a new empty List.

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2024, Tim Flynn <trflynn89@serenityos.org>
* Copyright (c) 2021-2026, Tim Flynn <trflynn89@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -7,6 +7,7 @@
#pragma once
#include <AK/CharacterTypes.h>
#include <AK/IterationDecision.h>
#include <AK/Optional.h>
#include <AK/String.h>
#include <AK/StringView.h>
@@ -73,6 +74,17 @@ struct LocaleID {
return removed_extensions;
}
template<typename ExtensionType, typename Callback>
void for_each_extension_of_type(Callback&& callback)
{
for (auto& extension : extensions) {
if (auto* extension_type = extension.get_pointer<ExtensionType>()) {
if (callback(*extension_type) == IterationDecision::Break)
break;
}
}
}
LanguageID language_id {};
Vector<Extension> extensions {};
Vector<String> private_use_extensions {};