2021-08-25 02:15:38 +00:00
|
|
|
/*
|
2024-06-08 15:22:05 +00:00
|
|
|
* Copyright (c) 2021-2024, Tim Flynn <trflynn89@serenityos.org>
|
2021-08-25 02:15:38 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-09-03 01:49:24 +00:00
|
|
|
#include <AK/CharacterTypes.h>
|
2021-08-25 02:15:38 +00:00
|
|
|
#include <AK/Optional.h>
|
2023-01-15 15:04:26 +00:00
|
|
|
#include <AK/String.h>
|
2021-08-25 02:15:38 +00:00
|
|
|
#include <AK/StringView.h>
|
2021-08-27 20:38:06 +00:00
|
|
|
#include <AK/Variant.h>
|
2021-08-25 02:15:38 +00:00
|
|
|
#include <AK/Vector.h>
|
2024-06-23 13:14:27 +00:00
|
|
|
#include <LibUnicode/Forward.h>
|
2021-08-25 02:15:38 +00:00
|
|
|
|
2024-06-23 13:14:27 +00:00
|
|
|
namespace Unicode {
|
2021-08-25 02:15:38 +00:00
|
|
|
|
|
|
|
struct LanguageID {
|
2023-08-22 19:39:18 +00:00
|
|
|
String to_string() const;
|
2021-09-03 01:44:12 +00:00
|
|
|
bool operator==(LanguageID const&) const = default;
|
2021-09-01 21:54:24 +00:00
|
|
|
|
2021-08-25 02:15:38 +00:00
|
|
|
bool is_root { false };
|
2023-01-19 15:53:20 +00:00
|
|
|
Optional<String> language {};
|
|
|
|
Optional<String> script {};
|
|
|
|
Optional<String> region {};
|
|
|
|
Vector<String> variants {};
|
2021-08-25 02:15:38 +00:00
|
|
|
};
|
|
|
|
|
2021-08-27 20:38:06 +00:00
|
|
|
struct Keyword {
|
2023-01-19 15:53:20 +00:00
|
|
|
String key {};
|
|
|
|
String value {};
|
2021-08-27 20:38:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct LocaleExtension {
|
2023-01-19 15:53:20 +00:00
|
|
|
Vector<String> attributes {};
|
2021-08-27 20:38:06 +00:00
|
|
|
Vector<Keyword> keywords {};
|
|
|
|
};
|
|
|
|
|
2021-08-27 21:11:48 +00:00
|
|
|
struct TransformedField {
|
2023-01-19 15:53:20 +00:00
|
|
|
String key {};
|
|
|
|
String value {};
|
2021-08-27 21:11:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct TransformedExtension {
|
|
|
|
Optional<LanguageID> language {};
|
|
|
|
Vector<TransformedField> fields {};
|
|
|
|
};
|
|
|
|
|
2021-08-27 21:24:20 +00:00
|
|
|
struct OtherExtension {
|
|
|
|
char key {};
|
2023-01-19 15:53:20 +00:00
|
|
|
String value {};
|
2021-08-27 21:24:20 +00:00
|
|
|
};
|
|
|
|
|
2022-09-02 15:02:34 +00:00
|
|
|
using Extension = AK::Variant<LocaleExtension, TransformedExtension, OtherExtension>;
|
2021-08-27 20:38:06 +00:00
|
|
|
|
2021-08-25 02:15:38 +00:00
|
|
|
struct LocaleID {
|
2023-08-22 19:39:18 +00:00
|
|
|
String to_string() const;
|
2021-09-01 21:54:24 +00:00
|
|
|
|
|
|
|
template<typename ExtensionType>
|
2021-09-05 22:29:11 +00:00
|
|
|
Vector<Extension> remove_extension_type()
|
2021-09-01 21:54:24 +00:00
|
|
|
{
|
2021-09-05 22:29:11 +00:00
|
|
|
Vector<Extension> removed_extensions {};
|
2021-09-01 21:54:24 +00:00
|
|
|
auto tmp_extensions = move(extensions);
|
|
|
|
|
|
|
|
for (auto& extension : tmp_extensions) {
|
2021-09-05 22:29:11 +00:00
|
|
|
if (extension.has<ExtensionType>())
|
|
|
|
removed_extensions.append(move(extension));
|
|
|
|
else
|
2021-09-01 21:54:24 +00:00
|
|
|
extensions.append(move(extension));
|
|
|
|
}
|
2021-09-05 22:29:11 +00:00
|
|
|
|
|
|
|
return removed_extensions;
|
2021-09-01 21:54:24 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 02:15:38 +00:00
|
|
|
LanguageID language_id {};
|
2021-08-27 20:38:06 +00:00
|
|
|
Vector<Extension> extensions {};
|
2023-01-19 15:53:20 +00:00
|
|
|
Vector<String> private_use_extensions {};
|
2021-08-25 02:15:38 +00:00
|
|
|
};
|
|
|
|
|
2024-06-16 01:04:55 +00:00
|
|
|
enum class Style {
|
LibJS+LibUnicode: Generate all styles of currency localizations
Currently, LibUnicode is only parsing and generating the "long" style of
currency display names. However, the CLDR contains "short" and "narrow"
forms as well that need to be handled. Parse these, and update LibJS to
actually respect the "style" option provided by the user for displaying
currencies with Intl.DisplayNames.
Note: There are some discrepencies between the engines on how style is
handled. In particular, running:
new Intl.DisplayNames('en', {type:'currency', style:'narrow'}).of('usd')
Gives:
SpiderMoney: "USD"
V8: "US Dollar"
LibJS: "$"
And running:
new Intl.DisplayNames('en', {type:'currency', style:'short'}).of('usd')
Gives:
SpiderMonkey: "$"
V8: "US Dollar"
LibJS: "$"
My best guess is V8 isn't handling style, and just returning the long
form (which is what LibJS did before this commit). And SpiderMoney can
handle some styles, but if they don't have a value for the requested
style, they fall back to the canonicalized code passed into of().
2021-11-12 18:11:30 +00:00
|
|
|
Long,
|
|
|
|
Short,
|
|
|
|
Narrow,
|
|
|
|
};
|
|
|
|
|
2021-08-25 02:15:38 +00:00
|
|
|
// Note: These methods only verify that the provided strings match the EBNF grammar of the
|
|
|
|
// Unicode identifier subtag (i.e. no validation is done that the tags actually exist).
|
2021-09-03 01:49:24 +00:00
|
|
|
constexpr bool is_unicode_language_subtag(StringView subtag)
|
|
|
|
{
|
|
|
|
// unicode_language_subtag = alpha{2,3} | alpha{5,8}
|
|
|
|
if ((subtag.length() < 2) || (subtag.length() == 4) || (subtag.length() > 8))
|
|
|
|
return false;
|
|
|
|
return all_of(subtag, is_ascii_alpha);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool is_unicode_script_subtag(StringView subtag)
|
|
|
|
{
|
|
|
|
// unicode_script_subtag = alpha{4}
|
|
|
|
if (subtag.length() != 4)
|
|
|
|
return false;
|
|
|
|
return all_of(subtag, is_ascii_alpha);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool is_unicode_region_subtag(StringView subtag)
|
|
|
|
{
|
|
|
|
// unicode_region_subtag = (alpha{2} | digit{3})
|
|
|
|
if (subtag.length() == 2)
|
|
|
|
return all_of(subtag, is_ascii_alpha);
|
|
|
|
if (subtag.length() == 3)
|
|
|
|
return all_of(subtag, is_ascii_digit);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool is_unicode_variant_subtag(StringView subtag)
|
|
|
|
{
|
|
|
|
// unicode_variant_subtag = (alphanum{5,8} | digit alphanum{3})
|
|
|
|
if ((subtag.length() >= 5) && (subtag.length() <= 8))
|
|
|
|
return all_of(subtag, is_ascii_alphanumeric);
|
|
|
|
if (subtag.length() == 4)
|
|
|
|
return is_ascii_digit(subtag[0]) && all_of(subtag.substring_view(1), is_ascii_alphanumeric);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-01 11:48:02 +00:00
|
|
|
bool is_type_identifier(StringView);
|
2021-08-25 02:15:38 +00:00
|
|
|
|
2023-08-22 19:39:18 +00:00
|
|
|
Optional<LanguageID> parse_unicode_language_id(StringView);
|
|
|
|
Optional<LocaleID> parse_unicode_locale_id(StringView);
|
2021-09-09 01:56:52 +00:00
|
|
|
|
2024-06-08 15:22:05 +00:00
|
|
|
String canonicalize_unicode_locale_id(StringView);
|
2024-06-17 21:04:29 +00:00
|
|
|
String canonicalize_unicode_extension_values(StringView key, StringView value);
|
2021-08-25 02:15:38 +00:00
|
|
|
|
2023-01-19 15:53:20 +00:00
|
|
|
StringView default_locale();
|
2021-08-25 02:17:08 +00:00
|
|
|
bool is_locale_available(StringView locale);
|
2022-01-04 17:22:25 +00:00
|
|
|
|
2022-01-25 16:27:02 +00:00
|
|
|
Style style_from_string(StringView style);
|
|
|
|
StringView style_to_string(Style style);
|
|
|
|
|
2024-06-08 17:23:26 +00:00
|
|
|
Optional<String> add_likely_subtags(StringView);
|
|
|
|
Optional<String> remove_likely_subtags(StringView);
|
2022-01-04 17:22:25 +00:00
|
|
|
|
2024-06-08 17:36:10 +00:00
|
|
|
bool is_locale_character_ordering_right_to_left(StringView locale);
|
|
|
|
|
2021-08-25 02:15:38 +00:00
|
|
|
}
|