LibJS+LibIMAP: Use the new Optional<U>(Optional<T>) constructor

These look much nicer than these repeated ternaries :^)
This commit is contained in:
Idan Horowitz 2022-01-23 17:18:10 +02:00
parent a3a4d0aea2
commit ace36681ff
Notes: sideshowbarker 2024-07-17 20:22:46 +09:00
7 changed files with 23 additions and 28 deletions

View file

@ -725,9 +725,9 @@ static NonnullOwnPtr<Interface> parse_interface(StringView filename, StringView
DictionaryMember member { DictionaryMember member {
required, required,
move(type), move(type),
move(name), name,
move(extended_attributes), move(extended_attributes),
default_value.has_value() ? default_value.value() : Optional<String> {}, Optional<String>(move(default_value)),
}; };
dictionary.members.append(move(member)); dictionary.members.append(move(member));
} }

View file

@ -315,7 +315,7 @@ FetchResponseData Parser::parse_fetch_response()
break; break;
case FetchCommand::DataItemType::BodySection: { case FetchCommand::DataItemType::BodySection: {
auto body = parse_nstring(); auto body = parse_nstring();
fetch_response.add_body_data(move(data_item), body.has_value() ? body.release_value() : Optional<String>()); fetch_response.add_body_data(move(data_item), Optional<String>(move(body)));
break; break;
} }
} }
@ -434,8 +434,8 @@ BodyStructure Parser::parse_one_part_body()
auto data = BodyStructureData { auto data = BodyStructureData {
type, type,
subtype, subtype,
id.has_value() ? Optional<String>(id.value()) : Optional<String>(), Optional<String>(move(id)),
description.has_value() ? Optional<String>(description.value()) : Optional<String>(), Optional<String>(move(description)),
encoding, encoding,
params, params,
num_octets, num_octets,
@ -496,8 +496,8 @@ BodyStructure Parser::parse_one_part_body()
BodyStructureData data { BodyStructureData data {
type, type,
subtype, subtype,
id.has_value() ? Optional<String>(id.value()) : Optional<String>(), Optional<String>(move(id)),
description.has_value() ? Optional<String>(description.value()) : Optional<String>(), Optional<String>(move(description)),
encoding, encoding,
params, params,
num_octets, num_octets,
@ -522,8 +522,8 @@ BodyStructure Parser::parse_one_part_body()
BodyStructureData data { BodyStructureData data {
type, type,
subtype, subtype,
id.has_value() ? Optional<String>(id.value()) : Optional<String>(), Optional<String>(move(id)),
description.has_value() ? Optional<String>(description.value()) : Optional<String>(), Optional<String>(move(description)),
encoding, encoding,
params, params,
num_octets, num_octets,
@ -802,18 +802,17 @@ Address Parser::parse_address()
{ {
consume("("); consume("(");
auto address = Address(); auto address = Address();
// I hate this so much. Why is there no Optional.map??
auto name = parse_nstring(); auto name = parse_nstring();
address.name = name.has_value() ? Optional<String>(name.value()) : Optional<String>(); address.name = Optional<String>(move(name));
consume(" "); consume(" ");
auto source_route = parse_nstring(); auto source_route = parse_nstring();
address.source_route = source_route.has_value() ? Optional<String>(source_route.value()) : Optional<String>(); address.source_route = Optional<String>(move(source_route));
consume(" "); consume(" ");
auto mailbox = parse_nstring(); auto mailbox = parse_nstring();
address.mailbox = mailbox.has_value() ? Optional<String>(mailbox.value()) : Optional<String>(); address.mailbox = Optional<String>(move(mailbox));
consume(" "); consume(" ");
auto host = parse_nstring(); auto host = parse_nstring();
address.host = host.has_value() ? Optional<String>(host.value()) : Optional<String>(); address.host = Optional<String>(move(host));
consume(")"); consume(")");
return address; return address;
} }

View file

@ -1220,7 +1220,7 @@ ThrowCompletionOr<ISODateTime> parse_iso_date_time(GlobalObject& global_object,
return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidTime); return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidTime);
// 20. Return the Record { [[Year]]: year, [[Month]]: month, [[Day]]: day, [[Hour]]: hour, [[Minute]]: minute, [[Second]]: second, [[Millisecond]]: millisecond, [[Microsecond]]: microsecond, [[Nanosecond]]: nanosecond, [[Calendar]]: calendar }. // 20. Return the Record { [[Year]]: year, [[Month]]: month, [[Day]]: day, [[Hour]]: hour, [[Minute]]: minute, [[Second]]: second, [[Millisecond]]: millisecond, [[Microsecond]]: microsecond, [[Nanosecond]]: nanosecond, [[Calendar]]: calendar }.
return ISODateTime { .year = year, .month = month, .day = day, .hour = hour, .minute = minute, .second = second, .millisecond = millisecond, .microsecond = microsecond, .nanosecond = nanosecond, .calendar = calendar_part.has_value() ? *calendar_part : Optional<String>() }; return ISODateTime { .year = year, .month = month, .day = day, .hour = hour, .minute = minute, .second = second, .millisecond = millisecond, .microsecond = microsecond, .nanosecond = nanosecond, .calendar = Optional<String>(move(calendar_part)) };
} }
// 13.34 ParseTemporalInstantString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalinstantstring // 13.34 ParseTemporalInstantString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalinstantstring
@ -1626,10 +1626,6 @@ ThrowCompletionOr<TemporalTimeZone> parse_temporal_time_zone_string(GlobalObject
{ {
auto& vm = global_object.vm(); auto& vm = global_object.vm();
auto optional_string_view_to_optional_string = [](Optional<StringView> const& s) {
return s.has_value() ? String { *s } : Optional<String> {};
};
// 1. Assert: Type(isoString) is String. // 1. Assert: Type(isoString) is String.
// 2. Let parseResult be ParseText(! StringToCodePoints(isoString), TemporalTimeZoneString). // 2. Let parseResult be ParseText(! StringToCodePoints(isoString), TemporalTimeZoneString).
@ -1655,7 +1651,7 @@ ThrowCompletionOr<TemporalTimeZone> parse_temporal_time_zone_string(GlobalObject
// 7. If z is not empty, then // 7. If z is not empty, then
if (z.has_value()) { if (z.has_value()) {
// a. Return the Record { [[Z]]: true, [[OffsetString]]: undefined, [[Name]]: name }. // a. Return the Record { [[Z]]: true, [[OffsetString]]: undefined, [[Name]]: name }.
return TemporalTimeZone { .z = true, .offset_string = {}, .name = optional_string_view_to_optional_string(name) }; return TemporalTimeZone { .z = true, .offset_string = {}, .name = Optional<String>(move(name)) };
} }
// 8. If offsetString is empty, then // 8. If offsetString is empty, then
@ -1665,7 +1661,7 @@ ThrowCompletionOr<TemporalTimeZone> parse_temporal_time_zone_string(GlobalObject
// NOTE: No-op. // NOTE: No-op.
// 10. Return the Record { [[Z]]: false, [[OffsetString]]: offsetString, [[Name]]: name }. // 10. Return the Record { [[Z]]: false, [[OffsetString]]: offsetString, [[Name]]: name }.
return TemporalTimeZone { .z = false, .offset_string = optional_string_view_to_optional_string(offset_string), .name = optional_string_view_to_optional_string(name) }; return TemporalTimeZone { .z = false, .offset_string = Optional<String>(move(offset_string)), .name = Optional<String>(move(name)) };
} }
// 13.44 ParseTemporalYearMonthString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalyearmonthstring // 13.44 ParseTemporalYearMonthString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalyearmonthstring

View file

@ -425,7 +425,7 @@ JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::round)
auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit); auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit);
// 19. Let roundingIncrement be ? ToTemporalRoundingIncrement(roundTo, maximum, false). // 19. Let roundingIncrement be ? ToTemporalRoundingIncrement(roundTo, maximum, false).
auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *round_to, maximum.has_value() ? *maximum : Optional<double> {}, false)); auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *round_to, Optional<double>(maximum), false));
// 20. Let relativeTo be ? ToRelativeTemporalObject(roundTo). // 20. Let relativeTo be ? ToRelativeTemporalObject(roundTo).
auto relative_to = TRY(to_relative_temporal_object(global_object, *round_to)); auto relative_to = TRY(to_relative_temporal_object(global_object, *round_to));

