diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-12-10 16:36:39 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-12-11 14:17:47 +0000 |
commit | 9cc323b0b0434e5e480b97fb53dff82639b37570 (patch) | |
tree | 8246ad790df7a4095526f51708a18d2c4380d4b5 /Meta/Lagom/Tools | |
parent | cdbfe01827b3fb5dd1a6ada63c0da64edd628304 (diff) | |
download | serenity-9cc323b0b0434e5e480b97fb53dff82639b37570.zip |
LibUnicode: Generate unique NumberFormat lists for each Unit
Diffstat (limited to 'Meta/Lagom/Tools')
-rw-r--r-- | Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeNumberFormat.cpp | 102 |
1 files changed, 40 insertions, 62 deletions
diff --git a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeNumberFormat.cpp b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeNumberFormat.cpp index 0ba7142c04..b5af991636 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeNumberFormat.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeNumberFormat.cpp @@ -157,9 +157,9 @@ struct NumberSystem { struct Unit { StringIndexType unit { 0 }; - Vector<NumberFormatIndexType> long_formats {}; - Vector<NumberFormatIndexType> short_formats {}; - Vector<NumberFormatIndexType> narrow_formats {}; + NumberFormatListIndexType long_formats { 0 }; + NumberFormatListIndexType short_formats { 0 }; + NumberFormatListIndexType narrow_formats { 0 }; }; struct Locale { @@ -487,11 +487,13 @@ static ErrorOr<void> parse_units(String locale_units_path, UnicodeLocaleData& lo return; } + auto& unit = ensure_unit(unit_name); + NumberFormatList formats; + value.as_object().for_each_member([&](auto const& unit_key, JsonValue const& pattern_value) { if (!unit_key.starts_with(unit_pattern_prefix)) return; - auto& unit = ensure_unit(unit_name); NumberFormat format {}; auto plurality = unit_key.substring_view(unit_pattern_prefix.length()); @@ -504,22 +506,24 @@ static ErrorOr<void> parse_units(String locale_units_path, UnicodeLocaleData& lo format.negative_format_index = locale_data.unique_strings.ensure(zero_format.replace("{number}"sv, "{minusSign}{number}"sv)); format.zero_format_index = locale_data.unique_strings.ensure(move(zero_format)); - auto format_index = locale_data.unique_formats.ensure(move(format)); - - switch (style) { - case Unicode::Style::Long: - unit.long_formats.append(format_index); - break; - case Unicode::Style::Short: - unit.short_formats.append(format_index); - break; - case Unicode::Style::Narrow: - unit.narrow_formats.append(format_index); - break; - default: - VERIFY_NOT_REACHED(); - } + formats.append(locale_data.unique_formats.ensure(move(format))); }); + + auto number_format_list_index = locale_data.unique_format_lists.ensure(move(formats)); + + switch (style) { + case Unicode::Style::Long: + unit.long_formats = number_format_list_index; + break; + case Unicode::Style::Short: + unit.short_formats = number_format_list_index; + break; + case Unicode::Style::Narrow: + unit.narrow_formats = number_format_list_index; + break; + default: + VERIFY_NOT_REACHED(); + } }); }; @@ -688,32 +692,15 @@ struct NumberSystem { struct Unit { @string_index_type@ unit { 0 }; - Span<@number_format_index_type@ const> long_formats {}; - Span<@number_format_index_type@ const> short_formats {}; - Span<@number_format_index_type@ const> narrow_formats {}; + @number_format_list_index_type@ long_formats { 0 }; + @number_format_list_index_type@ short_formats { 0 }; + @number_format_list_index_type@ narrow_formats { 0 }; }; )~~~"); locale_data.unique_formats.generate(generator, "NumberFormat"sv, "s_number_formats"sv, 10); locale_data.unique_format_lists.generate(generator, s_number_format_index_type, "s_number_format_lists"sv); - auto append_number_formats = [&](String name, auto const& number_formats) { - generator.set("name"sv, move(name)); - generator.set("size"sv, String::number(number_formats.size())); - - generator.append(R"~~~( -static constexpr Array<@number_format_index_type@, @size@> @name@ { {)~~~"); - - bool first = true; - for (auto number_format : number_formats) { - generator.append(first ? " " : ", "); - generator.append(String::number(number_format)); - first = false; - } - - generator.append(" } };"); - }; - auto append_number_systems = [&](String name, auto const& number_systems) { generator.set("name", name); generator.set("size", String::number(number_systems.size())); @@ -756,35 +743,25 @@ static constexpr Array<NumberSystem, @size@> @name@ { {)~~~"); }; auto append_units = [&](String name, auto const& units) { - auto format_name = [&](String unit, StringView format) { - unit = unit.replace("-"sv, "_"sv, true); - return String::formatted("{}_{}_{}", name, unit, format); - }; - - for (auto const& unit : units) { - append_number_formats(format_name(unit.key, "l"sv), unit.value.long_formats); - append_number_formats(format_name(unit.key, "s"sv), unit.value.short_formats); - append_number_formats(format_name(unit.key, "n"sv), unit.value.narrow_formats); - } - generator.set("name", name); generator.set("size", String::number(units.size())); generator.append(R"~~~( static constexpr Array<Unit, @size@> @name@ { {)~~~"); + bool first = true; for (auto const& unit : units) { generator.set("unit"sv, String::number(unit.value.unit)); - generator.set("long_formats"sv, format_name(unit.key, "l"sv)); - generator.set("short_formats"sv, format_name(unit.key, "s"sv)); - generator.set("narrow_formats"sv, format_name(unit.key, "n"sv)); - generator.append(R"~~~( - { @unit@, @long_formats@.span(), @short_formats@.span(), @narrow_formats@.span() },)~~~"); + generator.set("long_formats"sv, String::number(unit.value.long_formats)); + generator.set("short_formats"sv, String::number(unit.value.short_formats)); + generator.set("narrow_formats"sv, String::number(unit.value.narrow_formats)); + + generator.append(first ? " " : ", "); + generator.append("{ @unit@, @long_formats@, @short_formats@, @narrow_formats@ }"); + first = false; } - generator.append(R"~~~( -} }; -)~~~"); + generator.append(" } };"); }; generate_mapping(generator, locale_data.locales, "NumberSystem"sv, "s_number_systems"sv, "s_number_systems_{}", [&](auto const& name, auto const& value) { append_number_systems(name, value.number_systems); }); @@ -924,22 +901,23 @@ Vector<Unicode::NumberFormat> get_unit_formats(StringView locale, StringView uni Vector<Unicode::NumberFormat> formats; if (auto const* units = find_units(locale, unit); units != nullptr) { - Span<@number_format_index_type@ const> number_formats; + @number_format_list_index_type@ number_format_list_index { 0 }; switch (style) { case Style::Long: - number_formats = units->long_formats; + number_format_list_index = units->long_formats; break; case Style::Short: - number_formats = units->short_formats; + number_format_list_index = units->short_formats; break; case Style::Narrow: - number_formats = units->narrow_formats; + number_format_list_index = units->narrow_formats; break; default: VERIFY_NOT_REACHED(); } + auto number_formats = s_number_format_lists.at(number_format_list_index); formats.ensure_capacity(number_formats.size()); for (auto number_format : number_formats) |