diff options
author | Timothy Flynn <trflynn89@pm.me> | 2023-01-15 10:04:26 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-01-16 10:12:37 +0000 |
commit | edfdade9e94342c1828e99c2c89dff32eaa4b070 (patch) | |
tree | 6da1389ac0897cdee4c1f834999ba3c2fdaa593d /Userland | |
parent | b6b5ddeb3b34e88f264bdea5d910c00851c00e8d (diff) | |
download | serenity-edfdade9e94342c1828e99c2c89dff32eaa4b070.zip |
LibLocale: Add a method to convert LanguageID to a String
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibLocale/Locale.cpp | 26 | ||||
-rw-r--r-- | Userland/Libraries/LibLocale/Locale.h | 3 |
2 files changed, 19 insertions, 10 deletions
diff --git a/Userland/Libraries/LibLocale/Locale.cpp b/Userland/Libraries/LibLocale/Locale.cpp index a45e4e9ec1..55df3ef85e 100644 --- a/Userland/Libraries/LibLocale/Locale.cpp +++ b/Userland/Libraries/LibLocale/Locale.cpp @@ -906,25 +906,31 @@ DeprecatedString resolve_most_likely_territory_alias(LanguageID const& language_ return aliases[0].to_deprecated_string(); } -DeprecatedString LanguageID::to_deprecated_string() const +ErrorOr<String> LanguageID::to_string() const { StringBuilder builder; - auto append_segment = [&](Optional<DeprecatedString> const& segment) { + auto append_segment = [&](Optional<DeprecatedString> const& segment) -> ErrorOr<void> { if (!segment.has_value()) - return; + return {}; if (!builder.is_empty()) - builder.append('-'); - builder.append(*segment); + TRY(builder.try_append('-')); + TRY(builder.try_append(*segment)); + return {}; }; - append_segment(language); - append_segment(script); - append_segment(region); + TRY(append_segment(language)); + TRY(append_segment(script)); + TRY(append_segment(region)); for (auto const& variant : variants) - append_segment(variant); + TRY(append_segment(variant)); - return builder.build(); + return builder.to_string(); +} + +DeprecatedString LanguageID::to_deprecated_string() const +{ + return MUST(to_string()).to_deprecated_string(); } DeprecatedString LocaleID::to_deprecated_string() const diff --git a/Userland/Libraries/LibLocale/Locale.h b/Userland/Libraries/LibLocale/Locale.h index db3fa2a1d9..f88d67ae07 100644 --- a/Userland/Libraries/LibLocale/Locale.h +++ b/Userland/Libraries/LibLocale/Locale.h @@ -8,7 +8,9 @@ #include <AK/CharacterTypes.h> #include <AK/DeprecatedString.h> +#include <AK/Error.h> #include <AK/Optional.h> +#include <AK/String.h> #include <AK/StringView.h> #include <AK/Variant.h> #include <AK/Vector.h> @@ -17,6 +19,7 @@ namespace Locale { struct LanguageID { + ErrorOr<String> to_string() const; DeprecatedString to_deprecated_string() const; bool operator==(LanguageID const&) const = default; |