mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +00:00
LibJS+LibLocale: Port remaining locale APIs to String
This commit is contained in:
parent
d73a143004
commit
20536897e4
Notes:
sideshowbarker
2024-07-17 01:24:58 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/20536897e4 Pull-request: https://github.com/SerenityOS/serenity/pull/17122 Reviewed-by: https://github.com/Hendiadyoin1 Reviewed-by: https://github.com/nico
6 changed files with 58 additions and 50 deletions
|
@ -1679,7 +1679,7 @@ Optional<CharacterOrder> character_order_for_locale(StringView locale)
|
|||
return {};
|
||||
}
|
||||
|
||||
void resolve_complex_language_aliases(LanguageID& language_id)
|
||||
ErrorOr<void> 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<LanguageID> add_likely_subtags(LanguageID const& language_id)
|
||||
ErrorOr<Optional<LanguageID>> 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<LanguageID> 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<DeprecatedString> resolve_most_likely_territory(LanguageID const& language_id)
|
||||
ErrorOr<Optional<String>> 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 {};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -52,7 +52,7 @@ JS_DEFINE_NATIVE_FUNCTION(DisplayNamesPrototype::of)
|
|||
// 5. Let fields be displayNames.[[Fields]].
|
||||
// 6. If fields has a field [[<code>]], return fields.[[<code>]].
|
||||
Optional<StringView> result;
|
||||
Optional<DeprecatedString> formatted_result;
|
||||
Optional<String> 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);
|
||||
|
|
|
@ -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).
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -584,7 +584,7 @@ static ErrorOr<void> 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<void> 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<StringView> __attribute__((weak)) get_locale_short_date_field_mapping(S
|
|||
Optional<StringView> __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<DeprecatedString> format_locale_for_display(StringView locale, LocaleID locale_id)
|
||||
ErrorOr<Optional<String>> 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<StringView> script;
|
||||
|
@ -842,19 +842,26 @@ Optional<DeprecatedString> 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<DeprecatedString> secondary_tag;
|
||||
Optional<String> 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<ListPatterns> __attribute__((weak)) get_locale_list_patterns(StringView, StringView, Style) { return {}; }
|
||||
|
@ -866,10 +873,10 @@ Optional<StringView> __attribute__((weak)) resolve_territory_alias(StringView) {
|
|||
Optional<StringView> __attribute__((weak)) resolve_script_tag_alias(StringView) { return {}; }
|
||||
Optional<StringView> __attribute__((weak)) resolve_variant_alias(StringView) { return {}; }
|
||||
Optional<StringView> __attribute__((weak)) resolve_subdivision_alias(StringView) { return {}; }
|
||||
void __attribute__((weak)) resolve_complex_language_aliases(LanguageID&) { }
|
||||
Optional<LanguageID> __attribute__((weak)) add_likely_subtags(LanguageID const&) { return {}; }
|
||||
ErrorOr<void> __attribute__((weak)) resolve_complex_language_aliases(LanguageID&) { return {}; }
|
||||
ErrorOr<Optional<LanguageID>> __attribute__((weak)) add_likely_subtags(LanguageID const&) { return OptionalNone {}; }
|
||||
|
||||
Optional<LanguageID> remove_likely_subtags(LanguageID const& language_id)
|
||||
ErrorOr<Optional<LanguageID>> 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<LanguageID> 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<LanguageID> 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<String> language, Optional<String> script, Optional<String> region) -> Optional<LanguageID> {
|
||||
auto run_trial = [&](Optional<String> language, Optional<String> script, Optional<String> region) -> ErrorOr<Optional<LanguageID>> {
|
||||
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<DeprecatedString> __attribute__((weak)) resolve_most_likely_territory(LanguageID const&) { return {}; }
|
||||
ErrorOr<Optional<String>> __attribute__((weak)) resolve_most_likely_territory(LanguageID const&) { return OptionalNone {}; }
|
||||
|
||||
DeprecatedString resolve_most_likely_territory_alias(LanguageID const& language_id, StringView territory_alias)
|
||||
ErrorOr<String> 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<String> LanguageID::to_string() const
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/CharacterTypes.h>
|
||||
#include <AK/DeprecatedString.h>
|
||||
#include <AK/Error.h>
|
||||
#include <AK/Optional.h>
|
||||
#include <AK/String.h>
|
||||
|
@ -178,7 +177,7 @@ Vector<StringView> get_keywords_for_locale(StringView locale, StringView key);
|
|||
Optional<StringView> get_preferred_keyword_value_for_locale(StringView locale, StringView key);
|
||||
|
||||
Optional<DisplayPattern> get_locale_display_patterns(StringView locale);
|
||||
Optional<DeprecatedString> format_locale_for_display(StringView locale, LocaleID locale_id);
|
||||
ErrorOr<Optional<String>> format_locale_for_display(StringView locale, LocaleID locale_id);
|
||||
|
||||
Optional<StringView> get_locale_language_mapping(StringView locale, StringView language);
|
||||
Optional<StringView> get_locale_territory_mapping(StringView locale, StringView territory);
|
||||
|
@ -203,12 +202,12 @@ 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);
|
||||
void resolve_complex_language_aliases(LanguageID& language_id);
|
||||
ErrorOr<void> resolve_complex_language_aliases(LanguageID& language_id);
|
||||
|
||||
Optional<LanguageID> add_likely_subtags(LanguageID const& language_id);
|
||||
Optional<LanguageID> remove_likely_subtags(LanguageID const& language_id);
|
||||
ErrorOr<Optional<LanguageID>> add_likely_subtags(LanguageID const& language_id);
|
||||
ErrorOr<Optional<LanguageID>> remove_likely_subtags(LanguageID const& language_id);
|
||||
|
||||
Optional<DeprecatedString> resolve_most_likely_territory(LanguageID const& language_id);
|
||||
DeprecatedString resolve_most_likely_territory_alias(LanguageID const& language_id, StringView territory_alias);
|
||||
ErrorOr<Optional<String>> resolve_most_likely_territory(LanguageID const& language_id);
|
||||
ErrorOr<String> resolve_most_likely_territory_alias(LanguageID const& language_id, StringView territory_alias);
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue