mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:07:35 +00:00
LibUnicode: Parse and generate available candidate format patterns
These formats are used by ECMA-402 when neither a date nor time style is specified. In that case, these patterns are searched for a best match.
This commit is contained in:
parent
287d43f4be
commit
7872934861
3 changed files with 50 additions and 1 deletions
|
@ -38,6 +38,7 @@ struct Calendar {
|
||||||
CalendarFormat date_formats {};
|
CalendarFormat date_formats {};
|
||||||
CalendarFormat time_formats {};
|
CalendarFormat time_formats {};
|
||||||
CalendarFormat date_time_formats {};
|
CalendarFormat date_time_formats {};
|
||||||
|
Vector<CalendarPattern> available_formats {};
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Locale {
|
struct Locale {
|
||||||
|
@ -53,6 +54,8 @@ struct UnicodeLocaleData {
|
||||||
// FIXME: Aliases should come from BCP47. See: https://unicode-org.atlassian.net/browse/CLDR-15158
|
// FIXME: Aliases should come from BCP47. See: https://unicode-org.atlassian.net/browse/CLDR-15158
|
||||||
{ "gregorian"sv, "gregory"sv },
|
{ "gregorian"sv, "gregory"sv },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
size_t max_available_formats_size { 0 };
|
||||||
};
|
};
|
||||||
|
|
||||||
static void parse_date_time_pattern(CalendarPattern& format, String pattern, UnicodeLocaleData& locale_data)
|
static void parse_date_time_pattern(CalendarPattern& format, String pattern, UnicodeLocaleData& locale_data)
|
||||||
|
@ -114,6 +117,15 @@ static ErrorOr<void> parse_calendars(String locale_calendars_path, UnicodeLocale
|
||||||
|
|
||||||
auto const& date_time_formats_object = value.as_object().get("dateTimeFormats"sv);
|
auto const& date_time_formats_object = value.as_object().get("dateTimeFormats"sv);
|
||||||
parse_patterns(calendar.date_time_formats, date_time_formats_object.as_object());
|
parse_patterns(calendar.date_time_formats, date_time_formats_object.as_object());
|
||||||
|
|
||||||
|
auto const& available_formats = date_time_formats_object.as_object().get("availableFormats"sv);
|
||||||
|
available_formats.as_object().for_each_member([&](auto const&, JsonValue const& pattern) {
|
||||||
|
CalendarPattern format {};
|
||||||
|
parse_date_time_pattern(format, pattern.as_string(), locale_data);
|
||||||
|
calendar.available_formats.append(move(format));
|
||||||
|
});
|
||||||
|
|
||||||
|
locale_data.max_available_formats_size = max(locale_data.max_available_formats_size, calendar.available_formats.size());
|
||||||
});
|
});
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
|
@ -181,6 +193,7 @@ Optional<Calendar> calendar_from_string(StringView calendar);
|
||||||
Optional<Unicode::CalendarFormat> get_calendar_date_format(StringView locale, StringView calendar);
|
Optional<Unicode::CalendarFormat> get_calendar_date_format(StringView locale, StringView calendar);
|
||||||
Optional<Unicode::CalendarFormat> get_calendar_time_format(StringView locale, StringView calendar);
|
Optional<Unicode::CalendarFormat> get_calendar_time_format(StringView locale, StringView calendar);
|
||||||
Optional<Unicode::CalendarFormat> get_calendar_date_time_format(StringView locale, StringView calendar);
|
Optional<Unicode::CalendarFormat> get_calendar_date_time_format(StringView locale, StringView calendar);
|
||||||
|
Vector<Unicode::CalendarPattern> get_calendar_available_formats(StringView locale, StringView calendar);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -195,6 +208,7 @@ static void generate_unicode_locale_implementation(Core::File& file, UnicodeLoca
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
SourceGenerator generator { builder };
|
SourceGenerator generator { builder };
|
||||||
generator.set("string_index_type"sv, s_string_index_type);
|
generator.set("string_index_type"sv, s_string_index_type);
|
||||||
|
generator.set("available_formats_size"sv, String::number(locale_data.max_available_formats_size));
|
||||||
|
|
||||||
generator.append(R"~~~(
|
generator.append(R"~~~(
|
||||||
#include <AK/Array.h>
|
#include <AK/Array.h>
|
||||||
|
@ -242,6 +256,8 @@ struct CalendarData {
|
||||||
CalendarFormat date_formats {};
|
CalendarFormat date_formats {};
|
||||||
CalendarFormat time_formats {};
|
CalendarFormat time_formats {};
|
||||||
CalendarFormat date_time_formats {};
|
CalendarFormat date_time_formats {};
|
||||||
|
Array<CalendarPattern, @available_formats_size@> available_formats {};
|
||||||
|
size_t available_formats_size { 0 };
|
||||||
};
|
};
|
||||||
)~~~");
|
)~~~");
|
||||||
|
|
||||||
|
@ -281,7 +297,15 @@ static constexpr Array<CalendarData, @size@> @name@ { {)~~~");
|
||||||
append_calendar_format(calendar.time_formats);
|
append_calendar_format(calendar.time_formats);
|
||||||
generator.append(" ");
|
generator.append(" ");
|
||||||
append_calendar_format(calendar.date_time_formats);
|
append_calendar_format(calendar.date_time_formats);
|
||||||
generator.append(" },");
|
generator.append(" {{");
|
||||||
|
|
||||||
|
for (auto const& format : calendar.available_formats) {
|
||||||
|
generator.append(" ");
|
||||||
|
append_calendar_pattern(format);
|
||||||
|
}
|
||||||
|
|
||||||
|
generator.set("size", String::number(calendar.available_formats.size()));
|
||||||
|
generator.append(" }}, @size@ },");
|
||||||
}
|
}
|
||||||
|
|
||||||
generator.append(R"~~~(
|
generator.append(R"~~~(
|
||||||
|
@ -344,6 +368,20 @@ Optional<Unicode::CalendarFormat> get_calendar_date_time_format(StringView local
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Vector<Unicode::CalendarPattern> get_calendar_available_formats(StringView locale, StringView calendar)
|
||||||
|
{
|
||||||
|
Vector<Unicode::CalendarPattern> result {};
|
||||||
|
|
||||||
|
if (auto const* data = find_calendar_data(locale, calendar); data != nullptr) {
|
||||||
|
result.ensure_capacity(data->available_formats_size);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < data->available_formats_size; ++i)
|
||||||
|
result.unchecked_append(data->available_formats[i].to_unicode_calendar_pattern());
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
)~~~");
|
)~~~");
|
||||||
|
|
||||||
|
|
|
@ -63,4 +63,13 @@ Optional<CalendarFormat> get_calendar_format([[maybe_unused]] StringView locale,
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Vector<CalendarPattern> get_calendar_available_formats([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar)
|
||||||
|
{
|
||||||
|
#if ENABLE_UNICODE_DATA
|
||||||
|
return Detail::get_calendar_available_formats(locale, calendar);
|
||||||
|
#else
|
||||||
|
return {};
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
#include <AK/StringView.h>
|
#include <AK/StringView.h>
|
||||||
#include <AK/Types.h>
|
#include <AK/Types.h>
|
||||||
|
#include <AK/Vector.h>
|
||||||
#include <LibUnicode/Forward.h>
|
#include <LibUnicode/Forward.h>
|
||||||
|
|
||||||
namespace Unicode {
|
namespace Unicode {
|
||||||
|
@ -56,5 +57,6 @@ struct CalendarFormat {
|
||||||
CalendarPatternStyle calendar_pattern_style_from_string(StringView style);
|
CalendarPatternStyle calendar_pattern_style_from_string(StringView style);
|
||||||
StringView calendar_pattern_style_to_string(CalendarPatternStyle style);
|
StringView calendar_pattern_style_to_string(CalendarPatternStyle style);
|
||||||
Optional<CalendarFormat> get_calendar_format(StringView locale, StringView calendar, CalendarFormatType type);
|
Optional<CalendarFormat> get_calendar_format(StringView locale, StringView calendar, CalendarFormatType type);
|
||||||
|
Vector<CalendarPattern> get_calendar_available_formats(StringView locale, StringView calendar);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue