diff --git a/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateLocaleData.cpp b/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateLocaleData.cpp index af4a522a860..e762aabf31a 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateLocaleData.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateLocaleData.cpp @@ -1679,7 +1679,7 @@ Optional character_order_for_locale(StringView locale) return {}; } -void resolve_complex_language_aliases(LanguageID& language_id) +ErrorOr resolve_complex_language_aliases(LanguageID& language_id) { for (auto const& map : s_complex_alias) { auto key_language = decode_string(map.key.language); @@ -1695,7 +1695,7 @@ void resolve_complex_language_aliases(LanguageID& language_id) if (!map.key.matches_variants(language_id.variants)) continue; - auto alias = map.alias.to_unicode_language_id().release_value_but_fixme_should_propagate_errors(); + auto alias = TRY(map.alias.to_unicode_language_id()); if (alias.language == "und"sv) alias.language = move(language_id.language); @@ -1709,14 +1709,16 @@ void resolve_complex_language_aliases(LanguageID& language_id) language_id = move(alias); break; } + + return {}; } -Optional add_likely_subtags(LanguageID const& language_id) +ErrorOr> add_likely_subtags(LanguageID const& language_id) { // https://www.unicode.org/reports/tr35/#Likely_Subtags auto const* likely_subtag = resolve_likely_subtag(language_id); if (likely_subtag == nullptr) - return {}; + return OptionalNone {}; auto maximized = language_id; @@ -1728,20 +1730,20 @@ Optional add_likely_subtags(LanguageID const& language_id) auto alias_region = decode_string(likely_subtag->alias.region); if (maximized.language == "und"sv) - maximized.language = String::from_utf8(alias_language).release_value_but_fixme_should_propagate_errors(); + maximized.language = TRY(String::from_utf8(alias_language)); if (!maximized.script.has_value() || (!key_script.is_empty() && !alias_script.is_empty())) - maximized.script = String::from_utf8(alias_script).release_value_but_fixme_should_propagate_errors(); + maximized.script = TRY(String::from_utf8(alias_script)); if (!maximized.region.has_value() || (!key_region.is_empty() && !alias_region.is_empty())) - maximized.region = String::from_utf8(alias_region).release_value_but_fixme_should_propagate_errors(); + maximized.region = TRY(String::from_utf8(alias_region)); return maximized; } -Optional resolve_most_likely_territory(LanguageID const& language_id) +ErrorOr> resolve_most_likely_territory(LanguageID const& language_id) { if (auto const* likely_subtag = resolve_likely_subtag(language_id); likely_subtag != nullptr) - return decode_string(likely_subtag->alias.region); - return {}; + return String::from_utf8(decode_string(likely_subtag->alias.region)); + return OptionalNone {}; } } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesPrototype.cpp index 01f74a9eb74..a54367745f6 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesPrototype.cpp @@ -52,7 +52,7 @@ JS_DEFINE_NATIVE_FUNCTION(DisplayNamesPrototype::of) // 5. Let fields be displayNames.[[Fields]]. // 6. If fields has a field [[]], return fields.[[]]. Optional result; - Optional formatted_result; + Optional formatted_result; switch (display_names->type()) { case DisplayNames::Type::Language: @@ -63,7 +63,7 @@ JS_DEFINE_NATIVE_FUNCTION(DisplayNamesPrototype::of) } if (auto locale = MUST_OR_THROW_OOM(is_structurally_valid_language_tag(vm, code_string)); locale.has_value()) - formatted_result = ::Locale::format_locale_for_display(display_names->locale(), locale.release_value()); + formatted_result = TRY_OR_THROW_OOM(vm, ::Locale::format_locale_for_display(display_names->locale(), locale.release_value())); break; case DisplayNames::Type::Region: result = ::Locale::get_locale_territory_mapping(display_names->locale(), code_string); diff --git a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp index a6a05826aca..1175c15146c 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp @@ -65,7 +65,7 @@ JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::maximize) VERIFY(locale.has_value()); // 3. Let maximal be the result of the Add Likely Subtags algorithm applied to loc.[[Locale]]. If an error is signaled, set maximal to loc.[[Locale]]. - if (auto maximal = ::Locale::add_likely_subtags(locale->language_id); maximal.has_value()) + if (auto maximal = TRY_OR_THROW_OOM(vm, ::Locale::add_likely_subtags(locale->language_id)); maximal.has_value()) locale->language_id = maximal.release_value(); // 4. Return ! Construct(%Locale%, maximal). @@ -85,7 +85,7 @@ JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::minimize) VERIFY(locale.has_value()); // 3. Let minimal be the result of the Remove Likely Subtags algorithm applied to loc.[[Locale]]. If an error is signaled, set minimal to loc.[[Locale]]. - if (auto minimal = ::Locale::remove_likely_subtags(locale->language_id); minimal.has_value()) + if (auto minimal = TRY_OR_THROW_OOM(vm, ::Locale::remove_likely_subtags(locale->language_id)); minimal.has_value()) locale->language_id = minimal.release_value(); // 4. Return ! Construct(%Locale%, minimal). diff --git a/Userland/Libraries/LibLocale/DateTimeFormat.cpp b/Userland/Libraries/LibLocale/DateTimeFormat.cpp index fb330968a05..00ad40ec157 100644 --- a/Userland/Libraries/LibLocale/DateTimeFormat.cpp +++ b/Userland/Libraries/LibLocale/DateTimeFormat.cpp @@ -114,7 +114,7 @@ static auto find_regional_values_for_locale(StringView locale, GetRegionalValues return return_default_values(); if (!language->region.has_value()) - language = add_likely_subtags(*language); + language = add_likely_subtags(*language).release_value_but_fixme_should_propagate_errors(); if (!language.has_value() || !language->region.has_value()) return return_default_values(); diff --git a/Userland/Libraries/LibLocale/Locale.cpp b/Userland/Libraries/LibLocale/Locale.cpp index 7d6ad029623..c10d351a335 100644 --- a/Userland/Libraries/LibLocale/Locale.cpp +++ b/Userland/Libraries/LibLocale/Locale.cpp @@ -584,7 +584,7 @@ static ErrorOr transform_unicode_locale_id_to_canonical_syntax(LocaleID& l for (auto& variant : language_id.variants) variant = TRY(variant.to_lowercase()); - resolve_complex_language_aliases(language_id); + TRY(resolve_complex_language_aliases(language_id)); if (auto alias = resolve_language_alias(*language_id.language); alias.has_value()) { auto language_alias = TRY(parse_unicode_language_id(*alias)); @@ -606,7 +606,7 @@ static ErrorOr transform_unicode_locale_id_to_canonical_syntax(LocaleID& l if (language_id.region.has_value()) { if (auto alias = resolve_territory_alias(*language_id.region); alias.has_value()) - language_id.region = TRY(String::from_deprecated_string(resolve_most_likely_territory_alias(language_id, *alias))); + language_id.region = TRY(resolve_most_likely_territory_alias(language_id, *alias)); } quick_sort(language_id.variants); @@ -824,14 +824,14 @@ Optional __attribute__((weak)) get_locale_short_date_field_mapping(S Optional __attribute__((weak)) get_locale_narrow_date_field_mapping(StringView, StringView) { return {}; } // https://www.unicode.org/reports/tr35/tr35-39/tr35-general.html#Display_Name_Elements -Optional format_locale_for_display(StringView locale, LocaleID locale_id) +ErrorOr> format_locale_for_display(StringView locale, LocaleID locale_id) { auto language_id = move(locale_id.language_id); VERIFY(language_id.language.has_value()); auto patterns = get_locale_display_patterns(locale); if (!patterns.has_value()) - return {}; + return OptionalNone {}; auto primary_tag = get_locale_language_mapping(locale, *language_id.language).value_or(*language_id.language); Optional script; @@ -842,19 +842,26 @@ Optional format_locale_for_display(StringView locale, LocaleID if (language_id.region.has_value()) region = get_locale_territory_mapping(locale, *language_id.region).value_or(*language_id.region); - Optional secondary_tag; + Optional secondary_tag; - if (script.has_value() && region.has_value()) - secondary_tag = patterns->locale_separator.replace("{0}"sv, *script, ReplaceMode::FirstOnly).replace("{1}"sv, *region, ReplaceMode::FirstOnly); - else if (script.has_value()) - secondary_tag = *script; - else if (region.has_value()) - secondary_tag = *region; + if (script.has_value() && region.has_value()) { + secondary_tag = TRY(String::from_utf8(patterns->locale_separator)); + secondary_tag = TRY(secondary_tag->replace("{0}"sv, *script, ReplaceMode::FirstOnly)); + secondary_tag = TRY(secondary_tag->replace("{1}"sv, *region, ReplaceMode::FirstOnly)); + } else if (script.has_value()) { + secondary_tag = TRY(String::from_utf8(*script)); + } else if (region.has_value()) { + secondary_tag = TRY(String::from_utf8(*region)); + } if (!secondary_tag.has_value()) - return primary_tag; + return String::from_utf8(primary_tag); - return patterns->locale_pattern.replace("{0}"sv, primary_tag, ReplaceMode::FirstOnly).replace("{1}"sv, *secondary_tag, ReplaceMode::FirstOnly); + auto result = TRY(String::from_utf8(patterns->locale_pattern)); + result = TRY(result.replace("{0}"sv, primary_tag, ReplaceMode::FirstOnly)); + result = TRY(result.replace("{1}"sv, *secondary_tag, ReplaceMode::FirstOnly)); + + return result; } Optional __attribute__((weak)) get_locale_list_patterns(StringView, StringView, Style) { return {}; } @@ -866,10 +873,10 @@ Optional __attribute__((weak)) resolve_territory_alias(StringView) { Optional __attribute__((weak)) resolve_script_tag_alias(StringView) { return {}; } Optional __attribute__((weak)) resolve_variant_alias(StringView) { return {}; } Optional __attribute__((weak)) resolve_subdivision_alias(StringView) { return {}; } -void __attribute__((weak)) resolve_complex_language_aliases(LanguageID&) { } -Optional __attribute__((weak)) add_likely_subtags(LanguageID const&) { return {}; } +ErrorOr __attribute__((weak)) resolve_complex_language_aliases(LanguageID&) { return {}; } +ErrorOr> __attribute__((weak)) add_likely_subtags(LanguageID const&) { return OptionalNone {}; } -Optional remove_likely_subtags(LanguageID const& language_id) +ErrorOr> remove_likely_subtags(LanguageID const& language_id) { // https://www.unicode.org/reports/tr35/#Likely_Subtags auto return_language_and_variants = [](auto language, auto variants) { @@ -878,9 +885,9 @@ Optional remove_likely_subtags(LanguageID const& language_id) }; // 1. First get max = AddLikelySubtags(inputLocale). If an error is signaled, return it. - auto maximized = add_likely_subtags(language_id); + auto maximized = TRY(add_likely_subtags(language_id)); if (!maximized.has_value()) - return {}; + return OptionalNone {}; // 2. Remove the variants from max. auto variants = move(maximized->variants); @@ -892,38 +899,38 @@ Optional remove_likely_subtags(LanguageID const& language_id) // 4. Then for trial in {languagemax, languagemax_regionmax, languagemax_scriptmax}: // If AddLikelySubtags(trial) = max, then return trial + variants. - auto run_trial = [&](Optional language, Optional script, Optional region) -> Optional { + auto run_trial = [&](Optional language, Optional script, Optional region) -> ErrorOr> { LanguageID trial { .language = move(language), .script = move(script), .region = move(region) }; - if (add_likely_subtags(trial) == maximized) + if (TRY(add_likely_subtags(trial)) == maximized) return return_language_and_variants(move(trial), move(variants)); - return {}; + return OptionalNone {}; }; - if (auto trial = run_trial(language_max, {}, {}); trial.has_value()) + if (auto trial = TRY(run_trial(language_max, {}, {})); trial.has_value()) return trial; - if (auto trial = run_trial(language_max, {}, region_max); trial.has_value()) + if (auto trial = TRY(run_trial(language_max, {}, region_max)); trial.has_value()) return trial; - if (auto trial = run_trial(language_max, script_max, {}); trial.has_value()) + if (auto trial = TRY(run_trial(language_max, script_max, {})); trial.has_value()) return trial; // 5. If you do not get a match, return max + variants. return return_language_and_variants(maximized.release_value(), move(variants)); } -Optional __attribute__((weak)) resolve_most_likely_territory(LanguageID const&) { return {}; } +ErrorOr> __attribute__((weak)) resolve_most_likely_territory(LanguageID const&) { return OptionalNone {}; } -DeprecatedString resolve_most_likely_territory_alias(LanguageID const& language_id, StringView territory_alias) +ErrorOr resolve_most_likely_territory_alias(LanguageID const& language_id, StringView territory_alias) { auto aliases = territory_alias.split_view(' '); if (aliases.size() > 1) { - auto territory = resolve_most_likely_territory(language_id); + auto territory = TRY(resolve_most_likely_territory(language_id)); if (territory.has_value() && aliases.contains_slow(*territory)) return territory.release_value(); } - return aliases[0].to_deprecated_string(); + return String::from_utf8(aliases[0]); } ErrorOr LanguageID::to_string() const diff --git a/Userland/Libraries/LibLocale/Locale.h b/Userland/Libraries/LibLocale/Locale.h index e238f6b699a..52931633422 100644 --- a/Userland/Libraries/LibLocale/Locale.h +++ b/Userland/Libraries/LibLocale/Locale.h @@ -7,7 +7,6 @@ #pragma once #include -#include #include #include #include @@ -178,7 +177,7 @@ Vector get_keywords_for_locale(StringView locale, StringView key); Optional get_preferred_keyword_value_for_locale(StringView locale, StringView key); Optional get_locale_display_patterns(StringView locale); -Optional format_locale_for_display(StringView locale, LocaleID locale_id); +ErrorOr> format_locale_for_display(StringView locale, LocaleID locale_id); Optional get_locale_language_mapping(StringView locale, StringView language); Optional get_locale_territory_mapping(StringView locale, StringView territory); @@ -203,12 +202,12 @@ Optional resolve_territory_alias(StringView territory); Optional resolve_script_tag_alias(StringView script_tag); Optional resolve_variant_alias(StringView variant); Optional resolve_subdivision_alias(StringView subdivision); -void resolve_complex_language_aliases(LanguageID& language_id); +ErrorOr resolve_complex_language_aliases(LanguageID& language_id); -Optional add_likely_subtags(LanguageID const& language_id); -Optional remove_likely_subtags(LanguageID const& language_id); +ErrorOr> add_likely_subtags(LanguageID const& language_id); +ErrorOr> remove_likely_subtags(LanguageID const& language_id); -Optional resolve_most_likely_territory(LanguageID const& language_id); -DeprecatedString resolve_most_likely_territory_alias(LanguageID const& language_id, StringView territory_alias); +ErrorOr> resolve_most_likely_territory(LanguageID const& language_id); +ErrorOr resolve_most_likely_territory_alias(LanguageID const& language_id, StringView territory_alias); }