mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-04 05:20:30 +00:00
LibUnicode: Parse and generate standard percentage formatting rules
This commit is contained in:
parent
4317a1b552
commit
feb8c22a62
Notes:
sideshowbarker
2024-07-18 01:15:31 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/feb8c22a627 Pull-request: https://github.com/SerenityOS/serenity/pull/10873 Reviewed-by: https://github.com/linusg
4 changed files with 48 additions and 5 deletions
|
@ -57,6 +57,7 @@ struct NumberSystem {
|
|||
Vector<NumberFormat> decimal_long_formats {};
|
||||
Vector<NumberFormat> decimal_short_formats {};
|
||||
Vector<NumberFormat> currency_short_formats {};
|
||||
NumberFormat percent_format {};
|
||||
};
|
||||
|
||||
struct ListPatterns {
|
||||
|
@ -558,6 +559,7 @@ static void parse_number_systems(String locale_numbers_path, UnicodeLocaleData&
|
|||
constexpr auto symbols_prefix = "symbols-numberSystem-"sv;
|
||||
constexpr auto decimal_formats_prefix = "decimalFormats-numberSystem-"sv;
|
||||
constexpr auto currency_formats_prefix = "currencyFormats-numberSystem-"sv;
|
||||
constexpr auto percent_formats_prefix = "percentFormats-numberSystem-"sv;
|
||||
|
||||
if (key.starts_with(symbols_prefix)) {
|
||||
auto system = key.substring(symbols_prefix.length());
|
||||
|
@ -587,6 +589,12 @@ static void parse_number_systems(String locale_numbers_path, UnicodeLocaleData&
|
|||
auto const& short_format = value.as_object().get("short"sv).as_object().get("standard"sv);
|
||||
number_system.currency_short_formats = parse_number_format(short_format.as_object());
|
||||
}
|
||||
} else if (key.starts_with(percent_formats_prefix)) {
|
||||
auto system = key.substring(decimal_formats_prefix.length());
|
||||
auto& number_system = ensure_number_system(system);
|
||||
|
||||
auto format_object = value.as_object().get("standard"sv);
|
||||
number_system.percent_format.format_index = ensure_unique_string(locale_data, format_object.as_string());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -813,6 +821,7 @@ Optional<StringView> get_locale_key_mapping(StringView locale, StringView key);
|
|||
Optional<Key> key_from_string(StringView key);
|
||||
|
||||
Optional<StringView> get_number_system_symbol(StringView locale, StringView system, StringView numeric_symbol);
|
||||
Optional<NumberFormat> get_standard_number_system_format(StringView locale, StringView system, StandardNumberFormatType type);
|
||||
Vector<NumberFormat> get_compact_number_system_formats(StringView locale, StringView system, CompactNumberFormatType type);
|
||||
Optional<NumericSymbol> numeric_symbol_from_string(StringView numeric_symbol);
|
||||
|
||||
|
@ -910,6 +919,7 @@ struct NumberSystem {
|
|||
Span<NumberFormat const> decimal_long_formats {};
|
||||
Span<NumberFormat const> decimal_short_formats {};
|
||||
Span<NumberFormat const> currency_short_formats {};
|
||||
NumberFormat percent_format {};
|
||||
};
|
||||
)~~~");
|
||||
|
||||
|
@ -970,6 +980,13 @@ static constexpr Array<@string_index_type@, @size@> @name@ { {
|
|||
)~~~");
|
||||
};
|
||||
|
||||
auto append_number_format = [&](auto const& number_format) {
|
||||
generator.set("magnitude"sv, String::number(number_format.magnitude));
|
||||
generator.set("plurality"sv, String::number(static_cast<u8>(number_format.plurality)));
|
||||
generator.set("format"sv, String::number(number_format.format_index));
|
||||
generator.append("{ @magnitude@, @plurality@, @format@ },");
|
||||
};
|
||||
|
||||
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()));
|
||||
|
@ -985,10 +1002,7 @@ static constexpr Array<NumberFormat, @size@> @name@ { {
|
|||
if (values_in_current_row++ > 0)
|
||||
generator.append(" ");
|
||||
|
||||
generator.set("magnitude"sv, String::number(number_format.magnitude));
|
||||
generator.set("plurality"sv, String::number(static_cast<u8>(number_format.plurality)));
|
||||
generator.set("format"sv, String::number(number_format.format_index));
|
||||
generator.append("{ @magnitude@, @plurality@, @format@ },");
|
||||
append_number_format(number_format);
|
||||
|
||||
if (values_in_current_row == max_values_per_row) {
|
||||
values_in_current_row = 0;
|
||||
|
@ -1032,7 +1046,9 @@ static constexpr Array<NumberSystem, @size@> @name@ { {)~~~");
|
|||
generator.append(" @index@,");
|
||||
}
|
||||
|
||||
generator.append(" }, @decimal_long_formats@.span(), @decimal_short_formats@.span(), @currency_short_formats@.span() },");
|
||||
generator.append(" }, @decimal_long_formats@.span(), @decimal_short_formats@.span(), @currency_short_formats@.span(), ");
|
||||
append_number_format(number_system.value.percent_format);
|
||||
generator.append(" },");
|
||||
}
|
||||
|
||||
generator.append(R"~~~(
|
||||
|
@ -1400,6 +1416,18 @@ Optional<StringView> get_number_system_symbol(StringView locale, StringView syst
|
|||
return {};
|
||||
}
|
||||
|
||||
Optional<Unicode::NumberFormat> get_standard_number_system_format(StringView locale, StringView system, StandardNumberFormatType type)
|
||||
{
|
||||
if (auto const* number_system = find_number_system(locale, system); number_system != nullptr) {
|
||||
switch (type) {
|
||||
case StandardNumberFormatType::Percent:
|
||||
return number_system->percent_format.to_unicode_number_format();
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
Vector<Unicode::NumberFormat> get_compact_number_system_formats(StringView locale, StringView system, CompactNumberFormatType type)
|
||||
{
|
||||
Vector<Unicode::NumberFormat> formats;
|
||||
|
|
|
@ -19,6 +19,7 @@ enum class ListPatternType : u8;
|
|||
enum class Locale : u16;
|
||||
enum class Property : u8;
|
||||
enum class Script : u8;
|
||||
enum class StandardNumberFormatType : u8;
|
||||
enum class Territory : u8;
|
||||
enum class WordBreakProperty : u8;
|
||||
|
||||
|
|
|
@ -821,6 +821,15 @@ Vector<NumberFormat> get_compact_number_system_formats([[maybe_unused]] StringVi
|
|||
#endif
|
||||
}
|
||||
|
||||
Optional<NumberFormat> get_standard_number_system_format([[maybe_unused]] StringView locale, [[maybe_unused]] StringView system, [[maybe_unused]] StandardNumberFormatType type)
|
||||
{
|
||||
#if ENABLE_UNICODE_DATA
|
||||
return Detail::get_standard_number_system_format(locale, system, type);
|
||||
#else
|
||||
return {};
|
||||
#endif
|
||||
}
|
||||
|
||||
Optional<ListPatterns> get_locale_list_patterns([[maybe_unused]] StringView locale, [[maybe_unused]] StringView type, [[maybe_unused]] StringView style)
|
||||
{
|
||||
#if ENABLE_UNICODE_DATA
|
||||
|
|
|
@ -78,6 +78,10 @@ struct LocaleID {
|
|||
Vector<String> private_use_extensions {};
|
||||
};
|
||||
|
||||
enum class StandardNumberFormatType : u8 {
|
||||
Percent,
|
||||
};
|
||||
|
||||
enum class CompactNumberFormatType : u8 {
|
||||
DecimalLong,
|
||||
DecimalShort,
|
||||
|
@ -163,6 +167,7 @@ Optional<StringView> get_locale_script_mapping(StringView locale, StringView scr
|
|||
Optional<StringView> get_locale_currency_mapping(StringView locale, StringView currency);
|
||||
Vector<StringView> get_locale_key_mapping(StringView locale, StringView keyword);
|
||||
Optional<StringView> get_number_system_symbol(StringView locale, StringView system, StringView symbol);
|
||||
Optional<NumberFormat> get_standard_number_system_format(StringView locale, StringView system, StandardNumberFormatType type);
|
||||
Vector<NumberFormat> get_compact_number_system_formats(StringView locale, StringView system, CompactNumberFormatType type);
|
||||
Optional<ListPatterns> get_locale_list_patterns(StringView locale, StringView type, StringView style);
|
||||
|
||||
|
|
Loading…
Reference in a new issue