mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 19:37:36 +00:00
LibJS: Use StringViews in Round{NumberToIncrement, TemporalInstant}
This commit is contained in:
parent
4b5aa2102c
commit
456938add0
4 changed files with 14 additions and 14 deletions
|
@ -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
|
// 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();
|
auto& heap = global_object.heap();
|
||||||
|
|
||||||
// 1. Assert: x and increment are mathematical values.
|
// 1. Assert: x and increment are mathematical values.
|
||||||
// 2. Assert: roundingMode is "ceil", "floor", "trunc", or "halfExpand".
|
// 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
|
// OPTIMIZATION: If the increment is 1 the number is always rounded
|
||||||
if (increment == 1)
|
if (increment == 1)
|
||||||
|
@ -676,19 +676,19 @@ BigInt* round_number_to_increment(GlobalObject& global_object, BigInt const& x,
|
||||||
|
|
||||||
Crypto::SignedBigInteger rounded = move(division_result.quotient);
|
Crypto::SignedBigInteger rounded = move(division_result.quotient);
|
||||||
// 4. If roundingMode is "ceil", then
|
// 4. If roundingMode is "ceil", then
|
||||||
if (rounding_mode == "ceil") {
|
if (rounding_mode == "ceil"sv) {
|
||||||
// a. Let rounded be −floor(−quotient).
|
// a. Let rounded be −floor(−quotient).
|
||||||
if (!division_result.remainder.is_negative())
|
if (!division_result.remainder.is_negative())
|
||||||
rounded = rounded.plus(Crypto::UnsignedBigInteger { 1 });
|
rounded = rounded.plus(Crypto::UnsignedBigInteger { 1 });
|
||||||
}
|
}
|
||||||
// 5. Else if roundingMode is "floor", then
|
// 5. Else if roundingMode is "floor", then
|
||||||
else if (rounding_mode == "floor") {
|
else if (rounding_mode == "floor"sv) {
|
||||||
// a. Let rounded be floor(quotient).
|
// a. Let rounded be floor(quotient).
|
||||||
if (division_result.remainder.is_negative())
|
if (division_result.remainder.is_negative())
|
||||||
rounded = rounded.minus(Crypto::UnsignedBigInteger { 1 });
|
rounded = rounded.minus(Crypto::UnsignedBigInteger { 1 });
|
||||||
}
|
}
|
||||||
// 6. Else if roundingMode is "trunc", then
|
// 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.
|
// a. Let rounded be the integral part of quotient, removing any fractional digits.
|
||||||
// NOTE: This is a no-op
|
// NOTE: This is a no-op
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,7 +93,7 @@ String larger_of_two_temporal_units(StringView, StringView);
|
||||||
Optional<u16> maximum_temporal_duration_rounding_increment(StringView unit);
|
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);
|
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);
|
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<ISODateTime> parse_iso_date_time(GlobalObject&, String const& iso_string);
|
||||||
Optional<TemporalInstant> parse_temporal_instant_string(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);
|
Optional<String> parse_temporal_calendar_string(GlobalObject&, String const& iso_string);
|
||||||
|
|
|
@ -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
|
// 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.
|
// 1. Assert: Type(ns) is BigInt.
|
||||||
|
|
||||||
u64 increment_nanoseconds;
|
u64 increment_nanoseconds;
|
||||||
// 2. If unit is "hour", then
|
// 2. If unit is "hour", then
|
||||||
if (unit == "hour") {
|
if (unit == "hour"sv) {
|
||||||
// a. Let incrementNs be increment × 3.6 × 10^12.
|
// a. Let incrementNs be increment × 3.6 × 10^12.
|
||||||
increment_nanoseconds = increment * 3600000000000;
|
increment_nanoseconds = increment * 3600000000000;
|
||||||
}
|
}
|
||||||
// 3. Else if unit is "minute", then
|
// 3. Else if unit is "minute", then
|
||||||
else if (unit == "minute") {
|
else if (unit == "minute"sv) {
|
||||||
// a. Let incrementNs be increment × 6 × 10^10.
|
// a. Let incrementNs be increment × 6 × 10^10.
|
||||||
increment_nanoseconds = increment * 60000000000;
|
increment_nanoseconds = increment * 60000000000;
|
||||||
}
|
}
|
||||||
// 4. Else if unit is "second", then
|
// 4. Else if unit is "second", then
|
||||||
else if (unit == "second") {
|
else if (unit == "second"sv) {
|
||||||
// a. Let incrementNs be increment × 10^9.
|
// a. Let incrementNs be increment × 10^9.
|
||||||
increment_nanoseconds = increment * 1000000000;
|
increment_nanoseconds = increment * 1000000000;
|
||||||
}
|
}
|
||||||
// 5. Else if unit is "millisecond", then
|
// 5. Else if unit is "millisecond", then
|
||||||
else if (unit == "millisecond") {
|
else if (unit == "millisecond"sv) {
|
||||||
// a. Let incrementNs be increment × 10^6.
|
// a. Let incrementNs be increment × 10^6.
|
||||||
increment_nanoseconds = increment * 1000000;
|
increment_nanoseconds = increment * 1000000;
|
||||||
}
|
}
|
||||||
// 6. Else if unit is "microsecond", then
|
// 6. Else if unit is "microsecond", then
|
||||||
else if (unit == "microsecond") {
|
else if (unit == "microsecond"sv) {
|
||||||
// a. Let incrementNs be increment × 10^3.
|
// a. Let incrementNs be increment × 10^3.
|
||||||
increment_nanoseconds = increment * 1000;
|
increment_nanoseconds = increment * 1000;
|
||||||
}
|
}
|
||||||
// 7. Else,
|
// 7. Else,
|
||||||
else {
|
else {
|
||||||
// a. Assert: unit is "nanosecond".
|
// a. Assert: unit is "nanosecond".
|
||||||
VERIFY(unit == "nanosecond");
|
VERIFY(unit == "nanosecond"sv);
|
||||||
|
|
||||||
// b. Let incrementNs be increment.
|
// b. Let incrementNs be increment.
|
||||||
increment_nanoseconds = increment;
|
increment_nanoseconds = increment;
|
||||||
|
|
|
@ -42,7 +42,7 @@ BigInt* parse_temporal_instant(GlobalObject&, String const& iso_string);
|
||||||
i32 compare_epoch_nanoseconds(BigInt const&, BigInt const&);
|
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* 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* 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);
|
Optional<String> temporal_instant_to_string(GlobalObject&, Instant&, Value time_zone, Variant<String, u8> const& precision);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue