123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- /*
- * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
- #include <AK/AllOf.h>
- #include <AK/CharacterTypes.h>
- #include <AK/GenericLexer.h>
- #include <AK/QuickSort.h>
- #include <AK/StringBuilder.h>
- #include <LibUnicode/Locale.h>
- #if ENABLE_UNICODE_DATA
- # include <LibUnicode/UnicodeLocale.h>
- #endif
- namespace Unicode {
- 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);
- }
- 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);
- }
- 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;
- }
- 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;
- }
- static bool is_key(StringView key)
- {
- // key = alphanum alpha
- if (key.length() != 2)
- return false;
- return is_ascii_alphanumeric(key[0]) && is_ascii_alpha(key[1]);
- }
- static bool is_single_type(StringView type)
- {
- // type = alphanum{3,8} (sep alphanum{3,8})*
- // Note: Consecutive types are not handled here, that is left to the caller.
- if ((type.length() < 3) || (type.length() > 8))
- return false;
- return all_of(type, is_ascii_alphanumeric);
- }
- static bool is_attribute(StringView type)
- {
- // attribute = alphanum{3,8}
- if ((type.length() < 3) || (type.length() > 8))
- return false;
- return all_of(type, is_ascii_alphanumeric);
- }
- static Optional<StringView> consume_next_segment(GenericLexer& lexer, bool with_separator = true)
- {
- constexpr auto is_separator = is_any_of("-_"sv);
- if (with_separator) {
- if (!lexer.next_is(is_separator))
- return {};
- lexer.ignore();
- }
- auto segment = lexer.consume_until(is_separator);
- if (segment.is_empty()) {
- lexer.retreat(with_separator);
- return {};
- }
- return segment;
- }
- static Optional<LanguageID> parse_unicode_language_id(GenericLexer& lexer)
- {
- // https://unicode.org/reports/tr35/#Unicode_language_identifier
- //
- // unicode_language_id = "root"
- // OR
- // unicode_language_id = ((unicode_language_subtag (sep unicode_script_subtag)?) | unicode_script_subtag)
- // (sep unicode_region_subtag)?
- // (sep unicode_variant_subtag)*
- LanguageID language_id {};
- if (lexer.consume_specific("root"sv)) {
- language_id.is_root = true;
- return language_id;
- }
- enum class ParseState {
- ParsingLanguageOrScript,
- ParsingScript,
- ParsingRegion,
- ParsingVariant,
- Done,
- };
- auto state = ParseState::ParsingLanguageOrScript;
- while (!lexer.is_eof() && (state != ParseState::Done)) {
- auto segment = consume_next_segment(lexer, state != ParseState::ParsingLanguageOrScript);
- if (!segment.has_value())
- return {};
- switch (state) {
- case ParseState::ParsingLanguageOrScript:
- if (is_unicode_language_subtag(*segment)) {
- state = ParseState::ParsingScript;
- language_id.language = *segment;
- } else if (is_unicode_script_subtag(*segment)) {
- state = ParseState::ParsingRegion;
- language_id.script = *segment;
- } else {
- return {};
- }
- break;
- case ParseState::ParsingScript:
- if (is_unicode_script_subtag(*segment)) {
- state = ParseState::ParsingRegion;
- language_id.script = *segment;
- break;
- }
- state = ParseState::ParsingRegion;
- [[fallthrough]];
- case ParseState::ParsingRegion:
- if (is_unicode_region_subtag(*segment)) {
- state = ParseState::ParsingVariant;
- language_id.region = *segment;
- break;
- }
- state = ParseState::ParsingVariant;
- [[fallthrough]];
- case ParseState::ParsingVariant:
- if (is_unicode_variant_subtag(*segment)) {
- language_id.variants.append(*segment);
- } else {
- lexer.retreat(segment->length() + 1);
- state = ParseState::Done;
- }
- break;
- default:
- VERIFY_NOT_REACHED();
- }
- }
- return language_id;
- }
- static Optional<LocaleExtension> parse_unicode_locale_extension(GenericLexer& lexer)
- {
- // https://unicode.org/reports/tr35/#unicode_locale_extensions
- //
- // unicode_locale_extensions = sep [uU] ((sep keyword)+ | (sep attribute)+ (sep keyword)*)
- LocaleExtension locale_extension {};
- enum class ParseState {
- ParsingAttributeOrKeyword,
- ParsingAttribute,
- ParsingKeyword,
- Done,
- };
- auto state = ParseState::ParsingAttributeOrKeyword;
- while (!lexer.is_eof() && (state != ParseState::Done)) {
- auto segment = consume_next_segment(lexer);
- if (!segment.has_value())
- return {};
- if (state == ParseState::ParsingAttributeOrKeyword)
- state = is_key(*segment) ? ParseState::ParsingKeyword : ParseState::ParsingAttribute;
- switch (state) {
- case ParseState::ParsingAttribute:
- if (is_attribute(*segment)) {
- locale_extension.attributes.append(*segment);
- break;
- }
- state = ParseState::ParsingKeyword;
- [[fallthrough]];
- case ParseState::ParsingKeyword: {
- // keyword = key (sep type)?
- Keyword keyword { .key = *segment };
- if (!is_key(*segment)) {
- lexer.retreat(segment->length() + 1);
- state = ParseState::Done;
- break;
- }
- while (true) {
- auto type = consume_next_segment(lexer);
- if (!type.has_value() || !is_single_type(*type)) {
- if (type.has_value())
- lexer.retreat(type->length() + 1);
- break;
- }
- keyword.types.append(*type);
- }
- locale_extension.keywords.append(move(keyword));
- break;
- }
- default:
- VERIFY_NOT_REACHED();
- }
- }
- if (locale_extension.attributes.is_empty() && locale_extension.keywords.is_empty())
- return {};
- return locale_extension;
- }
- static Optional<Extension> parse_extension(GenericLexer& lexer)
- {
- // https://unicode.org/reports/tr35/#extensions
- //
- // extensions = unicode_locale_extensions | transformed_extensions | other_extensions
- size_t starting_position = lexer.tell();
- if (auto header = consume_next_segment(lexer); header.has_value() && (header->length() == 1)) {
- switch ((*header)[0]) {
- case 'u':
- case 'U':
- if (auto extension = parse_unicode_locale_extension(lexer); extension.has_value())
- return Extension { extension.release_value() };
- break;
- default:
- // FIXME: Handle transformed_extensions / other_extensions
- break;
- }
- }
- lexer.retreat(lexer.tell() - starting_position);
- return {};
- }
- Optional<LanguageID> parse_unicode_language_id(StringView language)
- {
- GenericLexer lexer { language };
- auto language_id = parse_unicode_language_id(lexer);
- if (!lexer.is_eof())
- return {};
- return language_id;
- }
- Optional<LocaleID> parse_unicode_locale_id(StringView locale)
- {
- GenericLexer lexer { locale };
- // https://unicode.org/reports/tr35/#Unicode_locale_identifier
- //
- // unicode_locale_id = unicode_language_id
- // extensions*
- // pu_extensions?
- auto language_id = parse_unicode_language_id(lexer);
- if (!language_id.has_value())
- return {};
- LocaleID locale_id { language_id.release_value() };
- while (true) {
- auto extension = parse_extension(lexer);
- if (!extension.has_value())
- break;
- locale_id.extensions.append(extension.release_value());
- }
- // FIXME: Handle pu_extensions.
- if (!lexer.is_eof())
- return {};
- return locale_id;
- }
- Optional<String> canonicalize_unicode_locale_id(LocaleID& locale_id)
- {
- // https://unicode.org/reports/tr35/#Canonical_Unicode_Locale_Identifiers
- StringBuilder builder;
- if (!locale_id.language_id.language.has_value())
- return {};
- builder.append(locale_id.language_id.language->to_lowercase_string());
- if (locale_id.language_id.script.has_value()) {
- builder.append('-');
- builder.append(locale_id.language_id.script->to_titlecase_string());
- }
- if (locale_id.language_id.region.has_value()) {
- builder.append('-');
- builder.append(locale_id.language_id.region->to_uppercase_string());
- }
- quick_sort(locale_id.language_id.variants);
- for (auto const& variant : locale_id.language_id.variants) {
- builder.append('-');
- builder.append(variant.to_lowercase_string());
- }
- // FIXME: Handle extensions and pu_extensions.
- return builder.build();
- }
- String const& default_locale()
- {
- static String locale = "en"sv;
- return locale;
- }
- bool is_locale_available([[maybe_unused]] StringView locale)
- {
- #if ENABLE_UNICODE_DATA
- return Detail::locale_from_string(locale).has_value();
- #else
- return false;
- #endif
- }
- Optional<StringView> get_locale_language_mapping([[maybe_unused]] StringView locale, [[maybe_unused]] StringView language)
- {
- #if ENABLE_UNICODE_DATA
- return Detail::get_locale_language_mapping(locale, language);
- #else
- return {};
- #endif
- }
- Optional<StringView> get_locale_territory_mapping([[maybe_unused]] StringView locale, [[maybe_unused]] StringView territory)
- {
- #if ENABLE_UNICODE_DATA
- return Detail::get_locale_territory_mapping(locale, territory);
- #else
- return {};
- #endif
- }
- Optional<StringView> get_locale_script_mapping([[maybe_unused]] StringView locale, [[maybe_unused]] StringView script)
- {
- #if ENABLE_UNICODE_DATA
- return Detail::get_locale_script_tag_mapping(locale, script);
- #else
- return {};
- #endif
- }
- Optional<StringView> get_locale_currency_mapping([[maybe_unused]] StringView locale, [[maybe_unused]] StringView currency)
- {
- #if ENABLE_UNICODE_DATA
- return Detail::get_locale_currency_mapping(locale, currency);
- #else
- return {};
- #endif
- }
- }
|