summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2022-01-25 11:27:02 -0500
committerLinus Groh <mail@linusgroh.de>2022-01-25 19:02:59 +0000
commite261132e8b875b3dfc1d618728706eb566c4577a (patch)
tree41cce186c38a59f54e88797ea8ebe4381632eaca
parent7f6edb7976cfd8501235bc8d6115b4663b8f8b8b (diff)
downloadserenity-e261132e8b875b3dfc1d618728706eb566c4577a.zip
LibUnicode: Add helper methods to convert a Style to and from a string
This conversion is duplicated a few times in our Intl implementation, so let's just define these once and be done with it.
-rw-r--r--Userland/Libraries/LibUnicode/Locale.cpp25
-rw-r--r--Userland/Libraries/LibUnicode/Locale.h3
2 files changed, 28 insertions, 0 deletions
diff --git a/Userland/Libraries/LibUnicode/Locale.cpp b/Userland/Libraries/LibUnicode/Locale.cpp
index d3c9b434e0..b34ef5b83b 100644
--- a/Userland/Libraries/LibUnicode/Locale.cpp
+++ b/Userland/Libraries/LibUnicode/Locale.cpp
@@ -741,6 +741,31 @@ bool is_locale_available(StringView locale)
return locale_from_string(locale).has_value();
}
+Style style_from_string(StringView style)
+{
+ if (style == "narrow"sv)
+ return Style::Narrow;
+ if (style == "short"sv)
+ return Style::Short;
+ if (style == "long"sv)
+ return Style::Long;
+ VERIFY_NOT_REACHED();
+}
+
+StringView style_to_string(Style style)
+{
+ switch (style) {
+ case Style::Narrow:
+ return "narrow"sv;
+ case Style::Short:
+ return "short"sv;
+ case Style::Long:
+ return "long"sv;
+ default:
+ VERIFY_NOT_REACHED();
+ }
+}
+
Optional<Locale> __attribute__((weak)) locale_from_string(StringView) { return {}; }
Optional<Language> __attribute__((weak)) language_from_string(StringView) { return {}; }
Optional<Territory> __attribute__((weak)) territory_from_string(StringView) { return {}; }
diff --git a/Userland/Libraries/LibUnicode/Locale.h b/Userland/Libraries/LibUnicode/Locale.h
index 6e715efe19..9fd15c63d6 100644
--- a/Userland/Libraries/LibUnicode/Locale.h
+++ b/Userland/Libraries/LibUnicode/Locale.h
@@ -145,6 +145,9 @@ Optional<String> canonicalize_unicode_locale_id(LocaleID&);
String const& default_locale();
bool is_locale_available(StringView locale);
+Style style_from_string(StringView style);
+StringView style_to_string(Style style);
+
Optional<Locale> locale_from_string(StringView locale);
Optional<Language> language_from_string(StringView language);
Optional<Territory> territory_from_string(StringView territory);