summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibUnicode/Locale.cpp
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2021-09-01 17:54:24 -0400
committerLinus Groh <mail@linusgroh.de>2021-09-02 17:56:42 +0100
commit21c4922ac08f98f99b242bfead03073247ce7c4b (patch)
tree1767d41bdb86761b9f62912bdce7aef6d2b7b8e2 /Userland/Libraries/LibUnicode/Locale.cpp
parenta05419db55c920fecd0a071ebc17f9722306dce5 (diff)
downloadserenity-21c4922ac08f98f99b242bfead03073247ce7c4b.zip
LibUnicode: Add helper methods to LocaleID and LanguageID for LibJS
Add a method to remove an extension type from the locale's extension set and methods to convert a locale and language to a string without canonicalization. Each of these will be used by LibJS.
Diffstat (limited to 'Userland/Libraries/LibUnicode/Locale.cpp')
-rw-r--r--Userland/Libraries/LibUnicode/Locale.cpp73
1 files changed, 73 insertions, 0 deletions
diff --git a/Userland/Libraries/LibUnicode/Locale.cpp b/Userland/Libraries/LibUnicode/Locale.cpp
index 8d3f3c87a9..4794523977 100644
--- a/Userland/Libraries/LibUnicode/Locale.cpp
+++ b/Userland/Libraries/LibUnicode/Locale.cpp
@@ -886,4 +886,77 @@ String resolve_most_likely_territory([[maybe_unused]] LanguageID const& language
return aliases[0].to_string();
}
+String LanguageID::to_string() const
+{
+ StringBuilder builder;
+
+ auto append_segment = [&](Optional<String> const& segment) {
+ if (!segment.has_value())
+ return;
+ if (!builder.is_empty())
+ builder.append('-');
+ builder.append(*segment);
+ };
+
+ append_segment(language);
+ append_segment(script);
+ append_segment(region);
+ for (auto const& variant : variants)
+ append_segment(variant);
+
+ return builder.build();
+}
+
+String LocaleID::to_string() const
+{
+ StringBuilder builder;
+
+ auto append_segment = [&](Optional<String> const& segment) {
+ if (!segment.has_value())
+ return;
+ if (!builder.is_empty())
+ builder.append('-');
+ builder.append(*segment);
+ };
+
+ auto append_key_value_list = [&](auto const& key, auto const& values) {
+ append_segment(key);
+ for (auto const& value : values)
+ append_segment(value);
+ };
+
+ append_segment(language_id.to_string());
+
+ for (auto const& extension : extensions) {
+ extension.visit(
+ [&](LocaleExtension const& ext) {
+ builder.append("-u"sv);
+ for (auto const& attribute : ext.attributes)
+ append_segment(attribute);
+ for (auto const& keyword : ext.keywords)
+ append_key_value_list(keyword.key, keyword.types);
+ },
+ [&](TransformedExtension const& ext) {
+ builder.append("-t"sv);
+ if (ext.language.has_value())
+ append_segment(ext.language->to_string());
+ for (auto const& field : ext.fields)
+ append_key_value_list(field.key, field.values);
+ },
+ [&](OtherExtension const& ext) {
+ builder.appendff("-{}", ext.key);
+ for (auto const& value : ext.values)
+ append_segment(value);
+ });
+ }
+
+ if (!private_use_extensions.is_empty()) {
+ builder.append("-x"sv);
+ for (auto const& extension : private_use_extensions)
+ append_segment(extension);
+ }
+
+ return builder.build();
+}
+
}