mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 15:38:10 +00:00
LibJS+LibUnicode: Supply field type in CalendarPattern's for-each method
Some callers will want different behavior depending on what field is being provided to the callback.
This commit is contained in:
parent
80ea6e664d
commit
6ace4000bf
3 changed files with 32 additions and 18 deletions
|
@ -499,11 +499,11 @@ static void generate_missing_patterns(Calendar& calendar, Vector<CalendarPattern
|
||||||
format.pattern12_index = replace_pattern(format.pattern_index, time_format.pattern12_index, date_format.pattern_index);
|
format.pattern12_index = replace_pattern(format.pattern_index, time_format.pattern12_index, date_format.pattern_index);
|
||||||
format.pattern_index = replace_pattern(format.pattern_index, time_format.pattern_index, date_format.pattern_index);
|
format.pattern_index = replace_pattern(format.pattern_index, time_format.pattern_index, date_format.pattern_index);
|
||||||
|
|
||||||
format.for_each_calendar_field_zipped_with(date_format, [](auto& field, auto const& date_field) {
|
format.for_each_calendar_field_zipped_with(date_format, [](auto& field, auto const& date_field, auto) {
|
||||||
if (date_field.has_value())
|
if (date_field.has_value())
|
||||||
field = date_field;
|
field = date_field;
|
||||||
});
|
});
|
||||||
format.for_each_calendar_field_zipped_with(time_format, [](auto& field, auto const& time_field) {
|
format.for_each_calendar_field_zipped_with(time_format, [](auto& field, auto const& time_field, auto) {
|
||||||
if (time_field.has_value())
|
if (time_field.has_value())
|
||||||
field = time_field;
|
field = time_field;
|
||||||
});
|
});
|
||||||
|
|
|
@ -249,7 +249,7 @@ ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(GlobalObject& glo
|
||||||
return &date_time_format;
|
return &date_time_format;
|
||||||
|
|
||||||
// 38. For each row in Table 4, except the header row, in table order, do
|
// 38. For each row in Table 4, except the header row, in table order, do
|
||||||
date_time_format.for_each_calendar_field_zipped_with(*best_format, [&](auto& date_time_format_field, auto const& best_format_field) {
|
date_time_format.for_each_calendar_field_zipped_with(*best_format, [&](auto& date_time_format_field, auto const& best_format_field, auto) {
|
||||||
// a. Let prop be the name given in the Property column of the row.
|
// a. Let prop be the name given in the Property column of the row.
|
||||||
// b. If bestFormat has a field [[<prop>]], then
|
// b. If bestFormat has a field [[<prop>]], then
|
||||||
if (best_format_field.has_value()) {
|
if (best_format_field.has_value()) {
|
||||||
|
@ -490,12 +490,12 @@ Optional<Unicode::CalendarPattern> date_time_style_format(StringView data_locale
|
||||||
Unicode::CalendarPattern format {};
|
Unicode::CalendarPattern format {};
|
||||||
|
|
||||||
// b. Add to format all fields from dateFormat except [[pattern]] and [[rangePatterns]].
|
// b. Add to format all fields from dateFormat except [[pattern]] and [[rangePatterns]].
|
||||||
format.for_each_calendar_field_zipped_with(date_format, [](auto& format_field, auto const& date_format_field) {
|
format.for_each_calendar_field_zipped_with(date_format, [](auto& format_field, auto const& date_format_field, auto) {
|
||||||
format_field = date_format_field;
|
format_field = date_format_field;
|
||||||
});
|
});
|
||||||
|
|
||||||
// c. Add to format all fields from timeFormat except [[pattern]], [[rangePatterns]], [[pattern12]], and [[rangePatterns12]], if present.
|
// c. Add to format all fields from timeFormat except [[pattern]], [[rangePatterns]], [[pattern12]], and [[rangePatterns12]], if present.
|
||||||
format.for_each_calendar_field_zipped_with(time_format, [](auto& format_field, auto const& time_format_field) {
|
format.for_each_calendar_field_zipped_with(time_format, [](auto& format_field, auto const& time_format_field, auto) {
|
||||||
if (time_format_field.has_value())
|
if (time_format_field.has_value())
|
||||||
format_field = time_format_field;
|
format_field = time_format_field;
|
||||||
});
|
});
|
||||||
|
@ -577,7 +577,7 @@ Optional<Unicode::CalendarPattern> basic_format_matcher(Unicode::CalendarPattern
|
||||||
int score = 0;
|
int score = 0;
|
||||||
|
|
||||||
// b. For each property name property shown in Table 4, do
|
// b. For each property name property shown in Table 4, do
|
||||||
format.for_each_calendar_field_zipped_with(options, [&](auto const& format_prop, auto const& options_prop) {
|
format.for_each_calendar_field_zipped_with(options, [&](auto const& format_prop, auto const& options_prop, auto) {
|
||||||
using ValueType = typename RemoveReference<decltype(options_prop)>::ValueType;
|
using ValueType = typename RemoveReference<decltype(options_prop)>::ValueType;
|
||||||
|
|
||||||
// i. If options has a field [[<property>]], let optionsProp be options.[[<property>]]; else let optionsProp be undefined.
|
// i. If options has a field [[<property>]], let optionsProp be options.[[<property>]]; else let optionsProp be undefined.
|
||||||
|
@ -659,7 +659,7 @@ Optional<Unicode::CalendarPattern> basic_format_matcher(Unicode::CalendarPattern
|
||||||
//
|
//
|
||||||
// Rather than generating an prohibitively large amount of nearly-duplicate patterns, which only
|
// Rather than generating an prohibitively large amount of nearly-duplicate patterns, which only
|
||||||
// differ by field length, we expand the field lengths here.
|
// differ by field length, we expand the field lengths here.
|
||||||
best_format->for_each_calendar_field_zipped_with(options, [](auto& best_format_field, auto const& option_field) {
|
best_format->for_each_calendar_field_zipped_with(options, [](auto& best_format_field, auto const& option_field, auto) {
|
||||||
if (best_format_field.has_value() && option_field.has_value())
|
if (best_format_field.has_value() && option_field.has_value())
|
||||||
best_format_field = option_field;
|
best_format_field = option_field;
|
||||||
});
|
});
|
||||||
|
|
|
@ -31,20 +31,34 @@ enum class CalendarPatternStyle : u8 {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct CalendarPattern {
|
struct CalendarPattern {
|
||||||
|
enum class Field {
|
||||||
|
Era,
|
||||||
|
Year,
|
||||||
|
Month,
|
||||||
|
Weekday,
|
||||||
|
Day,
|
||||||
|
DayPeriod,
|
||||||
|
Hour,
|
||||||
|
Minute,
|
||||||
|
Second,
|
||||||
|
FractionalSecondDigits,
|
||||||
|
TimeZoneName,
|
||||||
|
};
|
||||||
|
|
||||||
template<typename Callback>
|
template<typename Callback>
|
||||||
void for_each_calendar_field_zipped_with(CalendarPattern const& other, Callback&& callback)
|
void for_each_calendar_field_zipped_with(CalendarPattern const& other, Callback&& callback)
|
||||||
{
|
{
|
||||||
callback(era, other.era);
|
callback(era, other.era, Field::Era);
|
||||||
callback(year, other.year);
|
callback(year, other.year, Field::Year);
|
||||||
callback(month, other.month);
|
callback(month, other.month, Field::Month);
|
||||||
callback(weekday, other.weekday);
|
callback(weekday, other.weekday, Field::Weekday);
|
||||||
callback(day, other.day);
|
callback(day, other.day, Field::Day);
|
||||||
callback(day_period, other.day_period);
|
callback(day_period, other.day_period, Field::DayPeriod);
|
||||||
callback(hour, other.hour);
|
callback(hour, other.hour, Field::Hour);
|
||||||
callback(minute, other.minute);
|
callback(minute, other.minute, Field::Minute);
|
||||||
callback(second, other.second);
|
callback(second, other.second, Field::Second);
|
||||||
callback(fractional_second_digits, other.fractional_second_digits);
|
callback(fractional_second_digits, other.fractional_second_digits, Field::FractionalSecondDigits);
|
||||||
callback(time_zone_name, other.time_zone_name);
|
callback(time_zone_name, other.time_zone_name, Field::TimeZoneName);
|
||||||
}
|
}
|
||||||
|
|
||||||
String pattern {};
|
String pattern {};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue