mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-08 12:19:37 +03:00
Lagom/CodeGenerators: Use HashMap::try_ensure_capacity
This commit is contained in:
parent
a3a5d2b27d
commit
6debd967ba
Notes:
sideshowbarker
2024-07-17 03:28:24 +09:00
Author: https://github.com/thomasqueirozb Commit: https://github.com/SerenityOS/serenity/commit/6debd967ba Pull-request: https://github.com/SerenityOS/serenity/pull/16334
@ -8,6 +8,7 @@
|
||||
#include <AK/AllOf.h>
|
||||
#include <AK/CharacterTypes.h>
|
||||
#include <AK/DeprecatedString.h>
|
||||
#include <AK/Error.h>
|
||||
#include <AK/Find.h>
|
||||
#include <AK/Format.h>
|
||||
#include <AK/GenericLexer.h>
|
||||
@ -1991,9 +1992,9 @@ static constexpr Array<@type@, @size@> @name@ { {)~~~");
|
||||
append_mapping(cldr.weekend_end_regions, cldr.weekend_end, "u8"sv, "s_weekend_end"sv, [](auto weekend_end) { return to_underlying(weekend_end); });
|
||||
generator.append("\n");
|
||||
|
||||
auto append_from_string = [&](StringView enum_title, StringView enum_snake, auto const& values, Vector<Alias> const& aliases = {}) {
|
||||
auto append_from_string = [&](StringView enum_title, StringView enum_snake, auto const& values, Vector<Alias> const& aliases = {}) -> ErrorOr<void> {
|
||||
HashValueMap<DeprecatedString> hashes;
|
||||
hashes.ensure_capacity(values.size());
|
||||
TRY(hashes.try_ensure_capacity(values.size()));
|
||||
|
||||
for (auto const& value : values)
|
||||
hashes.set(value.hash(), format_identifier(enum_title, value));
|
||||
@ -2001,13 +2002,15 @@ static constexpr Array<@type@, @size@> @name@ { {)~~~");
|
||||
hashes.set(alias.alias.hash(), format_identifier(enum_title, alias.alias));
|
||||
|
||||
generate_value_from_string(generator, "{}_from_string"sv, enum_title, enum_snake, move(hashes));
|
||||
|
||||
return {};
|
||||
};
|
||||
|
||||
append_from_string("HourCycleRegion"sv, "hour_cycle_region"sv, cldr.hour_cycle_regions);
|
||||
append_from_string("MinimumDaysRegion"sv, "minimum_days_region"sv, cldr.minimum_days_regions);
|
||||
append_from_string("FirstDayRegion"sv, "first_day_region"sv, cldr.first_day_regions);
|
||||
append_from_string("WeekendStartRegion"sv, "weekend_start_region"sv, cldr.weekend_start_regions);
|
||||
append_from_string("WeekendEndRegion"sv, "weekend_end_region"sv, cldr.weekend_end_regions);
|
||||
TRY(append_from_string("HourCycleRegion"sv, "hour_cycle_region"sv, cldr.hour_cycle_regions));
|
||||
TRY(append_from_string("MinimumDaysRegion"sv, "minimum_days_region"sv, cldr.minimum_days_regions));
|
||||
TRY(append_from_string("FirstDayRegion"sv, "first_day_region"sv, cldr.first_day_regions));
|
||||
TRY(append_from_string("WeekendStartRegion"sv, "weekend_start_region"sv, cldr.weekend_start_regions));
|
||||
TRY(append_from_string("WeekendEndRegion"sv, "weekend_end_region"sv, cldr.weekend_end_regions));
|
||||
|
||||
generator.append(R"~~~(
|
||||
static Optional<Calendar> keyword_to_calendar(KeywordCalendar keyword)
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <AK/AllOf.h>
|
||||
#include <AK/CharacterTypes.h>
|
||||
#include <AK/DeprecatedString.h>
|
||||
#include <AK/Error.h>
|
||||
#include <AK/Format.h>
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/JsonObject.h>
|
||||
@ -1438,9 +1439,9 @@ Optional<StringView> get_locale_@enum_snake@_mapping(StringView locale, StringVi
|
||||
)~~~");
|
||||
};
|
||||
|
||||
auto append_from_string = [&](StringView enum_title, StringView enum_snake, auto const& values, Vector<Alias> const& aliases = {}) {
|
||||
auto append_from_string = [&](StringView enum_title, StringView enum_snake, auto const& values, Vector<Alias> const& aliases = {}) -> ErrorOr<void> {
|
||||
HashValueMap<DeprecatedString> hashes;
|
||||
hashes.ensure_capacity(values.size());
|
||||
TRY(hashes.try_ensure_capacity(values.size()));
|
||||
|
||||
for (auto const& value : values)
|
||||
hashes.set(value.hash(), format_identifier(enum_title, value));
|
||||
@ -1448,11 +1449,13 @@ Optional<StringView> get_locale_@enum_snake@_mapping(StringView locale, StringVi
|
||||
hashes.set(alias.alias.hash(), format_identifier(enum_title, alias.alias));
|
||||
|
||||
generate_value_from_string(generator, "{}_from_string"sv, enum_title, enum_snake, move(hashes));
|
||||
|
||||
return {};
|
||||
};
|
||||
|
||||
auto append_alias_search = [&](StringView enum_snake, auto const& aliases) {
|
||||
auto append_alias_search = [&](StringView enum_snake, auto const& aliases) -> ErrorOr<void> {
|
||||
HashValueMap<size_t> hashes;
|
||||
hashes.ensure_capacity(aliases.size());
|
||||
TRY(hashes.try_ensure_capacity(aliases.size()));
|
||||
|
||||
for (auto const& alias : aliases)
|
||||
hashes.set(alias.key.hash(), alias.value);
|
||||
@ -1462,34 +1465,36 @@ Optional<StringView> get_locale_@enum_snake@_mapping(StringView locale, StringVi
|
||||
options.return_format = "decode_string({})"sv;
|
||||
|
||||
generate_value_from_string(generator, "resolve_{}_alias"sv, string_index_type, enum_snake, move(hashes), options);
|
||||
|
||||
return {};
|
||||
};
|
||||
|
||||
append_from_string("Locale"sv, "locale"sv, cldr.locales.keys(), cldr.locale_aliases);
|
||||
TRY(append_from_string("Locale"sv, "locale"sv, cldr.locales.keys(), cldr.locale_aliases));
|
||||
|
||||
append_from_string("Language"sv, "language"sv, cldr.languages);
|
||||
TRY(append_from_string("Language"sv, "language"sv, cldr.languages));
|
||||
append_mapping_search("language"sv, "language"sv, "s_languages"sv, "s_language_lists"sv);
|
||||
append_alias_search("language"sv, cldr.language_aliases);
|
||||
TRY(append_alias_search("language"sv, cldr.language_aliases));
|
||||
|
||||
append_from_string("Territory"sv, "territory"sv, cldr.territories);
|
||||
TRY(append_from_string("Territory"sv, "territory"sv, cldr.territories));
|
||||
append_mapping_search("territory"sv, "territory"sv, "s_territories"sv, "s_territory_lists"sv);
|
||||
append_alias_search("territory"sv, cldr.territory_aliases);
|
||||
TRY(append_alias_search("territory"sv, cldr.territory_aliases));
|
||||
|
||||
append_from_string("ScriptTag"sv, "script_tag"sv, cldr.scripts);
|
||||
TRY(append_from_string("ScriptTag"sv, "script_tag"sv, cldr.scripts));
|
||||
append_mapping_search("script"sv, "script_tag"sv, "s_scripts"sv, "s_script_lists"sv);
|
||||
append_alias_search("script_tag"sv, cldr.script_aliases);
|
||||
TRY(append_alias_search("script_tag"sv, cldr.script_aliases));
|
||||
|
||||
append_from_string("Currency"sv, "currency"sv, cldr.currencies);
|
||||
TRY(append_from_string("Currency"sv, "currency"sv, cldr.currencies));
|
||||
append_mapping_search("long_currency"sv, "currency"sv, "s_long_currencies"sv, "s_currency_lists"sv);
|
||||
append_mapping_search("short_currency"sv, "currency"sv, "s_short_currencies"sv, "s_currency_lists"sv);
|
||||
append_mapping_search("narrow_currency"sv, "currency"sv, "s_narrow_currencies"sv, "s_currency_lists"sv);
|
||||
append_mapping_search("numeric_currency"sv, "currency"sv, "s_numeric_currencies"sv, "s_currency_lists"sv);
|
||||
|
||||
append_from_string("DateField"sv, "date_field"sv, cldr.date_fields, cldr.date_field_aliases);
|
||||
TRY(append_from_string("DateField"sv, "date_field"sv, cldr.date_fields, cldr.date_field_aliases));
|
||||
append_mapping_search("long_date_field"sv, "date_field"sv, "s_long_date_fields"sv, "s_date_field_lists"sv);
|
||||
append_mapping_search("short_date_field"sv, "date_field"sv, "s_short_date_fields"sv, "s_date_field_lists"sv);
|
||||
append_mapping_search("narrow_date_field"sv, "date_field"sv, "s_narrow_date_fields"sv, "s_date_field_lists"sv);
|
||||
|
||||
append_from_string("Key"sv, "key"sv, cldr.keywords.keys());
|
||||
TRY(append_from_string("Key"sv, "key"sv, cldr.keywords.keys()));
|
||||
|
||||
for (auto const& keyword : cldr.keywords) {
|
||||
auto const& keyword_name = cldr.keyword_names.find(keyword.key)->value;
|
||||
@ -1497,19 +1502,19 @@ Optional<StringView> get_locale_@enum_snake@_mapping(StringView locale, StringVi
|
||||
auto enum_snake = DeprecatedString::formatted("keyword_{}", keyword.key);
|
||||
|
||||
if (auto aliases = cldr.keyword_aliases.find(keyword.key); aliases != cldr.keyword_aliases.end())
|
||||
append_from_string(enum_name, enum_snake, keyword.value, aliases->value);
|
||||
TRY(append_from_string(enum_name, enum_snake, keyword.value, aliases->value));
|
||||
else
|
||||
append_from_string(enum_name, enum_snake, keyword.value);
|
||||
TRY(append_from_string(enum_name, enum_snake, keyword.value));
|
||||
}
|
||||
|
||||
append_mapping_search("calendar"sv, "keyword_ca"sv, "s_calendars"sv, "s_calendar_lists"sv);
|
||||
|
||||
append_alias_search("variant"sv, cldr.variant_aliases);
|
||||
append_alias_search("subdivision"sv, cldr.subdivision_aliases);
|
||||
TRY(append_alias_search("variant"sv, cldr.variant_aliases));
|
||||
TRY(append_alias_search("subdivision"sv, cldr.subdivision_aliases));
|
||||
|
||||
append_from_string("ListPatternType"sv, "list_pattern_type"sv, cldr.list_pattern_types);
|
||||
TRY(append_from_string("ListPatternType"sv, "list_pattern_type"sv, cldr.list_pattern_types));
|
||||
|
||||
append_from_string("CharacterOrder"sv, "character_order"sv, cldr.character_orders);
|
||||
TRY(append_from_string("CharacterOrder"sv, "character_order"sv, cldr.character_orders));
|
||||
generate_value_to_string(generator, "{}_to_string"sv, "CharacterOrder"sv, "character_order"sv, format_identifier, cldr.character_orders);
|
||||
|
||||
generator.append(R"~~~(
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include "../LibUnicode/GeneratorUtil.h" // FIXME: Move this somewhere common.
|
||||
#include <AK/DateConstants.h>
|
||||
#include <AK/DeprecatedString.h>
|
||||
#include <AK/Error.h>
|
||||
#include <AK/Format.h>
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/SourceGenerator.h>
|
||||
@ -607,9 +608,9 @@ static constexpr Array<Location, @size@> s_time_zone_locations { {
|
||||
}
|
||||
generator.append("} };\n");
|
||||
|
||||
auto append_string_conversions = [&](StringView enum_title, StringView enum_snake, auto const& values, Vector<Alias> const& aliases = {}) {
|
||||
auto append_string_conversions = [&](StringView enum_title, StringView enum_snake, auto const& values, Vector<Alias> const& aliases = {}) -> ErrorOr<void> {
|
||||
HashValueMap<DeprecatedString> hashes;
|
||||
hashes.ensure_capacity(values.size());
|
||||
TRY(hashes.try_ensure_capacity(values.size()));
|
||||
|
||||
auto hash = [](auto const& value) {
|
||||
return CaseInsensitiveStringViewTraits::hash(value);
|
||||
@ -625,11 +626,13 @@ static constexpr Array<Location, @size@> s_time_zone_locations { {
|
||||
|
||||
generate_value_from_string(generator, "{}_from_string"sv, enum_title, enum_snake, move(hashes), options);
|
||||
generate_value_to_string(generator, "{}_to_string"sv, enum_title, enum_snake, format_identifier, values);
|
||||
|
||||
return {};
|
||||
};
|
||||
|
||||
append_string_conversions("TimeZone"sv, "time_zone"sv, time_zone_data.time_zone_names, time_zone_data.time_zone_aliases);
|
||||
append_string_conversions("DaylightSavingsRule"sv, "daylight_savings_rule"sv, time_zone_data.dst_offset_names);
|
||||
append_string_conversions("Region"sv, "region"sv, time_zone_data.time_zone_region_names);
|
||||
TRY(append_string_conversions("TimeZone"sv, "time_zone"sv, time_zone_data.time_zone_names, time_zone_data.time_zone_aliases));
|
||||
TRY(append_string_conversions("DaylightSavingsRule"sv, "daylight_savings_rule"sv, time_zone_data.dst_offset_names));
|
||||
TRY(append_string_conversions("Region"sv, "region"sv, time_zone_data.time_zone_region_names));
|
||||
|
||||
generator.append(R"~~~(
|
||||
static Array<DaylightSavingsOffset const*, 2> find_dst_offsets(TimeZoneOffset const& time_zone_offset, AK::Time time)
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include <AK/Array.h>
|
||||
#include <AK/CharacterTypes.h>
|
||||
#include <AK/DeprecatedString.h>
|
||||
#include <AK/Error.h>
|
||||
#include <AK/Find.h>
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/Optional.h>
|
||||
@ -1190,9 +1191,9 @@ bool code_point_has_@enum_snake@(u32 code_point, @enum_title@ @enum_snake@)
|
||||
)~~~");
|
||||
};
|
||||
|
||||
auto append_from_string = [&](StringView enum_title, StringView enum_snake, auto const& prop_list, Vector<Alias> const& aliases) {
|
||||
auto append_from_string = [&](StringView enum_title, StringView enum_snake, auto const& prop_list, Vector<Alias> const& aliases) -> ErrorOr<void> {
|
||||
HashValueMap<StringView> hashes;
|
||||
hashes.ensure_capacity(prop_list.size() + aliases.size());
|
||||
TRY(hashes.try_ensure_capacity(prop_list.size() + aliases.size()));
|
||||
|
||||
ValueFromStringOptions options {};
|
||||
|
||||
@ -1209,22 +1210,24 @@ bool code_point_has_@enum_snake@(u32 code_point, @enum_title@ @enum_snake@)
|
||||
hashes.set(alias.alias.hash(), alias.alias);
|
||||
|
||||
generate_value_from_string(generator, "{}_from_string"sv, enum_title, enum_snake, move(hashes), options);
|
||||
|
||||
return {};
|
||||
};
|
||||
|
||||
append_from_string("Locale"sv, "locale"sv, unicode_data.locales, {});
|
||||
TRY(append_from_string("Locale"sv, "locale"sv, unicode_data.locales, {}));
|
||||
|
||||
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);
|
||||
TRY(append_from_string("GeneralCategory"sv, "general_category"sv, unicode_data.general_categories, unicode_data.general_category_aliases));
|
||||
|
||||
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);
|
||||
TRY(append_from_string("Property"sv, "property"sv, unicode_data.prop_list, unicode_data.prop_aliases));
|
||||
|
||||
append_prop_search("Script"sv, "script"sv, "s_scripts"sv);
|
||||
append_prop_search("Script"sv, "script_extension"sv, "s_script_extensions"sv);
|
||||
append_from_string("Script"sv, "script"sv, unicode_data.script_list, unicode_data.script_aliases);
|
||||
TRY(append_from_string("Script"sv, "script"sv, unicode_data.script_list, unicode_data.script_aliases));
|
||||
|
||||
append_prop_search("Block"sv, "block"sv, "s_blocks"sv);
|
||||
append_from_string("Block"sv, "block"sv, unicode_data.block_list, unicode_data.block_aliases);
|
||||
TRY(append_from_string("Block"sv, "block"sv, unicode_data.block_list, unicode_data.block_aliases));
|
||||
|
||||
append_prop_search("GraphemeBreakProperty"sv, "grapheme_break_property"sv, "s_grapheme_break_properties"sv);
|
||||
append_prop_search("WordBreakProperty"sv, "word_break_property"sv, "s_word_break_properties"sv);
|
||||
|
Loading…
Reference in New Issue
Block a user