From 9b1d519a456143c9085f35c7c9404c609ffcfb16 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Sun, 23 Apr 2023 18:53:58 +0200 Subject: [PATCH] Remove num-integer dependency --- Cargo.toml | 1 - src/format/mod.rs | 10 ++++------ src/format/parsed.rs | 10 ++++------ src/naive/date.rs | 5 ++++- src/naive/datetime/mod.rs | 4 ++-- src/naive/internals.rs | 6 +++--- src/naive/time/mod.rs | 7 ++++--- src/offset/fixed.rs | 7 ++++--- src/oldtime.rs | 24 +----------------------- 9 files changed, 26 insertions(+), 48 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7c3a02409d..23ffda6ea2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,6 @@ __doctest = [] [dependencies] time = { version = "0.1.43", optional = true } -num-integer = { version = "0.1.36", default-features = false } num-traits = { version = "0.2", default-features = false } rustc-serialize = { version = "0.3.20", optional = true } serde = { version = "1.0.99", default-features = false, optional = true } diff --git a/src/format/mod.rs b/src/format/mod.rs index c3db7ade92..130c2e8617 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -511,8 +511,6 @@ fn format_inner( ) -> fmt::Result { let locale = Locales::new(locale); - use num_integer::{div_floor, mod_floor}; - match *item { Item::Literal(s) | Item::Space(s) => result.push_str(s), #[cfg(any(feature = "alloc", feature = "std", test))] @@ -526,11 +524,11 @@ fn format_inner( let (width, v) = match *spec { Year => (4, date.map(|d| i64::from(d.year()))), - YearDiv100 => (2, date.map(|d| div_floor(i64::from(d.year()), 100))), - YearMod100 => (2, date.map(|d| mod_floor(i64::from(d.year()), 100))), + YearDiv100 => (2, date.map(|d| i64::from(d.year()).div_euclid(100))), + YearMod100 => (2, date.map(|d| i64::from(d.year()).rem_euclid(100))), IsoYear => (4, date.map(|d| i64::from(d.iso_week().year()))), - IsoYearDiv100 => (2, date.map(|d| div_floor(i64::from(d.iso_week().year()), 100))), - IsoYearMod100 => (2, date.map(|d| mod_floor(i64::from(d.iso_week().year()), 100))), + IsoYearDiv100 => (2, date.map(|d| i64::from(d.iso_week().year()).div_euclid(100))), + IsoYearMod100 => (2, date.map(|d| i64::from(d.iso_week().year()).rem_euclid(100))), Month => (2, date.map(|d| i64::from(d.month()))), Day => (2, date.map(|d| i64::from(d.day()))), WeekFromSun => (2, date.map(|d| i64::from(week_from_sun(d)))), diff --git a/src/format/parsed.rs b/src/format/parsed.rs index b5fb8e699f..2f31e000cc 100644 --- a/src/format/parsed.rs +++ b/src/format/parsed.rs @@ -4,7 +4,6 @@ //! A collection of parsed date and time items. //! They can be constructed incrementally while being checked for consistency. -use num_integer::div_rem; use num_traits::ToPrimitive; use super::{ParseResult, IMPOSSIBLE, NOT_ENOUGH, OUT_OF_RANGE}; @@ -311,7 +310,8 @@ impl Parsed { if y < 0 { return Err(OUT_OF_RANGE); } - let (q_, r_) = div_rem(y, 100); + let q_ = y / 100; + let r_ = y % 100; if q.unwrap_or(q_) == q_ && r.unwrap_or(r_) == r_ { Ok(Some(y)) } else { @@ -346,8 +346,7 @@ impl Parsed { let verify_ymd = |date: NaiveDate| { let year = date.year(); let (year_div_100, year_mod_100) = if year >= 0 { - let (q, r) = div_rem(year, 100); - (Some(q), Some(r)) + (Some(year / 100), Some(year % 100)) } else { (None, None) // they should be empty to be consistent }; @@ -367,8 +366,7 @@ impl Parsed { let isoweek = week.week(); let weekday = date.weekday(); let (isoyear_div_100, isoyear_mod_100) = if isoyear >= 0 { - let (q, r) = div_rem(isoyear, 100); - (Some(q), Some(r)) + (Some(isoyear / 100), Some(isoyear % 100)) } else { (None, None) // they should be empty to be consistent }; diff --git a/src/naive/date.rs b/src/naive/date.rs index c5b6fe0d74..d6e16ad486 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -9,7 +9,6 @@ use core::convert::TryFrom; use core::ops::{Add, AddAssign, RangeInclusive, Sub, SubAssign}; use core::{fmt, str}; -use num_integer::div_mod_floor; use num_traits::ToPrimitive; #[cfg(feature = "rkyv")] use rkyv::{Archive, Deserialize, Serialize}; @@ -56,6 +55,10 @@ const MIN_DAYS_FROM_YEAR_0: i32 = (MIN_YEAR + 400_000) * 365 + (MIN_YEAR + 400_0 #[cfg(test)] // only used for testing, but duplicated in naive::datetime const MAX_BITS: usize = 44; +fn div_mod_floor(val: i32, div: i32) -> (i32, i32) { + (val.div_euclid(div), val.rem_euclid(div)) +} + /// A week represented by a [`NaiveDate`] and a [`Weekday`] which is the first /// day of the week. #[derive(Debug)] diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index 671e9eddf2..b5e36e6490 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -10,7 +10,6 @@ use core::fmt::Write; use core::ops::{Add, AddAssign, Sub, SubAssign}; use core::{fmt, str}; -use num_integer::div_mod_floor; use num_traits::ToPrimitive; #[cfg(feature = "rkyv")] use rkyv::{Archive, Deserialize, Serialize}; @@ -239,7 +238,8 @@ impl NaiveDateTime { #[inline] #[must_use] pub fn from_timestamp_opt(secs: i64, nsecs: u32) -> Option { - let (days, secs) = div_mod_floor(secs, 86_400); + let days = secs.div_euclid(86_400); + let secs = secs.rem_euclid(86_400); let date = days .to_i32() .and_then(|days| days.checked_add(719_163)) diff --git a/src/naive/internals.rs b/src/naive/internals.rs index 2c954f7abb..777842822b 100644 --- a/src/naive/internals.rs +++ b/src/naive/internals.rs @@ -17,7 +17,6 @@ use crate::Weekday; use core::{fmt, i32}; -use num_integer::{div_rem, mod_floor}; use num_traits::FromPrimitive; /// The internal date representation. This also includes the packed `Mdf` value. @@ -95,7 +94,8 @@ static YEAR_DELTAS: [u8; 401] = [ ]; pub(super) fn cycle_to_yo(cycle: u32) -> (u32, u32) { - let (mut year_mod_400, mut ordinal0) = div_rem(cycle, 365); + let mut year_mod_400 = cycle / 365; + let mut ordinal0 = cycle % 365; let delta = u32::from(YEAR_DELTAS[year_mod_400 as usize]); if ordinal0 < delta { year_mod_400 -= 1; @@ -116,7 +116,7 @@ impl YearFlags { #[inline] #[must_use] pub fn from_year(year: i32) -> YearFlags { - let year = mod_floor(year, 400); + let year = year.rem_euclid(400); YearFlags::from_year_mod_400(year) } diff --git a/src/naive/time/mod.rs b/src/naive/time/mod.rs index 14e22fe61b..320cf957cc 100644 --- a/src/naive/time/mod.rs +++ b/src/naive/time/mod.rs @@ -8,7 +8,6 @@ use core::borrow::Borrow; use core::ops::{Add, AddAssign, Sub, SubAssign}; use core::{fmt, str}; -use num_integer::div_mod_floor; #[cfg(feature = "rkyv")] use rkyv::{Archive, Deserialize, Serialize}; @@ -762,8 +761,10 @@ impl NaiveTime { /// Returns a triple of the hour, minute and second numbers. fn hms(&self) -> (u32, u32, u32) { - let (mins, sec) = div_mod_floor(self.secs, 60); - let (hour, min) = div_mod_floor(mins, 60); + let sec = self.secs % 60; + let mins = self.secs / 60; + let min = mins % 60; + let hour = mins / 60; (hour, min, sec) } diff --git a/src/offset/fixed.rs b/src/offset/fixed.rs index 04ffa3b989..5284683e3a 100644 --- a/src/offset/fixed.rs +++ b/src/offset/fixed.rs @@ -6,7 +6,6 @@ use core::fmt; use core::ops::{Add, Sub}; -use num_integer::div_mod_floor; #[cfg(feature = "rkyv")] use rkyv::{Archive, Deserialize, Serialize}; @@ -140,8 +139,10 @@ impl fmt::Debug for FixedOffset { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let offset = self.local_minus_utc; let (sign, offset) = if offset < 0 { ('-', -offset) } else { ('+', offset) }; - let (mins, sec) = div_mod_floor(offset, 60); - let (hour, min) = div_mod_floor(mins, 60); + let sec = offset.rem_euclid(60); + let mins = offset.div_euclid(60); + let min = mins.rem_euclid(60); + let hour = mins.div_euclid(60); if sec == 0 { write!(f, "{}{:02}:{:02}", sign, hour, min) } else { diff --git a/src/oldtime.rs b/src/oldtime.rs index 8fde78670d..e27be7db63 100644 --- a/src/oldtime.rs +++ b/src/oldtime.rs @@ -462,31 +462,9 @@ impl Error for OutOfRangeError { } } -// Copied from libnum #[inline] const fn div_mod_floor_64(this: i64, other: i64) -> (i64, i64) { - (div_floor_64(this, other), mod_floor_64(this, other)) -} - -#[inline] -const fn div_floor_64(this: i64, other: i64) -> i64 { - match div_rem_64(this, other) { - (d, r) if (r > 0 && other < 0) || (r < 0 && other > 0) => d - 1, - (d, _) => d, - } -} - -#[inline] -const fn mod_floor_64(this: i64, other: i64) -> i64 { - match this % other { - r if (r > 0 && other < 0) || (r < 0 && other > 0) => r + other, - r => r, - } -} - -#[inline] -const fn div_rem_64(this: i64, other: i64) -> (i64, i64) { - (this / other, this % other) + (this.div_euclid(other), this.rem_euclid(other)) } #[cfg(feature = "arbitrary")]