diff options
author | Timothy Flynn <trflynn89@pm.me> | 2022-01-10 12:19:58 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-01-11 00:36:45 +0100 |
commit | 6da1bfeeea3a3a5987e6c9228c5a9500bdce64b8 (patch) | |
tree | 6050fc43e6285a89345a2f1199f02c7d0088c669 | |
parent | 3dccaa39d8e1830ed2c0994b9ef07e6881d0865d (diff) | |
download | serenity-6da1bfeeea3a3a5987e6c9228c5a9500bdce64b8.zip |
Meta: Support generating case-insensitive value-from-string methods
This also extracts the default parameters for generate_value_from_string
to a structure. This is just to make it cleaner to add new options.
-rw-r--r-- | Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeLocale.cpp | 6 | ||||
-rw-r--r-- | Meta/Lagom/Tools/CodeGenerators/LibUnicode/GeneratorUtil.h | 26 |
2 files changed, 27 insertions, 5 deletions
diff --git a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeLocale.cpp b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeLocale.cpp index cd73032bda..23197e4a47 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeLocale.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeLocale.cpp @@ -1077,7 +1077,11 @@ Optional<StringView> get_locale_@enum_snake@_mapping(StringView locale, StringVi for (auto const& alias : aliases) hashes.set(alias.key.hash(), alias.value); - generate_value_from_string(generator, "resolve_{}_alias"sv, s_string_index_type, enum_snake, move(hashes), "StringView"sv, "s_string_list[{}]"sv); + ValueFromStringOptions options {}; + options.return_type = "StringView"sv; + options.return_format = "s_string_list[{}]"sv; + + generate_value_from_string(generator, "resolve_{}_alias"sv, s_string_index_type, enum_snake, move(hashes), options); }; append_from_string("Locale"sv, "locale"sv, locale_data.locales.keys(), locale_data.locale_aliases); diff --git a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GeneratorUtil.h b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GeneratorUtil.h index b1501a89f4..ead34e0c4b 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GeneratorUtil.h +++ b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GeneratorUtil.h @@ -293,15 +293,21 @@ struct HashValueComparator template<typename ValueType> using HashValueMap = HashMap<unsigned, ValueType>; +struct ValueFromStringOptions { + Optional<StringView> return_type {}; + StringView return_format { "{}"sv }; + CaseSensitivity sensitivity { CaseSensitivity::CaseSensitive }; +}; + template<typename ValueType> -void generate_value_from_string(SourceGenerator& generator, StringView method_name_format, StringView value_type, StringView value_name, HashValueMap<ValueType> hashes, Optional<StringView> return_type = {}, StringView return_format = "{}"sv) +void generate_value_from_string(SourceGenerator& generator, StringView method_name_format, StringView value_type, StringView value_name, HashValueMap<ValueType> hashes, ValueFromStringOptions options = {}) { ensure_from_string_types_are_generated(generator); generator.set("method_name", String::formatted(method_name_format, value_name)); generator.set("value_type", value_type); generator.set("value_name", value_name); - generator.set("return_type", return_type.has_value() ? *return_type : value_type); + generator.set("return_type", options.return_type.has_value() ? *options.return_type : value_type); generator.set("size", String::number(hashes.size())); generator.append(R"~~~( @@ -334,11 +340,23 @@ Optional<@return_type@> @method_name@(StringView key) } } - generator.set("return_statement", String::formatted(return_format, "value->value"sv)); + generator.set("return_statement", String::formatted(options.return_format, "value->value"sv)); generator.append(R"~~~( } }; +)~~~"); + + if (options.sensitivity == CaseSensitivity::CaseSensitive) { + generator.append(R"~~~( + auto hash = key.hash(); +)~~~"); + } else { + generator.append(R"~~~( + auto hash = CaseInsensitiveStringViewTraits::hash(key); +)~~~"); + } - if (auto const* value = binary_search(hash_pairs, key.hash(), nullptr, HashValueComparator<@value_type@> {})) + generator.append(R"~~~( + if (auto const* value = binary_search(hash_pairs, hash, nullptr, HashValueComparator<@value_type@> {})) return @return_statement@; return {}; } |