Meta: Do not hard-code index types for UCD/CLDR/TZDB code generators

Hand-picking the smallest index type that fits a particular generated
array started with commit 3ad159537e. This
was to reduce the size of the generated library.

Since then, the number of types using UniqueStorage has grown a ton,
creating a long list of types for which index types are manually picked.
When a new UCD/CLDR/TZDB is released, and the current index type no
longer fits the generated data, we fail to generate. Tracking down which
index caused the failure is a pretty annoying process.

Instead, we can just use size_t while in the generators themselves, then
automatically pick the size needed for the generated code.
This commit is contained in:
Timothy Flynn 2022-11-18 11:04:33 -05:00 committed by Linus Groh
parent fa2579ffa9
commit b2164ad979
Notes: sideshowbarker 2024-07-17 07:31:31 +09:00
9 changed files with 268 additions and 379 deletions

View file

@ -27,54 +27,6 @@
#include <LibLocale/DateTimeFormat.h>
#include <LibTimeZone/TimeZone.h>
using StringIndexType = u16;
constexpr auto s_string_index_type = "u16"sv;
using CalendarPatternIndexType = u16;
constexpr auto s_calendar_pattern_index_type = "u16"sv;
using CalendarPatternListIndexType = u8;
constexpr auto s_calendar_pattern_list_index_type = "u8"sv;
using CalendarRangePatternIndexType = u16;
constexpr auto s_calendar_range_pattern_index_type = "u16"sv;
using CalendarRangePatternListIndexType = u16;
constexpr auto s_calendar_range_pattern_list_index_type = "u16"sv;
using CalendarFormatIndexType = u8;
constexpr auto s_calendar_format_index_type = "u8"sv;
using SymbolListIndexType = u16;
constexpr auto s_symbol_list_index_type = "u16"sv;
using CalendarSymbolsIndexType = u16;
constexpr auto s_calendar_symbols_index_type = "u16"sv;
using CalendarSymbolsListIndexType = u8;
constexpr auto s_calendar_symbols_list_index_type = "u8"sv;
using CalendarIndexType = u8;
constexpr auto s_calendar_index_type = "u8"sv;
using TimeZoneNamesIndexType = u16;
constexpr auto s_time_zone_index_type = "u16"sv;
using TimeZoneNamesListIndexType = u8;
constexpr auto s_time_zone_list_index_type = "u8"sv;
using TimeZoneFormatIndexType = u8;
constexpr auto s_time_zone_format_index_type = "u8"sv;
using DayPeriodIndexType = u8;
constexpr auto s_day_period_index_type = "u8"sv;
using DayPeriodListIndexType = u8;
constexpr auto s_day_period_list_index_type = "u8"sv;
using HourCycleListIndexType = u8;
constexpr auto s_hour_cycle_list_index_type = "u8"sv;
struct CalendarPattern : public Locale::CalendarPattern {
bool contains_only_date_fields() const
{
@ -131,9 +83,9 @@ struct CalendarPattern : public Locale::CalendarPattern {
&& (time_zone_name == other.time_zone_name);
}
StringIndexType skeleton_index { 0 };
StringIndexType pattern_index { 0 };
StringIndexType pattern12_index { 0 };
size_t skeleton_index { 0 };
size_t pattern_index { 0 };
size_t pattern12_index { 0 };
};
template<>
@ -196,9 +148,9 @@ struct CalendarRangePattern : public CalendarPattern {
}
Optional<Locale::CalendarRangePattern::Field> field {};
StringIndexType start_range { 0 };
StringIndexType separator { 0 };
StringIndexType end_range { 0 };
size_t start_range { 0 };
size_t separator { 0 };
size_t end_range { 0 };
};
template<>
@ -254,10 +206,10 @@ struct CalendarFormat {
&& (short_format == other.short_format);
}
CalendarPatternIndexType full_format { 0 };
CalendarPatternIndexType long_format { 0 };
CalendarPatternIndexType medium_format { 0 };
CalendarPatternIndexType short_format { 0 };
size_t full_format { 0 };
size_t long_format { 0 };
size_t medium_format { 0 };
size_t short_format { 0 };
};
template<>
@ -278,7 +230,7 @@ struct AK::Traits<CalendarFormat> : public GenericTraits<CalendarFormat> {
static unsigned hash(CalendarFormat const& c) { return c.hash(); }
};
using SymbolList = Vector<StringIndexType>;
using SymbolList = Vector<size_t>;
struct CalendarSymbols {
unsigned hash() const
@ -295,9 +247,9 @@ struct CalendarSymbols {
&& (long_symbols == other.long_symbols);
}
SymbolListIndexType narrow_symbols { 0 };
SymbolListIndexType short_symbols { 0 };
SymbolListIndexType long_symbols { 0 };
size_t narrow_symbols { 0 };
size_t short_symbols { 0 };
size_t long_symbols { 0 };
};
template<>
@ -317,9 +269,9 @@ struct AK::Traits<CalendarSymbols> : public GenericTraits<CalendarSymbols> {
static unsigned hash(CalendarSymbols const& c) { return c.hash(); }
};
using CalendarPatternList = Vector<CalendarPatternIndexType>;
using CalendarRangePatternList = Vector<CalendarRangePatternIndexType>;
using CalendarSymbolsList = Vector<CalendarSymbolsIndexType>;
using CalendarPatternList = Vector<size_t>;
using CalendarRangePatternList = Vector<size_t>;
using CalendarSymbolsList = Vector<size_t>;
struct Calendar {
unsigned hash() const
@ -347,16 +299,16 @@ struct Calendar {
&& (symbols == other.symbols);
}
CalendarFormatIndexType date_formats { 0 };
CalendarFormatIndexType time_formats { 0 };
CalendarFormatIndexType date_time_formats { 0 };
CalendarPatternListIndexType available_formats { 0 };
size_t date_formats { 0 };
size_t time_formats { 0 };
size_t date_time_formats { 0 };
size_t available_formats { 0 };
CalendarRangePatternIndexType default_range_format { 0 };
CalendarRangePatternListIndexType range_formats { 0 };
CalendarRangePatternListIndexType range12_formats { 0 };
size_t default_range_format { 0 };
size_t range_formats { 0 };
size_t range12_formats { 0 };
CalendarSymbolsListIndexType symbols { 0 };
size_t symbols { 0 };
};
template<>
@ -403,14 +355,14 @@ struct TimeZoneNames {
&& (long_generic_name == other.long_generic_name);
}
StringIndexType short_standard_name { 0 };
StringIndexType long_standard_name { 0 };
size_t short_standard_name { 0 };
size_t long_standard_name { 0 };
StringIndexType short_daylight_name { 0 };
StringIndexType long_daylight_name { 0 };
size_t short_daylight_name { 0 };
size_t long_daylight_name { 0 };
StringIndexType short_generic_name { 0 };
StringIndexType long_generic_name { 0 };
size_t short_generic_name { 0 };
size_t long_generic_name { 0 };
};
template<>
@ -455,14 +407,14 @@ struct TimeZoneFormat {
&& (gmt_zero_format == other.gmt_zero_format);
}
StringIndexType symbol_ahead_sign { 0 };
StringIndexType symbol_ahead_separator { 0 };
size_t symbol_ahead_sign { 0 };
size_t symbol_ahead_separator { 0 };
StringIndexType symbol_behind_sign { 0 };
StringIndexType symbol_behind_separator { 0 };
size_t symbol_behind_sign { 0 };
size_t symbol_behind_separator { 0 };
StringIndexType gmt_format { 0 };
StringIndexType gmt_zero_format { 0 };
size_t gmt_format { 0 };
size_t gmt_zero_format { 0 };
};
template<>
@ -522,8 +474,8 @@ struct AK::Traits<DayPeriod> : public GenericTraits<DayPeriod> {
static unsigned hash(DayPeriod const& d) { return d.hash(); }
};
using TimeZoneNamesList = Vector<TimeZoneNamesIndexType>;
using DayPeriodList = Vector<DayPeriodIndexType>;
using TimeZoneNamesList = Vector<size_t>;
using DayPeriodList = Vector<size_t>;
using HourCycleList = Vector<Locale::HourCycle>;
template<>
@ -535,35 +487,35 @@ struct AK::Formatter<Locale::HourCycle> : Formatter<FormatString> {
};
struct LocaleData {
HashMap<String, CalendarIndexType> calendars;
HashMap<String, size_t> calendars;
TimeZoneNamesListIndexType time_zones { 0 };
TimeZoneFormatIndexType time_zone_formats { 0 };
size_t time_zones { 0 };
size_t time_zone_formats { 0 };
DayPeriodListIndexType day_periods { 0 };
size_t day_periods { 0 };
};
struct CLDR {
UniqueStringStorage<StringIndexType> unique_strings;
UniqueStorage<CalendarPattern, CalendarPatternIndexType> unique_patterns;
UniqueStorage<CalendarPatternList, CalendarPatternListIndexType> unique_pattern_lists;
UniqueStorage<CalendarRangePattern, CalendarRangePatternIndexType> unique_range_patterns;
UniqueStorage<CalendarRangePatternList, CalendarRangePatternListIndexType> unique_range_pattern_lists;
UniqueStorage<CalendarFormat, CalendarFormatIndexType> unique_formats;
UniqueStorage<SymbolList, SymbolListIndexType> unique_symbol_lists;
UniqueStorage<CalendarSymbols, CalendarSymbolsIndexType> unique_calendar_symbols;
UniqueStorage<CalendarSymbolsList, CalendarSymbolsListIndexType> unique_calendar_symbols_lists;
UniqueStorage<Calendar, CalendarIndexType> unique_calendars;
UniqueStorage<TimeZoneNames, TimeZoneNamesIndexType> unique_time_zones;
UniqueStorage<TimeZoneNamesList, TimeZoneNamesListIndexType> unique_time_zone_lists;
UniqueStorage<TimeZoneFormat, TimeZoneFormatIndexType> unique_time_zone_formats;
UniqueStorage<DayPeriod, DayPeriodIndexType> unique_day_periods;
UniqueStorage<DayPeriodList, DayPeriodListIndexType> unique_day_period_lists;
UniqueStorage<HourCycleList, HourCycleListIndexType> unique_hour_cycle_lists;
UniqueStringStorage unique_strings;
UniqueStorage<CalendarPattern> unique_patterns;
UniqueStorage<CalendarPatternList> unique_pattern_lists;
UniqueStorage<CalendarRangePattern> unique_range_patterns;
UniqueStorage<CalendarRangePatternList> unique_range_pattern_lists;
UniqueStorage<CalendarFormat> unique_formats;
UniqueStorage<SymbolList> unique_symbol_lists;
UniqueStorage<CalendarSymbols> unique_calendar_symbols;
UniqueStorage<CalendarSymbolsList> unique_calendar_symbols_lists;
UniqueStorage<Calendar> unique_calendars;
UniqueStorage<TimeZoneNames> unique_time_zones;
UniqueStorage<TimeZoneNamesList> unique_time_zone_lists;
UniqueStorage<TimeZoneFormat> unique_time_zone_formats;
UniqueStorage<DayPeriod> unique_day_periods;
UniqueStorage<DayPeriodList> unique_day_period_lists;
UniqueStorage<HourCycleList> unique_hour_cycle_lists;
HashMap<String, LocaleData> locales;
HashMap<String, HourCycleListIndexType> hour_cycles;
HashMap<String, size_t> hour_cycles;
Vector<String> hour_cycle_regions;
HashMap<String, u8> minimum_days;
@ -1035,7 +987,7 @@ static Optional<CalendarPattern> parse_date_time_pattern_raw(String pattern, Str
return format;
}
static Optional<CalendarPatternIndexType> parse_date_time_pattern(String pattern, String skeleton, CLDR& cldr)
static Optional<size_t> parse_date_time_pattern(String pattern, String skeleton, CLDR& cldr)
{
auto format = parse_date_time_pattern_raw(move(pattern), move(skeleton), cldr);
if (!format.has_value())
@ -1241,7 +1193,7 @@ static void generate_missing_patterns(Calendar& calendar, CalendarPatternList& f
for (auto const& date_format : date_formats) {
auto const& date_time_formats = cldr.unique_formats.get(calendar.date_time_formats);
CalendarPatternIndexType date_time_format_index = 0;
size_t date_time_format_index = 0;
if (date_format.month == Locale::CalendarPatternStyle::Long) {
if (date_format.weekday.has_value())
@ -1534,7 +1486,7 @@ static ErrorOr<void> parse_time_zone_names(String locale_time_zone_names_path, C
if (meta_zone_object.is_null())
return {};
auto parse_name = [&](StringView type, JsonObject const& meta_zone_object, StringView key) -> Optional<StringIndexType> {
auto parse_name = [&](StringView type, JsonObject const& meta_zone_object, StringView key) -> Optional<size_t> {
auto const& names = meta_zone_object.get(type);
if (!names.is_object())
return {};
@ -1690,7 +1642,7 @@ static ErrorOr<void> parse_all_locales(String core_path, String dates_path, CLDR
auto dates_iterator = TRY(path_to_dir_iterator(move(dates_path)));
auto remove_variants_from_path = [&](String path) -> ErrorOr<String> {
auto parsed_locale = TRY(CanonicalLanguageID<StringIndexType>::parse(cldr.unique_strings, LexicalPath::basename(path)));
auto parsed_locale = TRY(CanonicalLanguageID::parse(cldr.unique_strings, LexicalPath::basename(path)));
StringBuilder builder;
builder.append(cldr.unique_strings.get(parsed_locale.language));
@ -1765,20 +1717,20 @@ static ErrorOr<void> generate_unicode_locale_implementation(Core::Stream::Buffer
{
StringBuilder builder;
SourceGenerator generator { builder };
generator.set("string_index_type"sv, s_string_index_type);
generator.set("calendar_pattern_index_type"sv, s_calendar_pattern_index_type);
generator.set("calendar_pattern_list_index_type"sv, s_calendar_pattern_list_index_type);
generator.set("calendar_range_pattern_index_type"sv, s_calendar_range_pattern_index_type);
generator.set("calendar_range_pattern_list_index_type"sv, s_calendar_range_pattern_list_index_type);
generator.set("calendar_format_index_type"sv, s_calendar_format_index_type);
generator.set("symbol_list_index_type"sv, s_symbol_list_index_type);
generator.set("calendar_symbols_index_type"sv, s_calendar_symbols_index_type);
generator.set("calendar_symbols_list_index_type"sv, s_calendar_symbols_list_index_type);
generator.set("calendar_index_type"sv, s_calendar_index_type);
generator.set("time_zone_index_type"sv, s_time_zone_index_type);
generator.set("time_zone_list_index_type"sv, s_time_zone_list_index_type);
generator.set("day_period_index_type"sv, s_day_period_index_type);
generator.set("day_period_list_index_type"sv, s_day_period_list_index_type);
generator.set("string_index_type"sv, cldr.unique_strings.type_that_fits());
generator.set("calendar_pattern_index_type"sv, cldr.unique_patterns.type_that_fits());
generator.set("calendar_pattern_list_index_type"sv, cldr.unique_pattern_lists.type_that_fits());
generator.set("calendar_range_pattern_index_type"sv, cldr.unique_range_patterns.type_that_fits());
generator.set("calendar_range_pattern_list_index_type"sv, cldr.unique_range_pattern_lists.type_that_fits());
generator.set("calendar_format_index_type"sv, cldr.unique_formats.type_that_fits());
generator.set("symbol_list_index_type"sv, cldr.unique_symbol_lists.type_that_fits());
generator.set("calendar_symbols_index_type"sv, cldr.unique_calendar_symbols.type_that_fits());
generator.set("calendar_symbols_list_index_type"sv, cldr.unique_calendar_symbols_lists.type_that_fits());
generator.set("calendar_index_type"sv, cldr.unique_calendars.type_that_fits());
generator.set("time_zone_index_type"sv, cldr.unique_time_zones.type_that_fits());
generator.set("time_zone_list_index_type"sv, cldr.unique_time_zone_lists.type_that_fits());
generator.set("day_period_index_type"sv, cldr.unique_day_periods.type_that_fits());
generator.set("day_period_list_index_type"sv, cldr.unique_day_period_lists.type_that_fits());
generator.append(R"~~~(
#include <AK/Array.h>
@ -1889,9 +1841,9 @@ struct CalendarRangePatternImpl {
)~~~");
cldr.unique_patterns.generate(generator, "CalendarPatternImpl"sv, "s_calendar_patterns"sv, 10);
cldr.unique_pattern_lists.generate(generator, s_calendar_pattern_index_type, "s_calendar_pattern_lists"sv);
cldr.unique_pattern_lists.generate(generator, cldr.unique_patterns.type_that_fits(), "s_calendar_pattern_lists"sv);
cldr.unique_range_patterns.generate(generator, "CalendarRangePatternImpl"sv, "s_calendar_range_patterns"sv, 10);
cldr.unique_range_pattern_lists.generate(generator, s_calendar_range_pattern_index_type, "s_calendar_range_pattern_lists"sv);
cldr.unique_range_pattern_lists.generate(generator, cldr.unique_range_patterns.type_that_fits(), "s_calendar_range_pattern_lists"sv);
generator.append(R"~~~(
struct CalendarFormatImpl {
@ -1974,16 +1926,16 @@ struct DayPeriodData {
)~~~");
cldr.unique_formats.generate(generator, "CalendarFormatImpl"sv, "s_calendar_formats"sv, 10);
cldr.unique_symbol_lists.generate(generator, s_string_index_type, "s_symbol_lists"sv);
cldr.unique_symbol_lists.generate(generator, cldr.unique_strings.type_that_fits(), "s_symbol_lists"sv);
cldr.unique_calendar_symbols.generate(generator, "CalendarSymbols"sv, "s_calendar_symbols"sv, 10);
cldr.unique_calendar_symbols_lists.generate(generator, s_calendar_symbols_index_type, "s_calendar_symbol_lists"sv);
cldr.unique_calendar_symbols_lists.generate(generator, cldr.unique_calendar_symbols.type_that_fits(), "s_calendar_symbol_lists"sv);
cldr.unique_calendars.generate(generator, "CalendarData"sv, "s_calendars"sv, 10);
cldr.unique_time_zones.generate(generator, "TimeZoneNames"sv, "s_time_zones"sv, 30);
cldr.unique_time_zone_lists.generate(generator, s_time_zone_index_type, "s_time_zone_lists"sv);
cldr.unique_time_zone_lists.generate(generator, cldr.unique_time_zones.type_that_fits(), "s_time_zone_lists"sv);
cldr.unique_time_zone_formats.generate(generator, "TimeZoneFormatImpl"sv, "s_time_zone_formats"sv, 30);
cldr.unique_day_periods.generate(generator, "DayPeriodData"sv, "s_day_periods"sv, 30);
cldr.unique_day_period_lists.generate(generator, s_day_period_index_type, "s_day_period_lists"sv);
cldr.unique_hour_cycle_lists.generate(generator, "u8"sv, "s_hour_cycle_lists"sv);
cldr.unique_day_period_lists.generate(generator, cldr.unique_day_periods.type_that_fits(), "s_day_period_lists"sv);
cldr.unique_hour_cycle_lists.generate(generator, cldr.unique_hour_cycle_lists.type_that_fits(), "s_hour_cycle_lists"sv);
auto append_calendars = [&](String name, auto const& calendars) {
generator.set("name", name);
@ -2028,11 +1980,11 @@ static constexpr Array<@type@, @size@> @name@ { {)~~~");
auto locales = cldr.locales.keys();
quick_sort(locales);
generate_mapping(generator, cldr.locales, s_calendar_index_type, "s_locale_calendars"sv, "s_calendars_{}"sv, format_identifier, [&](auto const& name, auto const& value) { append_calendars(name, value.calendars); });
append_mapping(locales, cldr.locales, s_time_zone_index_type, "s_locale_time_zones"sv, [](auto const& locale) { return locale.time_zones; });
append_mapping(locales, cldr.locales, s_time_zone_format_index_type, "s_locale_time_zone_formats"sv, [](auto const& locale) { return locale.time_zone_formats; });
append_mapping(locales, cldr.locales, s_day_period_index_type, "s_locale_day_periods"sv, [](auto const& locale) { return locale.day_periods; });
append_mapping(cldr.hour_cycle_regions, cldr.hour_cycles, s_hour_cycle_list_index_type, "s_hour_cycles"sv, [](auto const& hour_cycles) { return hour_cycles; });
generate_mapping(generator, cldr.locales, cldr.unique_calendars.type_that_fits(), "s_locale_calendars"sv, "s_calendars_{}"sv, format_identifier, [&](auto const& name, auto const& value) { append_calendars(name, value.calendars); });
append_mapping(locales, cldr.locales, cldr.unique_time_zones.type_that_fits(), "s_locale_time_zones"sv, [](auto const& locale) { return locale.time_zones; });
append_mapping(locales, cldr.locales, cldr.unique_time_zone_formats.type_that_fits(), "s_locale_time_zone_formats"sv, [](auto const& locale) { return locale.time_zone_formats; });
append_mapping(locales, cldr.locales, cldr.unique_day_periods.type_that_fits(), "s_locale_day_periods"sv, [](auto const& locale) { return locale.day_periods; });
append_mapping(cldr.hour_cycle_regions, cldr.hour_cycles, cldr.unique_hour_cycle_lists.type_that_fits(), "s_hour_cycles"sv, [](auto const& hour_cycles) { return hour_cycles; });
append_mapping(cldr.minimum_days_regions, cldr.minimum_days, "u8"sv, "s_minimum_days"sv, [](auto minimum_days) { return minimum_days; });
append_mapping(cldr.first_day_regions, cldr.first_day, "u8"sv, "s_first_day"sv, [](auto first_day) { return to_underlying(first_day); });
append_mapping(cldr.weekend_start_regions, cldr.weekend_start, "u8"sv, "s_weekend_start"sv, [](auto weekend_start) { return to_underlying(weekend_start); });

View file

@ -22,42 +22,6 @@
#include <LibCore/File.h>
#include <LibCore/Stream.h>
using StringIndexType = u32;
constexpr auto s_string_index_type = "u32"sv;
using DisplayPatternIndexType = u8;
constexpr auto s_display_pattern_index_type = "u8"sv;
using LanguageListIndexType = u8;
constexpr auto s_language_list_index_type = "u8"sv;
using TerritoryListIndexType = u8;
constexpr auto s_territory_list_index_type = "u8"sv;
using ScriptListIndexType = u8;
constexpr auto s_script_list_index_type = "u8"sv;
using CurrencyListIndexType = u16;
constexpr auto s_currency_list_index_type = "u16"sv;
using CalendarListIndexType = u8;
constexpr auto s_calendar_list_index_type = "u8"sv;
using DateFieldListIndexType = u16;
constexpr auto s_date_field_list_index_type = "u16"sv;
using KeywordListIndexType = u8;
constexpr auto s_keyword_list_index_type = "u8"sv;
using ListPatternIndexType = u16;
constexpr auto s_list_pattern_index_type = "u16"sv;
using ListPatternListIndexType = u8;
constexpr auto s_list_pattern_list_index_type = "u8"sv;
using TextLayoutIndexType = u8;
constexpr auto s_text_layout_index_type = "u8"sv;
static String format_identifier(StringView owner, String identifier)
{
identifier = identifier.replace("-"sv, "_"sv, ReplaceMode::All);
@ -81,8 +45,8 @@ struct DisplayPattern {
&& (locale_separator == other.locale_separator);
}
StringIndexType locale_pattern { 0 };
StringIndexType locale_separator { 0 };
size_t locale_pattern { 0 };
size_t locale_separator { 0 };
};
template<>
@ -124,10 +88,10 @@ struct ListPatterns {
StringView type;
StringView style;
StringIndexType start { 0 };
StringIndexType middle { 0 };
StringIndexType end { 0 };
StringIndexType pair { 0 };
size_t start { 0 };
size_t middle { 0 };
size_t end { 0 };
size_t pair { 0 };
};
template<>
@ -179,57 +143,57 @@ struct AK::Traits<TextLayout> : public GenericTraits<TextLayout> {
static unsigned hash(TextLayout const& t) { return t.hash(); }
};
using LanguageList = Vector<StringIndexType>;
using TerritoryList = Vector<StringIndexType>;
using ScriptList = Vector<StringIndexType>;
using CurrencyList = Vector<StringIndexType>;
using CalendarList = Vector<StringIndexType>;
using DateFieldList = Vector<StringIndexType>;
using KeywordList = Vector<StringIndexType>;
using ListPatternList = Vector<ListPatternIndexType>;
using LanguageList = Vector<size_t>;
using TerritoryList = Vector<size_t>;
using ScriptList = Vector<size_t>;
using CurrencyList = Vector<size_t>;
using CalendarList = Vector<size_t>;
using DateFieldList = Vector<size_t>;
using KeywordList = Vector<size_t>;
using ListPatternList = Vector<size_t>;
struct LocaleData {
String language;
Optional<String> territory;
Optional<String> variant;
DisplayPatternIndexType display_patterns { 0 };
LanguageListIndexType languages { 0 };
TerritoryListIndexType territories { 0 };
ScriptListIndexType scripts { 0 };
CurrencyListIndexType long_currencies { 0 };
CurrencyListIndexType short_currencies { 0 };
CurrencyListIndexType narrow_currencies { 0 };
CurrencyListIndexType numeric_currencies { 0 };
CalendarListIndexType calendars { 0 };
DateFieldListIndexType long_date_fields { 0 };
DateFieldListIndexType short_date_fields { 0 };
DateFieldListIndexType narrow_date_fields { 0 };
KeywordListIndexType calendar_keywords { 0 };
KeywordListIndexType collation_case_keywords { 0 };
KeywordListIndexType collation_numeric_keywords { 0 };
KeywordListIndexType number_system_keywords { 0 };
ListPatternListIndexType list_patterns { 0 };
TextLayoutIndexType text_layout { 0 };
size_t display_patterns { 0 };
size_t languages { 0 };
size_t territories { 0 };
size_t scripts { 0 };
size_t long_currencies { 0 };
size_t short_currencies { 0 };
size_t narrow_currencies { 0 };
size_t numeric_currencies { 0 };
size_t calendars { 0 };
size_t long_date_fields { 0 };
size_t short_date_fields { 0 };
size_t narrow_date_fields { 0 };
size_t calendar_keywords { 0 };
size_t collation_case_keywords { 0 };
size_t collation_numeric_keywords { 0 };
size_t number_system_keywords { 0 };
size_t list_patterns { 0 };
size_t text_layout { 0 };
};
struct LanguageMapping {
CanonicalLanguageID<StringIndexType> key {};
CanonicalLanguageID<StringIndexType> alias {};
CanonicalLanguageID key {};
CanonicalLanguageID alias {};
};
struct CLDR {
UniqueStringStorage<StringIndexType> unique_strings;
UniqueStorage<DisplayPattern, DisplayPatternIndexType> unique_display_patterns;
UniqueStorage<LanguageList, LanguageListIndexType> unique_language_lists;
UniqueStorage<TerritoryList, TerritoryListIndexType> unique_territory_lists;
UniqueStorage<ScriptList, ScriptListIndexType> unique_script_lists;
UniqueStorage<CurrencyList, CurrencyListIndexType> unique_currency_lists;
UniqueStorage<CalendarList, CalendarListIndexType> unique_calendar_lists;
UniqueStorage<DateFieldList, DateFieldListIndexType> unique_date_field_lists;
UniqueStorage<KeywordList, KeywordListIndexType> unique_keyword_lists;
UniqueStorage<ListPatterns, ListPatternIndexType> unique_list_patterns;
UniqueStorage<ListPatternList, ListPatternListIndexType> unique_list_pattern_lists;
UniqueStorage<TextLayout, TextLayoutIndexType> unique_text_layouts;
UniqueStringStorage unique_strings;
UniqueStorage<DisplayPattern> unique_display_patterns;
UniqueStorage<LanguageList> unique_language_lists;
UniqueStorage<TerritoryList> unique_territory_lists;
UniqueStorage<ScriptList> unique_script_lists;
UniqueStorage<CurrencyList> unique_currency_lists;
UniqueStorage<CalendarList> unique_calendar_lists;
UniqueStorage<DateFieldList> unique_date_field_lists;
UniqueStorage<KeywordList> unique_keyword_lists;
UniqueStorage<ListPatterns> unique_list_patterns;
UniqueStorage<ListPatternList> unique_list_pattern_lists;
UniqueStorage<TextLayout> unique_text_layouts;
HashMap<String, LocaleData> locales;
Vector<Alias> locale_aliases;
@ -254,11 +218,11 @@ struct CLDR {
Vector<String> list_pattern_types;
Vector<String> character_orders;
HashMap<String, StringIndexType> language_aliases;
HashMap<String, StringIndexType> territory_aliases;
HashMap<String, StringIndexType> script_aliases;
HashMap<String, StringIndexType> variant_aliases;
HashMap<String, StringIndexType> subdivision_aliases;
HashMap<String, size_t> language_aliases;
HashMap<String, size_t> territory_aliases;
HashMap<String, size_t> script_aliases;
HashMap<String, size_t> variant_aliases;
HashMap<String, size_t> subdivision_aliases;
Vector<LanguageMapping> complex_mappings;
Vector<LanguageMapping> likely_subtags;
size_t max_variant_size { 0 };
@ -276,8 +240,8 @@ struct CLDR {
static ErrorOr<LanguageMapping> parse_language_mapping(CLDR& cldr, StringView key, StringView alias)
{
auto parsed_key = TRY(CanonicalLanguageID<StringIndexType>::parse(cldr.unique_strings, key));
auto parsed_alias = TRY(CanonicalLanguageID<StringIndexType>::parse(cldr.unique_strings, alias));
auto parsed_key = TRY(CanonicalLanguageID::parse(cldr.unique_strings, key));
auto parsed_alias = TRY(CanonicalLanguageID::parse(cldr.unique_strings, alias));
return LanguageMapping { move(parsed_key), move(parsed_alias) };
}
@ -914,7 +878,7 @@ static ErrorOr<void> define_aliases_without_scripts(CLDR& cldr)
};
auto append_alias_without_script = [&](auto const& locale) -> ErrorOr<void> {
auto parsed_locale = TRY(CanonicalLanguageID<StringIndexType>::parse(cldr.unique_strings, locale));
auto parsed_locale = TRY(CanonicalLanguageID::parse(cldr.unique_strings, locale));
if ((parsed_locale.language == 0) || (parsed_locale.script == 0) || (parsed_locale.region == 0))
return {};
@ -961,7 +925,7 @@ static ErrorOr<void> parse_all_locales(String bcp47_path, String core_path, Stri
TRY(parse_likely_subtags(core_supplemental_path.string(), cldr));
auto remove_variants_from_path = [&](String path) -> ErrorOr<String> {
auto parsed_locale = TRY(CanonicalLanguageID<StringIndexType>::parse(cldr.unique_strings, LexicalPath::basename(path)));
auto parsed_locale = TRY(CanonicalLanguageID::parse(cldr.unique_strings, LexicalPath::basename(path)));
StringBuilder builder;
builder.append(cldr.unique_strings.get(parsed_locale.language));
@ -1088,9 +1052,11 @@ namespace Locale {
static ErrorOr<void> generate_unicode_locale_implementation(Core::Stream::BufferedFile& file, CLDR& cldr)
{
auto string_index_type = cldr.unique_strings.type_that_fits();
StringBuilder builder;
SourceGenerator generator { builder };
generator.set("string_index_type"sv, s_string_index_type);
generator.set("string_index_type"sv, string_index_type);
generator.set("locales_size"sv, String::number(cldr.locales.size()));
generator.set("territories_size", String::number(cldr.territories.size()));
generator.set("variants_size", String::number(cldr.max_variant_size));
@ -1184,15 +1150,15 @@ Span<StringView const> get_available_keyword_values(StringView key)
)~~~");
cldr.unique_display_patterns.generate(generator, "DisplayPatternImpl"sv, "s_display_patterns"sv, 30);
cldr.unique_language_lists.generate(generator, s_string_index_type, "s_language_lists"sv);
cldr.unique_territory_lists.generate(generator, s_string_index_type, "s_territory_lists"sv);
cldr.unique_script_lists.generate(generator, s_string_index_type, "s_script_lists"sv);
cldr.unique_currency_lists.generate(generator, s_string_index_type, "s_currency_lists"sv);
cldr.unique_calendar_lists.generate(generator, s_string_index_type, "s_calendar_lists"sv);
cldr.unique_date_field_lists.generate(generator, s_string_index_type, "s_date_field_lists"sv);
cldr.unique_keyword_lists.generate(generator, s_string_index_type, "s_keyword_lists"sv);
cldr.unique_language_lists.generate(generator, string_index_type, "s_language_lists"sv);
cldr.unique_territory_lists.generate(generator, string_index_type, "s_territory_lists"sv);
cldr.unique_script_lists.generate(generator, string_index_type, "s_script_lists"sv);
cldr.unique_currency_lists.generate(generator, string_index_type, "s_currency_lists"sv);
cldr.unique_calendar_lists.generate(generator, string_index_type, "s_calendar_lists"sv);
cldr.unique_date_field_lists.generate(generator, string_index_type, "s_date_field_lists"sv);
cldr.unique_keyword_lists.generate(generator, string_index_type, "s_keyword_lists"sv);
cldr.unique_list_patterns.generate(generator, "Patterns"sv, "s_list_patterns"sv, 10);
cldr.unique_list_pattern_lists.generate(generator, s_list_pattern_index_type, "s_list_pattern_lists"sv);
cldr.unique_list_pattern_lists.generate(generator, cldr.unique_list_patterns.type_that_fits(), "s_list_pattern_lists"sv);
cldr.unique_text_layouts.generate(generator, "TextLayout"sv, "s_text_layouts"sv, 30);
auto append_index = [&](auto index) {
@ -1239,24 +1205,24 @@ static constexpr Array<@type@, @size@> @name@ { {)~~~");
auto locales = cldr.locales.keys();
quick_sort(locales);
append_mapping(locales, cldr.locales, s_display_pattern_index_type, "s_locale_display_patterns"sv, [&](auto const& locale) { return locale.display_patterns; });
append_mapping(locales, cldr.locales, s_language_list_index_type, "s_languages"sv, [&](auto const& locale) { return locale.languages; });
append_mapping(locales, cldr.locales, s_territory_list_index_type, "s_territories"sv, [&](auto const& locale) { return locale.territories; });
append_mapping(locales, cldr.locales, s_script_list_index_type, "s_scripts"sv, [&](auto const& locale) { return locale.scripts; });
append_mapping(locales, cldr.locales, s_currency_list_index_type, "s_long_currencies"sv, [&](auto const& locale) { return locale.long_currencies; });
append_mapping(locales, cldr.locales, s_currency_list_index_type, "s_short_currencies"sv, [&](auto const& locale) { return locale.short_currencies; });
append_mapping(locales, cldr.locales, s_currency_list_index_type, "s_narrow_currencies"sv, [&](auto const& locale) { return locale.narrow_currencies; });
append_mapping(locales, cldr.locales, s_currency_list_index_type, "s_numeric_currencies"sv, [&](auto const& locale) { return locale.numeric_currencies; });
append_mapping(locales, cldr.locales, s_calendar_list_index_type, "s_calendars"sv, [&](auto const& locale) { return locale.calendars; });
append_mapping(locales, cldr.locales, s_date_field_list_index_type, "s_long_date_fields"sv, [&](auto const& locale) { return locale.long_date_fields; });
append_mapping(locales, cldr.locales, s_date_field_list_index_type, "s_short_date_fields"sv, [&](auto const& locale) { return locale.short_date_fields; });
append_mapping(locales, cldr.locales, s_date_field_list_index_type, "s_narrow_date_fields"sv, [&](auto const& locale) { return locale.narrow_date_fields; });
append_mapping(locales, cldr.locales, s_keyword_list_index_type, "s_calendar_keywords"sv, [&](auto const& locale) { return locale.calendar_keywords; });
append_mapping(locales, cldr.locales, s_keyword_list_index_type, "s_collation_case_keywords"sv, [&](auto const& locale) { return locale.collation_case_keywords; });
append_mapping(locales, cldr.locales, s_keyword_list_index_type, "s_collation_numeric_keywords"sv, [&](auto const& locale) { return locale.collation_numeric_keywords; });
append_mapping(locales, cldr.locales, s_keyword_list_index_type, "s_number_system_keywords"sv, [&](auto const& locale) { return locale.number_system_keywords; });
append_mapping(locales, cldr.locales, s_list_pattern_list_index_type, "s_locale_list_patterns"sv, [&](auto const& locale) { return locale.list_patterns; });
append_mapping(locales, cldr.locales, s_text_layout_index_type, "s_locale_text_layouts"sv, [&](auto const& locale) { return locale.text_layout; });
append_mapping(locales, cldr.locales, cldr.unique_display_patterns.type_that_fits(), "s_locale_display_patterns"sv, [&](auto const& locale) { return locale.display_patterns; });
append_mapping(locales, cldr.locales, cldr.unique_language_lists.type_that_fits(), "s_languages"sv, [&](auto const& locale) { return locale.languages; });
append_mapping(locales, cldr.locales, cldr.unique_territory_lists.type_that_fits(), "s_territories"sv, [&](auto const& locale) { return locale.territories; });
append_mapping(locales, cldr.locales, cldr.unique_script_lists.type_that_fits(), "s_scripts"sv, [&](auto const& locale) { return locale.scripts; });
append_mapping(locales, cldr.locales, cldr.unique_currency_lists.type_that_fits(), "s_long_currencies"sv, [&](auto const& locale) { return locale.long_currencies; });
append_mapping(locales, cldr.locales, cldr.unique_currency_lists.type_that_fits(), "s_short_currencies"sv, [&](auto const& locale) { return locale.short_currencies; });
append_mapping(locales, cldr.locales, cldr.unique_currency_lists.type_that_fits(), "s_narrow_currencies"sv, [&](auto const& locale) { return locale.narrow_currencies; });
append_mapping(locales, cldr.locales, cldr.unique_currency_lists.type_that_fits(), "s_numeric_currencies"sv, [&](auto const& locale) { return locale.numeric_currencies; });
append_mapping(locales, cldr.locales, cldr.unique_calendar_lists.type_that_fits(), "s_calendars"sv, [&](auto const& locale) { return locale.calendars; });
append_mapping(locales, cldr.locales, cldr.unique_date_field_lists.type_that_fits(), "s_long_date_fields"sv, [&](auto const& locale) { return locale.long_date_fields; });
append_mapping(locales, cldr.locales, cldr.unique_date_field_lists.type_that_fits(), "s_short_date_fields"sv, [&](auto const& locale) { return locale.short_date_fields; });
append_mapping(locales, cldr.locales, cldr.unique_date_field_lists.type_that_fits(), "s_narrow_date_fields"sv, [&](auto const& locale) { return locale.narrow_date_fields; });
append_mapping(locales, cldr.locales, cldr.unique_keyword_lists.type_that_fits(), "s_calendar_keywords"sv, [&](auto const& locale) { return locale.calendar_keywords; });
append_mapping(locales, cldr.locales, cldr.unique_keyword_lists.type_that_fits(), "s_collation_case_keywords"sv, [&](auto const& locale) { return locale.collation_case_keywords; });
append_mapping(locales, cldr.locales, cldr.unique_keyword_lists.type_that_fits(), "s_collation_numeric_keywords"sv, [&](auto const& locale) { return locale.collation_numeric_keywords; });
append_mapping(locales, cldr.locales, cldr.unique_keyword_lists.type_that_fits(), "s_number_system_keywords"sv, [&](auto const& locale) { return locale.number_system_keywords; });
append_mapping(locales, cldr.locales, cldr.unique_list_pattern_lists.type_that_fits(), "s_locale_list_patterns"sv, [&](auto const& locale) { return locale.list_patterns; });
append_mapping(locales, cldr.locales, cldr.unique_text_layouts.type_that_fits(), "s_locale_text_layouts"sv, [&](auto const& locale) { return locale.text_layout; });
generator.append(R"~~~(
@ -1485,7 +1451,7 @@ Optional<StringView> get_locale_@enum_snake@_mapping(StringView locale, StringVi
};
auto append_alias_search = [&](StringView enum_snake, auto const& aliases) {
HashValueMap<StringIndexType> hashes;
HashValueMap<size_t> hashes;
hashes.ensure_capacity(aliases.size());
for (auto const& alias : aliases)
@ -1495,7 +1461,7 @@ Optional<StringView> get_locale_@enum_snake@_mapping(StringView locale, StringVi
options.return_type = "StringView"sv;
options.return_format = "decode_string({})"sv;
generate_value_from_string(generator, "resolve_{}_alias"sv, s_string_index_type, enum_snake, move(hashes), options);
generate_value_from_string(generator, "resolve_{}_alias"sv, string_index_type, enum_snake, move(hashes), options);
};
append_from_string("Locale"sv, "locale"sv, cldr.locales.keys(), cldr.locale_aliases);

View file

@ -32,24 +32,6 @@
#include <LibLocale/PluralRules.h>
#include <math.h>
using StringIndexType = u16;
constexpr auto s_string_index_type = "u16"sv;
using NumberFormatIndexType = u16;
constexpr auto s_number_format_index_type = "u16"sv;
using NumberFormatListIndexType = u16;
constexpr auto s_number_format_list_index_type = "u16"sv;
using NumericSymbolListIndexType = u8;
constexpr auto s_numeric_symbol_list_index_type = "u8"sv;
using NumberSystemIndexType = u8;
constexpr auto s_number_system_index_type = "u8"sv;
using UnitIndexType = u16;
constexpr auto s_unit_index_type = "u16"sv;
enum class NumberFormatType {
Standard,
Compact,
@ -83,10 +65,10 @@ struct NumberFormat : public Locale::NumberFormat {
&& (identifier_indices == other.identifier_indices);
}
StringIndexType zero_format_index { 0 };
StringIndexType positive_format_index { 0 };
StringIndexType negative_format_index { 0 };
Vector<StringIndexType> identifier_indices {};
size_t zero_format_index { 0 };
size_t positive_format_index { 0 };
size_t negative_format_index { 0 };
Vector<size_t> identifier_indices {};
};
template<>
@ -113,8 +95,8 @@ struct AK::Traits<NumberFormat> : public GenericTraits<NumberFormat> {
static unsigned hash(NumberFormat const& f) { return f.hash(); }
};
using NumberFormatList = Vector<NumberFormatIndexType>;
using NumericSymbolList = Vector<StringIndexType>;
using NumberFormatList = Vector<size_t>;
using NumericSymbolList = Vector<size_t>;
struct NumberSystem {
unsigned hash() const
@ -150,22 +132,22 @@ struct NumberSystem {
&& (scientific_format == other.scientific_format);
}
NumericSymbolListIndexType symbols { 0 };
size_t symbols { 0 };
u8 primary_grouping_size { 0 };
u8 secondary_grouping_size { 0 };
NumberFormatIndexType decimal_format { 0 };
NumberFormatListIndexType decimal_long_formats { 0 };
NumberFormatListIndexType decimal_short_formats { 0 };
size_t decimal_format { 0 };
size_t decimal_long_formats { 0 };
size_t decimal_short_formats { 0 };
NumberFormatIndexType currency_format { 0 };
NumberFormatIndexType accounting_format { 0 };
NumberFormatListIndexType currency_unit_formats { 0 };
NumberFormatListIndexType currency_short_formats { 0 };
size_t currency_format { 0 };
size_t accounting_format { 0 };
size_t currency_unit_formats { 0 };
size_t currency_short_formats { 0 };
NumberFormatIndexType percent_format { 0 };
NumberFormatIndexType scientific_format { 0 };
size_t percent_format { 0 };
size_t scientific_format { 0 };
};
template<>
@ -212,10 +194,10 @@ struct Unit {
&& (narrow_formats == other.narrow_formats);
}
StringIndexType unit { 0 };
NumberFormatListIndexType long_formats { 0 };
NumberFormatListIndexType short_formats { 0 };
NumberFormatListIndexType narrow_formats { 0 };
size_t unit { 0 };
size_t long_formats { 0 };
size_t short_formats { 0 };
size_t narrow_formats { 0 };
};
template<>
@ -237,18 +219,18 @@ struct AK::Traits<Unit> : public GenericTraits<Unit> {
};
struct LocaleData {
Vector<NumberSystemIndexType> number_systems;
HashMap<String, UnitIndexType> units {};
Vector<size_t> number_systems;
HashMap<String, size_t> units {};
u8 minimum_grouping_digits { 0 };
};
struct CLDR {
UniqueStringStorage<StringIndexType> unique_strings;
UniqueStorage<NumberFormat, NumberFormatIndexType> unique_formats;
UniqueStorage<NumberFormatList, NumberFormatListIndexType> unique_format_lists;
UniqueStorage<NumericSymbolList, NumericSymbolListIndexType> unique_symbols;
UniqueStorage<NumberSystem, NumberSystemIndexType> unique_systems;
UniqueStorage<Unit, UnitIndexType> unique_units;
UniqueStringStorage unique_strings;
UniqueStorage<NumberFormat> unique_formats;
UniqueStorage<NumberFormatList> unique_format_lists;
UniqueStorage<NumericSymbolList> unique_symbols;
UniqueStorage<NumberSystem> unique_systems;
UniqueStorage<Unit> unique_units;
HashMap<String, Array<u32, 10>> number_system_digits;
Vector<String> number_systems;
@ -419,7 +401,7 @@ static void parse_number_pattern(Vector<String> patterns, CLDR& cldr, NumberForm
format.zero_format_index = cldr.unique_strings.ensure(move(zero_format));
}
static void parse_number_pattern(Vector<String> patterns, CLDR& cldr, NumberFormatType type, NumberFormatIndexType& format_index, NumberSystem* number_system_for_groupings = nullptr)
static void parse_number_pattern(Vector<String> patterns, CLDR& cldr, NumberFormatType type, size_t& format_index, NumberSystem* number_system_for_groupings = nullptr)
{
NumberFormat format {};
parse_number_pattern(move(patterns), cldr, type, format, number_system_for_groupings);
@ -453,7 +435,7 @@ static ErrorOr<void> parse_number_systems(String locale_numbers_path, CLDR& cldr
};
auto parse_number_format = [&](auto const& format_object) {
Vector<NumberFormatIndexType> result;
Vector<size_t> result;
result.ensure_capacity(format_object.size());
format_object.for_each_member([&](auto const& key, JsonValue const& value) {
@ -601,7 +583,7 @@ static ErrorOr<void> parse_number_systems(String locale_numbers_path, CLDR& cldr
locale.number_systems.ensure_capacity(number_systems.size());
for (auto& number_system : number_systems) {
NumberSystemIndexType system_index = 0;
size_t system_index = 0;
if (number_system.has_value())
system_index = cldr.unique_systems.ensure(number_system.release_value());
@ -727,7 +709,7 @@ static ErrorOr<void> parse_all_locales(String core_path, String numbers_path, St
TRY(parse_number_system_digits(core_supplemental_path.string(), cldr));
auto remove_variants_from_path = [&](String path) -> ErrorOr<String> {
auto parsed_locale = TRY(CanonicalLanguageID<StringIndexType>::parse(cldr.unique_strings, LexicalPath::basename(path)));
auto parsed_locale = TRY(CanonicalLanguageID::parse(cldr.unique_strings, LexicalPath::basename(path)));
StringBuilder builder;
builder.append(cldr.unique_strings.get(parsed_locale.language));
@ -790,10 +772,10 @@ static ErrorOr<void> generate_unicode_locale_implementation(Core::Stream::Buffer
{
StringBuilder builder;
SourceGenerator generator { builder };
generator.set("string_index_type"sv, s_string_index_type);
generator.set("number_format_index_type"sv, s_number_format_index_type);
generator.set("number_format_list_index_type"sv, s_number_format_list_index_type);
generator.set("numeric_symbol_list_index_type"sv, s_numeric_symbol_list_index_type);
generator.set("string_index_type"sv, cldr.unique_strings.type_that_fits());
generator.set("number_format_index_type"sv, cldr.unique_formats.type_that_fits());
generator.set("number_format_list_index_type"sv, cldr.unique_format_lists.type_that_fits());
generator.set("numeric_symbol_list_index_type"sv, cldr.unique_symbols.type_that_fits());
generator.set("identifier_count", String::number(cldr.max_identifier_count));
generator.append(R"~~~(
@ -870,8 +852,8 @@ struct Unit {
)~~~");
cldr.unique_formats.generate(generator, "NumberFormatImpl"sv, "s_number_formats"sv, 10);
cldr.unique_format_lists.generate(generator, s_number_format_index_type, "s_number_format_lists"sv);
cldr.unique_symbols.generate(generator, s_string_index_type, "s_numeric_symbol_lists"sv);
cldr.unique_format_lists.generate(generator, cldr.unique_formats.type_that_fits(), "s_number_format_lists"sv);
cldr.unique_symbols.generate(generator, cldr.unique_strings.type_that_fits(), "s_numeric_symbol_lists"sv);
cldr.unique_systems.generate(generator, "NumberSystemData"sv, "s_number_systems"sv, 10);
cldr.unique_units.generate(generator, "Unit"sv, "s_units"sv, 10);
@ -912,8 +894,8 @@ static constexpr Array<@type@, @size@> @name@ { {)~~~");
};
generate_mapping(generator, cldr.number_system_digits, "u32"sv, "s_number_systems_digits"sv, "s_number_systems_digits_{}"sv, nullptr, [&](auto const& name, auto const& value) { append_map(name, "u32"sv, value); });
generate_mapping(generator, cldr.locales, s_number_system_index_type, "s_locale_number_systems"sv, "s_number_systems_{}"sv, nullptr, [&](auto const& name, auto const& value) { append_map(name, s_number_system_index_type, value.number_systems); });
generate_mapping(generator, cldr.locales, s_unit_index_type, "s_locale_units"sv, "s_units_{}"sv, nullptr, [&](auto const& name, auto const& value) { append_map(name, s_unit_index_type, value.units); });
generate_mapping(generator, cldr.locales, cldr.unique_systems.type_that_fits(), "s_locale_number_systems"sv, "s_number_systems_{}"sv, nullptr, [&](auto const& name, auto const& value) { append_map(name, cldr.unique_systems.type_that_fits(), value.number_systems); });
generate_mapping(generator, cldr.locales, cldr.unique_units.type_that_fits(), "s_locale_units"sv, "s_units_{}"sv, nullptr, [&](auto const& name, auto const& value) { append_map(name, cldr.unique_units.type_that_fits(), value.units); });
generator.append(R"~~~(
static Optional<NumberSystem> keyword_to_number_system(KeywordNumbers keyword)

View file

@ -19,8 +19,6 @@
#include <LibCore/Stream.h>
#include <LibLocale/PluralRules.h>
using StringIndexType = u16;
static String format_identifier(StringView owner, String identifier)
{
identifier = identifier.replace("-"sv, "_"sv, ReplaceMode::All);
@ -214,7 +212,7 @@ struct LocaleData {
};
struct CLDR {
UniqueStringStorage<StringIndexType> unique_strings;
UniqueStringStorage unique_strings;
HashMap<String, LocaleData> locales;
};
@ -404,7 +402,7 @@ static ErrorOr<void> parse_all_locales(String core_path, String locale_names_pat
VERIFY(Core::File::is_directory(core_supplemental_path.string()));
auto remove_variants_from_path = [&](String path) -> ErrorOr<String> {
auto parsed_locale = TRY(CanonicalLanguageID<StringIndexType>::parse(cldr.unique_strings, LexicalPath::basename(path)));
auto parsed_locale = TRY(CanonicalLanguageID::parse(cldr.unique_strings, LexicalPath::basename(path)));
StringBuilder builder;
builder.append(cldr.unique_strings.get(parsed_locale.language));

View file

@ -20,12 +20,6 @@
#include <LibLocale/Locale.h>
#include <LibLocale/RelativeTimeFormat.h>
using StringIndexType = u16;
constexpr auto s_string_index_type = "u16"sv;
using RelativeTimeFormatIndexType = u16;
constexpr auto s_relative_time_format_index_type = "u16"sv;
struct RelativeTimeFormat {
unsigned hash() const
{
@ -49,8 +43,8 @@ struct RelativeTimeFormat {
String time_unit;
String style;
String plurality;
StringIndexType tense_or_number { 0 };
StringIndexType pattern { 0 };
size_t tense_or_number { 0 };
size_t pattern { 0 };
};
template<>
@ -73,12 +67,12 @@ struct AK::Traits<RelativeTimeFormat> : public GenericTraits<RelativeTimeFormat>
};
struct LocaleData {
Vector<RelativeTimeFormatIndexType> time_units;
Vector<size_t> time_units;
};
struct CLDR {
UniqueStringStorage<StringIndexType> unique_strings;
UniqueStorage<RelativeTimeFormat, RelativeTimeFormatIndexType> unique_formats;
UniqueStringStorage unique_strings;
UniqueStorage<RelativeTimeFormat> unique_formats;
HashMap<String, LocaleData> locales;
};
@ -147,7 +141,7 @@ static ErrorOr<void> parse_all_locales(String dates_path, CLDR& cldr)
auto dates_iterator = TRY(path_to_dir_iterator(move(dates_path)));
auto remove_variants_from_path = [&](String path) -> ErrorOr<String> {
auto parsed_locale = TRY(CanonicalLanguageID<StringIndexType>::parse(cldr.unique_strings, LexicalPath::basename(path)));
auto parsed_locale = TRY(CanonicalLanguageID::parse(cldr.unique_strings, LexicalPath::basename(path)));
StringBuilder builder;
builder.append(cldr.unique_strings.get(parsed_locale.language));
@ -195,8 +189,8 @@ static ErrorOr<void> generate_unicode_locale_implementation(Core::Stream::Buffer
{
StringBuilder builder;
SourceGenerator generator { builder };
generator.set("string_index_type"sv, s_string_index_type);
generator.set("relative_time_format_index_type"sv, s_relative_time_format_index_type);
generator.set("string_index_type"sv, cldr.unique_strings.type_that_fits());
generator.set("relative_time_format_index_type"sv, cldr.unique_formats.type_that_fits());
generator.append(R"~~~(
#include <AK/Array.h>
@ -250,7 +244,7 @@ static constexpr Array<@relative_time_format_index_type@, @size@> @name@ { {)~~~
generator.append(" } };");
};
generate_mapping(generator, cldr.locales, s_relative_time_format_index_type, "s_locale_relative_time_formats"sv, "s_number_systems_digits_{}"sv, nullptr, [&](auto const& name, auto const& value) { append_list(name, value.time_units); });
generate_mapping(generator, cldr.locales, cldr.unique_formats.type_that_fits(), "s_locale_relative_time_formats"sv, "s_number_systems_digits_{}"sv, nullptr, [&](auto const& name, auto const& value) { append_list(name, value.time_units); });
generator.append(R"~~~(
Vector<RelativeTimeFormat> get_relative_time_format_patterns(StringView locale, TimeUnit time_unit, StringView tense_or_number, Style style)

View file

@ -18,9 +18,6 @@
namespace {
using StringIndexType = u16;
constexpr auto s_string_index_type = "u16"sv;
struct DateTime {
u16 year { 0 };
Optional<u8> month;
@ -43,8 +40,8 @@ struct TimeZoneOffset {
Optional<i32> dst_rule_index;
i64 dst_offset { 0 };
StringIndexType standard_format { 0 };
StringIndexType daylight_format { 0 };
size_t standard_format { 0 };
size_t daylight_format { 0 };
};
struct DaylightSavingsOffset {
@ -53,11 +50,11 @@ struct DaylightSavingsOffset {
Optional<u16> year_to;
DateTime in_effect;
StringIndexType format { 0 };
size_t format { 0 };
};
struct TimeZoneData {
UniqueStringStorage<StringIndexType> unique_strings;
UniqueStringStorage unique_strings;
HashMap<String, Vector<TimeZoneOffset>> time_zones;
Vector<String> time_zone_names;
@ -68,7 +65,7 @@ struct TimeZoneData {
HashMap<String, TimeZone::Location> time_zone_coordinates;
HashMap<String, Vector<StringIndexType>> time_zone_regions;
HashMap<String, Vector<size_t>> time_zone_regions;
Vector<String> time_zone_region_names;
};
@ -479,7 +476,7 @@ static ErrorOr<void> generate_time_zone_data_implementation(Core::Stream::Buffer
{
StringBuilder builder;
SourceGenerator generator { builder };
generator.set("string_index_type"sv, s_string_index_type);
generator.set("string_index_type"sv, time_zone_data.unique_strings.type_that_fits());
set_dst_rule_indices(time_zone_data);
@ -578,7 +575,7 @@ static constexpr Array<@type@, @size@> @name@ { {
append_offsets(name, "DaylightSavingsOffset"sv, dst_offsets);
});
generate_mapping(generator, time_zone_data.time_zone_region_names, s_string_index_type, "s_regional_time_zones"sv, "s_regional_time_zones_{}"sv, format_identifier,
generate_mapping(generator, time_zone_data.time_zone_region_names, time_zone_data.unique_strings.type_that_fits(), "s_regional_time_zones"sv, "s_regional_time_zones_{}"sv, format_identifier,
[&](auto const& name, auto const& value) {
auto const& time_zones = time_zone_data.time_zone_regions.find(value)->value;

View file

@ -15,11 +15,8 @@
#include <LibCore/Stream.h>
#include <LibUnicode/Emoji.h>
using StringIndexType = u16;
constexpr auto s_string_index_type = "u16"sv;
struct Emoji {
StringIndexType name { 0 };
size_t name { 0 };
Optional<String> image_path;
Unicode::EmojiGroup group;
String subgroup;
@ -31,7 +28,7 @@ struct Emoji {
};
struct EmojiData {
UniqueStringStorage<StringIndexType> unique_strings;
UniqueStringStorage unique_strings;
Vector<Emoji> emojis;
};
@ -180,7 +177,7 @@ static ErrorOr<void> generate_emoji_data_implementation(Core::Stream::BufferedFi
StringBuilder builder;
SourceGenerator generator { builder };
generator.set("string_index_type"sv, s_string_index_type);
generator.set("string_index_type"sv, emoji_data.unique_strings.type_that_fits());
generator.set("emojis_size"sv, String::number(emoji_data.emojis.size()));
generator.append(R"~~~(

View file

@ -20,9 +20,6 @@
#include <LibCore/ArgsParser.h>
#include <LibCore/Stream.h>
using StringIndexType = u16;
constexpr auto s_string_index_type = "u16"sv;
// Some code points are excluded from UnicodeData.txt, and instead are part of a "range" of code
// points, as indicated by the "name" field. For example:
// 3400;<CJK Ideograph Extension A, First>;Lo;0;L;;;;;N;;;;;
@ -74,7 +71,7 @@ using NormalizationProps = HashMap<String, Vector<Normalization>>;
struct CodePointName {
CodePointRange code_point_range;
StringIndexType name { 0 };
size_t name { 0 };
};
// UnicodeData source: https://www.unicode.org/Public/13.0.0/ucd/UnicodeData.txt
@ -83,7 +80,7 @@ struct CodePointName {
struct CodePointData {
u32 code_point { 0 };
String name;
Optional<StringIndexType> abbreviation;
Optional<size_t> abbreviation;
u8 canonical_combining_class { 0 };
String bidi_class;
Optional<CodePointDecomposition> decomposition_mapping;
@ -101,11 +98,11 @@ struct CodePointData {
struct BlockName {
CodePointRange code_point_range;
StringIndexType name { 0 };
size_t name { 0 };
};
struct UnicodeData {
UniqueStringStorage<StringIndexType> unique_strings;
UniqueStringStorage unique_strings;
u32 code_points_with_non_zero_combining_class { 0 };
@ -125,8 +122,8 @@ struct UnicodeData {
Vector<CodePointData> code_point_data;
HashMap<u32, StringIndexType> code_point_abbreviations;
HashMap<u32, StringIndexType> code_point_display_name_aliases;
HashMap<u32, size_t> code_point_abbreviations;
HashMap<u32, size_t> code_point_display_name_aliases;
Vector<CodePointName> code_point_display_names;
PropList general_categories;
@ -795,7 +792,7 @@ static ErrorOr<void> generate_unicode_data_implementation(Core::Stream::Buffered
StringBuilder builder;
SourceGenerator generator { builder };
generator.set("string_index_type"sv, s_string_index_type);
generator.set("string_index_type"sv, unicode_data.unique_strings.type_that_fits());
generator.set("largest_special_casing_size", String::number(unicode_data.largest_special_casing_size));
generator.set("special_casing_size", String::number(unicode_data.special_casing.size()));
@ -947,7 +944,7 @@ static constexpr Array<@mapping_type@, @size@> s_@name@_mappings { {
generator.set("code_point", String::formatted("{:#x}", data.code_point));
generator.append("{ @code_point@");
if constexpr (IsSame<decltype(mapping), Optional<u32>> || IsSame<decltype(mapping), Optional<StringIndexType>>) {
if constexpr (IsSame<decltype(mapping), Optional<u32>> || IsSame<decltype(mapping), Optional<size_t>>) {
generator.set("mapping", String::formatted("{:#x}", *mapping));
generator.append(", @mapping@ },");
} else if constexpr (IsSame<decltype(mapping), Optional<CodePointDecomposition>>) {

View file

@ -51,10 +51,10 @@ struct AK::Traits<Vector<T>> : public GenericTraits<Vector<T>> {
}
};
template<typename StorageType, typename IndexType>
template<typename StorageType>
class UniqueStorage {
public:
IndexType ensure(StorageType value)
size_t ensure(StorageType value)
{
// We maintain a set of unique values in two structures: a vector which stores the values in
// the order they are added, and a hash map which maps that value to its index in the vector.
@ -68,17 +68,14 @@ public:
return *index;
m_storage.append(move(value));
size_t index = m_storage.size();
VERIFY(index < NumericLimits<IndexType>::max());
auto storage_index = static_cast<IndexType>(index);
auto storage_index = m_storage.size();
m_storage_indices.set(m_storage.last(), storage_index);
return storage_index;
}
StorageType const& get(IndexType index) const
StorageType const& get(size_t index) const
{
if (index == 0) {
static StorageType empty {};
@ -89,6 +86,17 @@ public:
return m_storage.at(index - 1);
}
StringView type_that_fits() const
{
if (m_storage.size() <= NumericLimits<u8>::max())
return "u8"sv;
if (m_storage.size() <= NumericLimits<u16>::max())
return "u16"sv;
if (m_storage.size() <= NumericLimits<u32>::max())
return "u32"sv;
return "u64"sv;
}
void generate(SourceGenerator& generator, StringView type, StringView name, size_t max_values_per_row) requires(!StorageTypeIsList<StorageType>)
{
generator.set("type"sv, type);
@ -177,13 +185,12 @@ static constexpr Array<Span<@type@ const>, @size@ + 1> @name@ { {
// clang-format gets confused by the requires() clauses above, and formats this section very weirdly.
protected:
Vector<StorageType> m_storage;
HashMap<StorageType, IndexType> m_storage_indices;
HashMap<StorageType, size_t> m_storage_indices;
// clang-format on
};
template<typename StringIndexType>
class UniqueStringStorage : public UniqueStorage<String, StringIndexType> {
using Base = UniqueStorage<String, StringIndexType>;
class UniqueStringStorage : public UniqueStorage<String> {
using Base = UniqueStorage<String>;
public:
// The goal of the string table generator is to ensure the table is located within the read-only
@ -275,9 +282,8 @@ struct Alias {
String alias;
};
template<typename StringIndexType>
struct CanonicalLanguageID {
static ErrorOr<CanonicalLanguageID> parse(UniqueStringStorage<StringIndexType>& unique_strings, StringView language)
static ErrorOr<CanonicalLanguageID> parse(UniqueStringStorage& unique_strings, StringView language)
{
CanonicalLanguageID language_id {};
@ -314,10 +320,10 @@ struct CanonicalLanguageID {
return language_id;
}
StringIndexType language { 0 };
StringIndexType script { 0 };
StringIndexType region { 0 };
Vector<StringIndexType> variants {};
size_t language { 0 };
size_t script { 0 };
size_t region { 0 };
Vector<size_t> variants {};
};
inline ErrorOr<NonnullOwnPtr<Core::Stream::BufferedFile>> open_file(StringView path, Core::Stream::OpenMode mode)