View file

@ -544,7 +544,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::until)
auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit); auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit);
// 12. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false). // 12. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false).
auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, maximum.has_value() ? *maximum : Optional<double> {}, false)); auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, Optional<double>(maximum), false));
// 13. Let diff be ? DifferenceISODateTime(dateTime.[[ISOYear]], dateTime.[[ISOMonth]], dateTime.[[ISODay]], dateTime.[[ISOHour]], dateTime.[[ISOMinute]], dateTime.[[ISOSecond]], dateTime.[[ISOMillisecond]], dateTime.[[ISOMicrosecond]], dateTime.[[ISONanosecond]], other.[[ISOYear]], other.[[ISOMonth]], other.[[ISODay]], other.[[ISOHour]], other.[[ISOMinute]], other.[[ISOSecond]], other.[[ISOMillisecond]], other.[[ISOMicrosecond]], other.[[ISONanosecond]], dateTime.[[Calendar]], largestUnit, options). // 13. Let diff be ? DifferenceISODateTime(dateTime.[[ISOYear]], dateTime.[[ISOMonth]], dateTime.[[ISODay]], dateTime.[[ISOHour]], dateTime.[[ISOMinute]], dateTime.[[ISOSecond]], dateTime.[[ISOMillisecond]], dateTime.[[ISOMicrosecond]], dateTime.[[ISONanosecond]], other.[[ISOYear]], other.[[ISOMonth]], other.[[ISODay]], other.[[ISOHour]], other.[[ISOMinute]], other.[[ISOSecond]], other.[[ISOMillisecond]], other.[[ISOMicrosecond]], other.[[ISONanosecond]], dateTime.[[Calendar]], largestUnit, options).
auto diff = TRY(difference_iso_date_time(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->iso_hour(), date_time->iso_minute(), date_time->iso_second(), date_time->iso_millisecond(), date_time->iso_microsecond(), date_time->iso_nanosecond(), other->iso_year(), other->iso_month(), other->iso_day(), other->iso_hour(), other->iso_minute(), other->iso_second(), other->iso_millisecond(), other->iso_microsecond(), other->iso_nanosecond(), date_time->calendar(), *largest_unit, options)); auto diff = TRY(difference_iso_date_time(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->iso_hour(), date_time->iso_minute(), date_time->iso_second(), date_time->iso_millisecond(), date_time->iso_microsecond(), date_time->iso_nanosecond(), other->iso_year(), other->iso_month(), other->iso_day(), other->iso_hour(), other->iso_minute(), other->iso_second(), other->iso_millisecond(), other->iso_microsecond(), other->iso_nanosecond(), date_time->calendar(), *largest_unit, options));
@ -601,7 +601,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::since)
auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit); auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit);
// 13. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false). // 13. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false).
auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, maximum.has_value() ? *maximum : Optional<double> {}, false)); auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, Optional<double>(maximum), false));
// 14. Let diff be ? DifferenceISODateTime(dateTime.[[ISOYear]], dateTime.[[ISOMonth]], dateTime.[[ISODay]], dateTime.[[ISOHour]], dateTime.[[ISOMinute]], dateTime.[[ISOSecond]], dateTime.[[ISOMillisecond]], dateTime.[[ISOMicrosecond]], dateTime.[[ISONanosecond]], other.[[ISOYear]], other.[[ISOMonth]], other.[[ISODay]], other.[[ISOHour]], other.[[ISOMinute]], other.[[ISOSecond]], other.[[ISOMillisecond]], other.[[ISOMicrosecond]], other.[[ISONanosecond]], dateTime.[[Calendar]], largestUnit, options). // 14. Let diff be ? DifferenceISODateTime(dateTime.[[ISOYear]], dateTime.[[ISOMonth]], dateTime.[[ISODay]], dateTime.[[ISOHour]], dateTime.[[ISOMinute]], dateTime.[[ISOSecond]], dateTime.[[ISOMillisecond]], dateTime.[[ISOMicrosecond]], dateTime.[[ISONanosecond]], other.[[ISOYear]], other.[[ISOMonth]], other.[[ISODay]], other.[[ISOHour]], other.[[ISOMinute]], other.[[ISOSecond]], other.[[ISOMillisecond]], other.[[ISOMicrosecond]], other.[[ISONanosecond]], dateTime.[[Calendar]], largestUnit, options).
auto diff = TRY(difference_iso_date_time(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->iso_hour(), date_time->iso_minute(), date_time->iso_second(), date_time->iso_millisecond(), date_time->iso_microsecond(), date_time->iso_nanosecond(), other->iso_year(), other->iso_month(), other->iso_day(), other->iso_hour(), other->iso_minute(), other->iso_second(), other->iso_millisecond(), other->iso_microsecond(), other->iso_nanosecond(), date_time->calendar(), *largest_unit, options)); auto diff = TRY(difference_iso_date_time(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->iso_hour(), date_time->iso_minute(), date_time->iso_second(), date_time->iso_millisecond(), date_time->iso_microsecond(), date_time->iso_nanosecond(), other->iso_year(), other->iso_month(), other->iso_day(), other->iso_hour(), other->iso_minute(), other->iso_second(), other->iso_millisecond(), other->iso_microsecond(), other->iso_nanosecond(), date_time->calendar(), *largest_unit, options));

