2021-08-25 02:15:38 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
#include <AK/String.h>
|
|
|
|
#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>
|
|
|
|
#include <LibUnicode/Forward.h>
|
|
|
|
|
|
|
|
namespace Unicode {
|
|
|
|
|
|
|
|
struct LanguageID {
|
2021-09-01 21:54:24 +00:00
|
|
|
String to_string() const;
|
|
|
|
|
2021-08-25 02:15:38 +00:00
|
|
|
bool is_root { false };
|
2021-08-30 18:31:48 +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 {
|
2021-08-30 18:31:48 +00:00
|
|
|
String key {};
|
|
|
|
Vector<String> types {};
|
2021-08-27 20:38:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct LocaleExtension {
|
2021-08-30 18:31:48 +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 {
|
2021-08-30 18:31:48 +00:00
|
|
|
String key;
|
|
|
|
Vector<String> values {};
|
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 {};
|
2021-08-30 18:31:48 +00:00
|
|
|
Vector<String> values {};
|
2021-08-27 21:24:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
using Extension = Variant<LocaleExtension, TransformedExtension, OtherExtension>;
|
2021-08-27 20:38:06 +00:00
|
|
|
|
2021-08-25 02:15:38 +00:00
|
|
|
struct LocaleID {
|
2021-09-01 21:54:24 +00:00
|
|
|
String to_string() const;
|
|
|
|
|
|
|
|
template<typename ExtensionType>
|
|
|
|
void remove_extension_type()
|
|
|
|
{
|
|
|
|
auto tmp_extensions = move(extensions);
|
|
|
|
|
|
|
|
for (auto& extension : tmp_extensions) {
|
|
|
|
if (!extension.has<ExtensionType>())
|
|
|
|
extensions.append(move(extension));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 02:15:38 +00:00
|
|
|
LanguageID language_id {};
|
2021-08-27 20:38:06 +00:00
|
|
|
Vector<Extension> extensions {};
|
2021-08-30 18:31:48 +00:00
|
|
|
Vector<String> private_use_extensions {};
|
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
|
|
|
|
|
|
|
Optional<LanguageID> parse_unicode_language_id(StringView);
|
|
|
|
Optional<LocaleID> parse_unicode_locale_id(StringView);
|
|
|
|
Optional<String> canonicalize_unicode_locale_id(LocaleID&);
|
|
|
|
|
2021-08-25 02:17:08 +00:00
|
|
|
String const& default_locale();
|
|
|
|
bool is_locale_available(StringView locale);
|
|
|
|
|
2021-08-26 12:17:01 +00:00
|
|
|
Optional<StringView> get_locale_language_mapping(StringView locale, StringView language);
|
2021-08-26 10:56:17 +00:00
|
|
|
Optional<StringView> get_locale_territory_mapping(StringView locale, StringView territory);
|
2021-08-26 12:29:39 +00:00
|
|
|
Optional<StringView> get_locale_script_mapping(StringView locale, StringView script);
|
2021-08-26 12:38:54 +00:00
|
|
|
Optional<StringView> get_locale_currency_mapping(StringView locale, StringView currency);
|
2021-08-25 02:17:08 +00:00
|
|
|
|
2021-08-30 18:56:23 +00:00
|
|
|
Optional<StringView> resolve_language_alias(StringView language);
|
|
|
|
Optional<StringView> resolve_territory_alias(StringView territory);
|
|
|
|
Optional<StringView> resolve_script_tag_alias(StringView script_tag);
|
|
|
|
Optional<StringView> resolve_variant_alias(StringView variant);
|
|
|
|
Optional<StringView> resolve_subdivision_alias(StringView subdivision);
|
|
|
|
|
2021-09-02 22:21:42 +00:00
|
|
|
Optional<LanguageID> add_likely_subtags(LanguageID const& language_id);
|
2021-08-31 13:40:24 +00:00
|
|
|
String resolve_most_likely_territory(LanguageID const& language_id, StringView territory_alias);
|
|
|
|
|
2021-08-25 02:15:38 +00:00
|
|
|
}
|