Skip to content

Commit

Permalink
LibJS: Use StringViews in Round{NumberToIncrement, TemporalInstant}
Browse files Browse the repository at this point in the history
  • Loading branch information
IdanHo authored and linusg committed Sep 6, 2021
1 parent 4b5aa21 commit 456938a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
10 changes: 5 additions & 5 deletions Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,13 +654,13 @@ double constrain_to_range(double x, double minimum, double maximum)
}

// 13.32 RoundNumberToIncrement ( x, increment, roundingMode ), https://tc39.es/proposal-temporal/#sec-temporal-roundnumbertoincrement
BigInt* round_number_to_increment(GlobalObject& global_object, BigInt const& x, u64 increment, String const& rounding_mode)
BigInt* round_number_to_increment(GlobalObject& global_object, BigInt const& x, u64 increment, StringView rounding_mode)
{
auto& heap = global_object.heap();

// 1. Assert: x and increment are mathematical values.
// 2. Assert: roundingMode is "ceil", "floor", "trunc", or "halfExpand".
VERIFY(rounding_mode == "ceil" || rounding_mode == "floor" || rounding_mode == "trunc" || rounding_mode == "halfExpand");
VERIFY(rounding_mode == "ceil"sv || rounding_mode == "floor"sv || rounding_mode == "trunc"sv || rounding_mode == "halfExpand"sv);

// OPTIMIZATION: If the increment is 1 the number is always rounded
if (increment == 1)
Expand All @@ -676,19 +676,19 @@ BigInt* round_number_to_increment(GlobalObject& global_object, BigInt const& x,

Crypto::SignedBigInteger rounded = move(division_result.quotient);
// 4. If roundingMode is "ceil", then
if (rounding_mode == "ceil") {
if (rounding_mode == "ceil"sv) {
// a. Let rounded be −floor(−quotient).
if (!division_result.remainder.is_negative())
rounded = rounded.plus(Crypto::UnsignedBigInteger { 1 });
}
// 5. Else if roundingMode is "floor", then
else if (rounding_mode == "floor") {
else if (rounding_mode == "floor"sv) {
// a. Let rounded be floor(quotient).
if (division_result.remainder.is_negative())
rounded = rounded.minus(Crypto::UnsignedBigInteger { 1 });
}
// 6. Else if roundingMode is "trunc", then
else if (rounding_mode == "trunc") {
else if (rounding_mode == "trunc"sv) {
// a. Let rounded be the integral part of quotient, removing any fractional digits.
// NOTE: This is a no-op
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ String larger_of_two_temporal_units(StringView, StringView);
Optional<u16> maximum_temporal_duration_rounding_increment(StringView unit);
String format_seconds_string_part(u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, Variant<String, u8> const& precision);
double constrain_to_range(double x, double minimum, double maximum);
BigInt* round_number_to_increment(GlobalObject&, BigInt const&, u64 increment, String const& rounding_mode);
BigInt* round_number_to_increment(GlobalObject&, BigInt const&, u64 increment, StringView rounding_mode);
Optional<ISODateTime> parse_iso_date_time(GlobalObject&, String const& iso_string);
Optional<TemporalInstant> parse_temporal_instant_string(GlobalObject&, String const& iso_string);
Optional<String> parse_temporal_calendar_string(GlobalObject&, String const& iso_string);
Expand Down
14 changes: 7 additions & 7 deletions Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,40 +204,40 @@ BigInt* difference_instant(GlobalObject& global_object, BigInt const& nanosecond
}

// 8.5.8 RoundTemporalInstant ( ns, increment, unit, roundingMode ), https://tc39.es/proposal-temporal/#sec-temporal-roundtemporalinstant
BigInt* round_temporal_instant(GlobalObject& global_object, BigInt const& nanoseconds, u64 increment, String const& unit, String const& rounding_mode)
BigInt* round_temporal_instant(GlobalObject& global_object, BigInt const& nanoseconds, u64 increment, StringView unit, StringView rounding_mode)
{
// 1. Assert: Type(ns) is BigInt.

u64 increment_nanoseconds;
// 2. If unit is "hour", then
if (unit == "hour") {
if (unit == "hour"sv) {
// a. Let incrementNs be increment × 3.6 × 10^12.
increment_nanoseconds = increment * 3600000000000;
}
// 3. Else if unit is "minute", then
else if (unit == "minute") {
else if (unit == "minute"sv) {
// a. Let incrementNs be increment × 6 × 10^10.
increment_nanoseconds = increment * 60000000000;
}
// 4. Else if unit is "second", then
else if (unit == "second") {
else if (unit == "second"sv) {
// a. Let incrementNs be increment × 10^9.
increment_nanoseconds = increment * 1000000000;
}
// 5. Else if unit is "millisecond", then
else if (unit == "millisecond") {
else if (unit == "millisecond"sv) {
// a. Let incrementNs be increment × 10^6.
increment_nanoseconds = increment * 1000000;
}
// 6. Else if unit is "microsecond", then
else if (unit == "microsecond") {
else if (unit == "microsecond"sv) {
// a. Let incrementNs be increment × 10^3.
increment_nanoseconds = increment * 1000;
}
// 7. Else,
else {
// a. Assert: unit is "nanosecond".
VERIFY(unit == "nanosecond");
VERIFY(unit == "nanosecond"sv);

// b. Let incrementNs be increment.
increment_nanoseconds = increment;
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibJS/Runtime/Temporal/Instant.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ BigInt* parse_temporal_instant(GlobalObject&, String const& iso_string);
i32 compare_epoch_nanoseconds(BigInt const&, BigInt const&);
BigInt* add_instant(GlobalObject&, BigInt const& epoch_nanoseconds, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds);
BigInt* difference_instant(GlobalObject&, BigInt const& nanoseconds1, BigInt const& nanoseconds2, u64 rounding_increment, StringView smallest_unit, StringView rounding_mode);
BigInt* round_temporal_instant(GlobalObject&, BigInt const& nanoseconds, u64 increment, String const& unit, String const& rounding_mode);
BigInt* round_temporal_instant(GlobalObject&, BigInt const& nanoseconds, u64 increment, StringView unit, StringView rounding_mode);
Optional<String> temporal_instant_to_string(GlobalObject&, Instant&, Value time_zone, Variant<String, u8> const& precision);

}

0 comments on commit 456938a

Please sign in to comment.