View file

@ -281,7 +281,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::until)
auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit); auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit);
// 10. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false). // 10. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false).
auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, maximum.has_value() ? *maximum : Optional<double> {}, false)); auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, Optional<double>(maximum), false));
// 11. Let result be ! DifferenceTime(temporalTime.[[ISOHour]], temporalTime.[[ISOMinute]], temporalTime.[[ISOSecond]], temporalTime.[[ISOMillisecond]], temporalTime.[[ISOMicrosecond]], temporalTime.[[ISONanosecond]], other.[[ISOHour]], other.[[ISOMinute]], other.[[ISOSecond]], other.[[ISOMillisecond]], other.[[ISOMicrosecond]], other.[[ISONanosecond]]). // 11. Let result be ! DifferenceTime(temporalTime.[[ISOHour]], temporalTime.[[ISOMinute]], temporalTime.[[ISOSecond]], temporalTime.[[ISOMillisecond]], temporalTime.[[ISOMicrosecond]], temporalTime.[[ISONanosecond]], other.[[ISOHour]], other.[[ISOMinute]], other.[[ISOSecond]], other.[[ISOMillisecond]], other.[[ISOMicrosecond]], other.[[ISONanosecond]]).
auto result = difference_time(temporal_time->iso_hour(), temporal_time->iso_minute(), temporal_time->iso_second(), temporal_time->iso_millisecond(), temporal_time->iso_microsecond(), temporal_time->iso_nanosecond(), other->iso_hour(), other->iso_minute(), other->iso_second(), other->iso_millisecond(), other->iso_microsecond(), other->iso_nanosecond()); auto result = difference_time(temporal_time->iso_hour(), temporal_time->iso_minute(), temporal_time->iso_second(), temporal_time->iso_millisecond(), temporal_time->iso_microsecond(), temporal_time->iso_nanosecond(), other->iso_hour(), other->iso_minute(), other->iso_second(), other->iso_millisecond(), other->iso_microsecond(), other->iso_nanosecond());
@ -328,7 +328,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::since)
auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit); auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit);
// 11. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false). // 11. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false).
auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, maximum.has_value() ? *maximum : Optional<double> {}, false)); auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, Optional<double>(maximum), false));
// 12. Let result be ! DifferenceTime(other.[[ISOHour]], other.[[ISOMinute]], other.[[ISOSecond]], other.[[ISOMillisecond]], other.[[ISOMicrosecond]], other.[[ISONanosecond]], temporalTime.[[ISOHour]], temporalTime.[[ISOMinute]], temporalTime.[[ISOSecond]], temporalTime.[[ISOMillisecond]], temporalTime.[[ISOMicrosecond]], temporalTime.[[ISONanosecond]]). // 12. Let result be ! DifferenceTime(other.[[ISOHour]], other.[[ISOMinute]], other.[[ISOSecond]], other.[[ISOMillisecond]], other.[[ISOMicrosecond]], other.[[ISONanosecond]], temporalTime.[[ISOHour]], temporalTime.[[ISOMinute]], temporalTime.[[ISOSecond]], temporalTime.[[ISOMillisecond]], temporalTime.[[ISOMicrosecond]], temporalTime.[[ISONanosecond]]).
auto result = difference_time(other->iso_hour(), other->iso_minute(), other->iso_second(), other->iso_millisecond(), other->iso_microsecond(), other->iso_nanosecond(), temporal_time->iso_hour(), temporal_time->iso_minute(), temporal_time->iso_second(), temporal_time->iso_millisecond(), temporal_time->iso_microsecond(), temporal_time->iso_nanosecond()); auto result = difference_time(other->iso_hour(), other->iso_minute(), other->iso_second(), other->iso_millisecond(), other->iso_microsecond(), other->iso_nanosecond(), temporal_time->iso_hour(), temporal_time->iso_minute(), temporal_time->iso_second(), temporal_time->iso_millisecond(), temporal_time->iso_microsecond(), temporal_time->iso_nanosecond());

View file

@ -982,7 +982,7 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::until)
auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit); auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit);
// 12. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false). // 12. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false).
auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, maximum.has_value() ? *maximum : Optional<double> {}, false)); auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, Optional<double>(maximum), false));
// 13. If largestUnit is not one of "year", "month", "week", or "day", then // 13. If largestUnit is not one of "year", "month", "week", or "day", then
if (!largest_unit->is_one_of("year"sv, "month"sv, "week"sv, "day"sv)) { if (!largest_unit->is_one_of("year"sv, "month"sv, "week"sv, "day"sv)) {
@ -1059,7 +1059,7 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::since)
auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit); auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit);
// 13. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false). // 13. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false).
auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, maximum.has_value() ? *maximum : Optional<double> {}, false)); auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, Optional<double>(maximum), false));
// 14. If largestUnit is not one of "year", "month", "week", or "day", then // 14. If largestUnit is not one of "year", "month", "week", or "day", then
if (!largest_unit->is_one_of("year"sv, "month"sv, "week"sv, "day"sv)) { if (!largest_unit->is_one_of("year"sv, "month"sv, "week"sv, "day"sv)) {