diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-08-10 09:04:47 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-08-11 13:11:01 +0200 |
commit | e6e462249fbc000d78eee27a2ed809d483ea9433 (patch) | |
tree | 06582213761d0c88a35f1cc3a17973c7d79ba494 /Userland | |
parent | 5ac23d244dc70971a9cdbb6c39f94a24f360fb91 (diff) | |
download | serenity-e6e462249fbc000d78eee27a2ed809d483ea9433.zip |
LibUnicode: Generate *_from_string methods using a hash map
Rather than a long series of string comparisons, generate each of these
methods using a hash map of the enumeration name to its value.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibUnicode/CodeGenerators/GenerateUnicodeData.cpp | 82 |
1 files changed, 27 insertions, 55 deletions
diff --git a/Userland/Libraries/LibUnicode/CodeGenerators/GenerateUnicodeData.cpp b/Userland/Libraries/LibUnicode/CodeGenerators/GenerateUnicodeData.cpp index c31e5f9c31..b1d03a2ab3 100644 --- a/Userland/Libraries/LibUnicode/CodeGenerators/GenerateUnicodeData.cpp +++ b/Userland/Libraries/LibUnicode/CodeGenerators/GenerateUnicodeData.cpp @@ -554,6 +554,7 @@ static void generate_unicode_data_implementation(Core::File& file, UnicodeData c #include <AK/BinarySearch.h> #include <AK/CharacterTypes.h> #include <AK/HashMap.h> +#include <AK/String.h> #include <AK/StringView.h> #include <LibUnicode/UnicodeData.h> @@ -774,74 +775,45 @@ bool code_point_has_@enum_snake@(u32 code_point, @enum_title@ @enum_snake@) )~~~"); }; - append_prop_search("GeneralCategory"sv, "general_category"sv, "s_general_categories"sv); + auto append_from_string = [&](StringView enum_title, StringView enum_snake, PropList const& prop_list, Vector<Alias> const& aliases) { + generator.set("enum_title", enum_title); + generator.set("enum_snake", enum_snake); - generator.append(R"~~~( -Optional<GeneralCategory> general_category_from_string(StringView const& general_category) -{)~~~"); + auto properties = prop_list.keys(); + for (auto const& alias : aliases) + properties.append(alias.alias); + quick_sort(properties); - for (auto const& general_category : unicode_data.general_categories) { - generator.set("general_category", general_category.key); - generator.append(R"~~~( - if (general_category == "@general_category@"sv) - return GeneralCategory::@general_category@;)~~~"); - } - for (auto const& alias : unicode_data.general_category_aliases) { - generator.set("general_category", alias.alias); generator.append(R"~~~( - if (general_category == "@general_category@"sv) - return GeneralCategory::@general_category@;)~~~"); - } - - generator.append(R"~~~( - return {}; -} -)~~~"); - - append_prop_search("Property"sv, "property"sv, "s_properties"sv); - - generator.append(R"~~~( -Optional<Property> property_from_string(StringView const& property) +Optional<@enum_title@> @enum_snake@_from_string(StringView const& @enum_snake@) { - if (property == "Assigned"sv) - return Property::Assigned;)~~~"); + static HashMap<String, @enum_title@> @enum_snake@_values { {)~~~"); + + for (auto const& property : properties) { + generator.set("property", property); + generator.append(R"~~~( + { "@property@"sv, @enum_title@::@property@ },)~~~"); + } - for (auto const& property : unicode_data.prop_list) { - generator.set("property", property.key); - generator.append(R"~~~( - if (property == "@property@"sv) - return Property::@property@;)~~~"); - } - for (auto const& alias : unicode_data.prop_aliases) { - generator.set("property", alias.alias); generator.append(R"~~~( - if (property == "@property@"sv) - return Property::@property@;)~~~"); - } + } }; - generator.append(R"~~~( + if (auto value = @enum_snake@_values.get(@enum_snake@); value.has_value()) + return value.value(); return {}; } +)~~~"); + }; -Optional<Script> script_from_string(StringView const& script) -{)~~~"); + append_prop_search("GeneralCategory"sv, "general_category"sv, "s_general_categories"sv); + append_from_string("GeneralCategory"sv, "general_category"sv, unicode_data.general_categories, unicode_data.general_category_aliases); - for (auto const& script : unicode_data.script_list) { - generator.set("script", script.key); - generator.append(R"~~~( - if (script == "@script@"sv) - return Script::@script@;)~~~"); - } - for (auto const& alias : unicode_data.script_aliases) { - generator.set("script", alias.alias); - generator.append(R"~~~( - if (script == "@script@"sv) - return Script::@script@;)~~~"); - } + append_prop_search("Property"sv, "property"sv, "s_properties"sv); + append_from_string("Property"sv, "property"sv, unicode_data.prop_list, unicode_data.prop_aliases); + + append_from_string("Script"sv, "script"sv, unicode_data.script_list, unicode_data.script_aliases); generator.append(R"~~~( - return {}; -} } |