Browse Source

LibJS+LibUnicode: Convert Intl.ListFormat to use Unicode::Style

Remove ListFormat's own definition of the Style enum, which was further
duplicated by a generated ListPatternStyle enum with the same values.
Timothy Flynn 3 năm trước cách đây
mục cha
commit
bced4e9324

+ 4 - 13
Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeLocale.cpp

@@ -131,7 +131,7 @@ struct AK::Formatter<ListPatterns> : Formatter<FormatString> {
     ErrorOr<void> format(FormatBuilder& builder, ListPatterns const& patterns)
     {
         return Formatter<FormatString>::format(builder,
-            "{{ ListPatternType::{}, ListPatternStyle::{}, {}, {}, {}, {} }}",
+            "{{ ListPatternType::{}, Style::{}, {}, {}, {}, {} }}",
             format_identifier({}, patterns.type),
             format_identifier({}, patterns.style),
             patterns.start,
@@ -217,7 +217,6 @@ struct UnicodeLocaleData {
     };
     Vector<String> keywords { "ca"sv, "nu"sv }; // FIXME: These should be parsed from BCP47. https://unicode-org.atlassian.net/browse/CLDR-15158
     Vector<String> list_pattern_types;
-    Vector<String> list_pattern_styles;
     HashMap<String, StringIndexType> language_aliases;
     HashMap<String, StringIndexType> territory_aliases;
     HashMap<String, StringIndexType> script_aliases;
@@ -507,8 +506,6 @@ static ErrorOr<void> parse_locale_list_patterns(String misc_path, UnicodeLocaleD
 
         if (!locale_data.list_pattern_types.contains_slow(type))
             locale_data.list_pattern_types.append(type);
-        if (!locale_data.list_pattern_styles.contains_slow(style))
-            locale_data.list_pattern_styles.append(style);
 
         ListPatterns list_pattern { type, style, start, middle, end, pair };
         list_patterns.append(locale_data.unique_list_patterns.ensure(move(list_pattern)));
@@ -953,7 +950,6 @@ namespace Unicode {
     generate_enum(generator, format_identifier, "Key"sv, {}, locale_data.keywords);
     generate_enum(generator, format_identifier, "Variant"sv, {}, locale_data.variants);
     generate_enum(generator, format_identifier, "ListPatternType"sv, {}, locale_data.list_pattern_types);
-    generate_enum(generator, format_identifier, "ListPatternStyle"sv, {}, locale_data.list_pattern_styles);
 
     generator.append(R"~~~(
 }
@@ -1003,7 +999,7 @@ struct DisplayPatternImpl {
 
 struct Patterns {
     ListPatternType type;
-    ListPatternStyle style;
+    Style style;
     @string_index_type@ start { 0 };
     @string_index_type@ middle { 0 };
     @string_index_type@ end { 0 };
@@ -1356,7 +1352,6 @@ Optional<StringView> get_locale_@enum_snake@_mapping(StringView locale, StringVi
     append_alias_search("subdivision"sv, locale_data.subdivision_aliases);
 
     append_from_string("ListPatternType"sv, "list_pattern_type"sv, locale_data.list_pattern_types);
-    append_from_string("ListPatternStyle"sv, "list_pattern_style"sv, locale_data.list_pattern_styles);
 
     generator.append(R"~~~(
 Optional<DisplayPattern> get_locale_display_patterns(StringView locale)
@@ -1372,7 +1367,7 @@ Optional<DisplayPattern> get_locale_display_patterns(StringView locale)
     return display_patterns.to_display_pattern();
 }
 
-Optional<ListPatterns> get_locale_list_patterns(StringView locale, StringView list_pattern_type, StringView list_pattern_style)
+Optional<ListPatterns> get_locale_list_patterns(StringView locale, StringView list_pattern_type, Style list_pattern_style)
 {
     auto locale_value = locale_from_string(locale);
     if (!locale_value.has_value())
@@ -1382,10 +1377,6 @@ Optional<ListPatterns> get_locale_list_patterns(StringView locale, StringView li
     if (!type_value.has_value())
         return {};
 
-    auto style_value = list_pattern_style_from_string(list_pattern_style);
-    if (!style_value.has_value())
-        return {};
-
     auto locale_index = to_underlying(*locale_value) - 1; // Subtract 1 because 0 == Locale::None.
 
     auto list_patterns_list_index = s_locale_list_patterns.at(locale_index);
@@ -1394,7 +1385,7 @@ Optional<ListPatterns> get_locale_list_patterns(StringView locale, StringView li
     for (auto list_patterns_index : locale_list_patterns) {
         auto const& list_patterns = s_list_patterns.at(list_patterns_index);
 
-        if ((list_patterns.type == type_value) && (list_patterns.style == style_value)) {
+        if ((list_patterns.type == type_value) && (list_patterns.style == list_pattern_style)) {
             auto const& start = s_string_list[list_patterns.start];
             auto const& middle = s_string_list[list_patterns.middle];
             auto const& end = s_string_list[list_patterns.end];

+ 1 - 29
Userland/Libraries/LibJS/Runtime/Intl/ListFormat.cpp

@@ -9,7 +9,6 @@
 #include <LibJS/Runtime/GlobalObject.h>
 #include <LibJS/Runtime/Intl/ListFormat.h>
 #include <LibJS/Runtime/IteratorOperations.h>
-#include <LibUnicode/Locale.h>
 
 namespace JS::Intl {
 
@@ -46,33 +45,6 @@ StringView ListFormat::type_string() const
     }
 }
 
-void ListFormat::set_style(StringView style)
-{
-    if (style == "narrow"sv) {
-        m_style = Style::Narrow;
-    } else if (style == "short"sv) {
-        m_style = Style::Short;
-    } else if (style == "long"sv) {
-        m_style = Style::Long;
-    } else {
-        VERIFY_NOT_REACHED();
-    }
-}
-
-StringView ListFormat::style_string() const
-{
-    switch (m_style) {
-    case Style::Narrow:
-        return "narrow"sv;
-    case Style::Short:
-        return "short"sv;
-    case Style::Long:
-        return "long"sv;
-    default:
-        VERIFY_NOT_REACHED();
-    }
-}
-
 // 13.1.1 DeconstructPattern ( pattern, placeables ), https://tc39.es/ecma402/#sec-deconstructpattern
 Vector<PatternPartition> deconstruct_pattern(StringView pattern, Placeables placeables)
 {
@@ -123,7 +95,7 @@ Vector<PatternPartition> deconstruct_pattern(StringView pattern, Placeables plac
 // 13.1.2 CreatePartsFromList ( listFormat, list ), https://tc39.es/ecma402/#sec-createpartsfromlist
 Vector<PatternPartition> create_parts_from_list(ListFormat const& list_format, Vector<String> const& list)
 {
-    auto list_patterns = Unicode::get_locale_list_patterns(list_format.locale(), list_format.type_string(), list_format.style_string());
+    auto list_patterns = Unicode::get_locale_list_patterns(list_format.locale(), list_format.type_string(), list_format.style());
     if (!list_patterns.has_value())
         return {};
 

+ 7 - 13
Userland/Libraries/LibJS/Runtime/Intl/ListFormat.h

@@ -13,6 +13,7 @@
 #include <AK/Vector.h>
 #include <LibJS/Runtime/Intl/AbstractOperations.h>
 #include <LibJS/Runtime/Object.h>
+#include <LibUnicode/Locale.h>
 
 namespace JS::Intl {
 
@@ -27,13 +28,6 @@ public:
         Unit,
     };
 
-    enum class Style {
-        Invalid,
-        Narrow,
-        Short,
-        Long,
-    };
-
     ListFormat(Object& prototype);
     virtual ~ListFormat() override = default;
 
@@ -44,14 +38,14 @@ public:
     void set_type(StringView type);
     StringView type_string() const;
 
-    Style style() const { return m_style; }
-    void set_style(StringView style);
-    StringView style_string() const;
+    Unicode::Style style() const { return m_style; }
+    void set_style(StringView style) { m_style = Unicode::style_from_string(style); }
+    StringView style_string() const { return Unicode::style_to_string(m_style); }
 
 private:
-    String m_locale;                  // [[Locale]]
-    Type m_type { Type::Invalid };    // [[Type]]
-    Style m_style { Style::Invalid }; // [[Style]]
+    String m_locale;                                 // [[Locale]]
+    Type m_type { Type::Invalid };                   // [[Type]]
+    Unicode::Style m_style { Unicode::Style::Long }; // [[Style]]
 };
 
 using Placeables = HashMap<StringView, Variant<PatternPartition, Vector<PatternPartition>>>;

+ 1 - 2
Userland/Libraries/LibUnicode/Locale.cpp

@@ -775,7 +775,6 @@ Optional<CalendarName> __attribute__((weak)) calendar_name_from_string(StringVie
 Optional<DateField> __attribute__((weak)) date_field_from_string(StringView) { return {}; }
 Optional<Key> __attribute__((weak)) key_from_string(StringView) { return {}; }
 Optional<ListPatternType> __attribute__((weak)) list_pattern_type_from_string(StringView) { return {}; }
-Optional<ListPatternStyle> __attribute__((weak)) list_pattern_style_from_string(StringView) { return {}; }
 Optional<DisplayPattern> __attribute__((weak)) get_locale_display_patterns(StringView) { return {}; }
 Optional<StringView> __attribute__((weak)) get_locale_language_mapping(StringView, StringView) { return {}; }
 Optional<StringView> __attribute__((weak)) get_locale_territory_mapping(StringView, StringView) { return {}; }
@@ -844,7 +843,7 @@ Vector<StringView> get_locale_key_mapping_list(StringView locale, StringView key
     return {};
 }
 
-Optional<ListPatterns> __attribute__((weak)) get_locale_list_patterns(StringView, StringView, StringView) { return {}; }
+Optional<ListPatterns> __attribute__((weak)) get_locale_list_patterns(StringView, StringView, Style) { return {}; }
 Optional<StringView> __attribute__((weak)) resolve_language_alias(StringView) { return {}; }
 Optional<StringView> __attribute__((weak)) resolve_territory_alias(StringView) { return {}; }
 Optional<StringView> __attribute__((weak)) resolve_script_tag_alias(StringView) { return {}; }

+ 1 - 2
Userland/Libraries/LibUnicode/Locale.h

@@ -157,7 +157,6 @@ Optional<CalendarName> calendar_name_from_string(StringView calendar);
 Optional<DateField> date_field_from_string(StringView calendar);
 Optional<Key> key_from_string(StringView key);
 Optional<ListPatternType> list_pattern_type_from_string(StringView list_pattern_type);
-Optional<ListPatternStyle> list_pattern_style_from_string(StringView list_pattern_style);
 
 Optional<DisplayPattern> get_locale_display_patterns(StringView locale);
 Optional<String> format_locale_for_display(StringView locale, LocaleID locale_id);
@@ -176,7 +175,7 @@ Optional<StringView> get_locale_narrow_date_field_mapping(StringView locale, Str
 Optional<StringView> get_locale_key_mapping(StringView locale, StringView keyword);
 Vector<StringView> get_locale_key_mapping_list(StringView locale, StringView keyword);
 
-Optional<ListPatterns> get_locale_list_patterns(StringView locale, StringView type, StringView style);
+Optional<ListPatterns> get_locale_list_patterns(StringView locale, StringView type, Style style);
 
 Optional<StringView> resolve_language_alias(StringView language);
 Optional<StringView> resolve_territory_alias(StringView territory);