LibUnicode: Generate enum/alias from-string methods without a HashMap

The *_from_string() and resolve_*_alias() generated methods are the last
remaining users of HashMap in the LibUnicode generated files (read: the
last methods not using compile-time structures). This converts these
methods to use an array containing pairs of hash values to the desired
lookup value.

Because this code generation is the same between GenerateUnicodeData.cpp
and GenerateUnicodeLocale.cpp, this adds a GeneratorUtil.h header to the
LibUnicode generators to contain the method that generates the methods.
This commit is contained in:
Timothy Flynn 2021-10-12 12:22:47 -04:00 committed by Andreas Kling
parent 203ee58aa2
commit f91d63af83
Notes: sideshowbarker 2024-07-18 02:47:08 +09:00
3 changed files with 120 additions and 80 deletions

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "GeneratorUtil.h"
#include <AK/AllOf.h>
#include <AK/Array.h>
#include <AK/CharacterTypes.h>
@ -560,7 +561,6 @@ static void generate_unicode_data_implementation(Core::File& file, UnicodeData c
#include <AK/Array.h>
#include <AK/BinarySearch.h>
#include <AK/CharacterTypes.h>
#include <AK/HashMap.h>
#include <AK/String.h>
#include <AK/StringView.h>
#include <LibUnicode/UnicodeData.h>
@ -810,33 +810,15 @@ bool code_point_has_@enum_snake@(u32 code_point, @enum_title@ @enum_snake@)
};
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);
HashValueMap<StringView> hashes;
hashes.ensure_capacity(prop_list.size() + aliases.size());
auto properties = prop_list.keys();
for (auto const& prop : prop_list)
hashes.set(prop.key.hash(), prop.key);
for (auto const& alias : aliases)
properties.append(alias.alias);
quick_sort(properties);
hashes.set(alias.alias.hash(), alias.alias);
generator.append(R"~~~(
Optional<@enum_title@> @enum_snake@_from_string(StringView const& @enum_snake@)
{
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@ },)~~~");
}
generator.append(R"~~~(
} };
if (auto value = @enum_snake@_values.get(@enum_snake@); value.has_value())
return value.value();
return {};
}
)~~~");
generate_value_from_string(generator, "{}_from_string"sv, enum_title, enum_snake, move(hashes));
};
append_prop_search("GeneralCategory"sv, "general_category"sv, "s_general_categories"sv);