Skip to content

Commit e3f591f

Browse files
pitdickerdjc
authored andcommitted
Remove num-integer dependency
1 parent 3bfb6ab commit e3f591f

File tree

9 files changed

+26
-48
lines changed

9 files changed

+26
-48
lines changed

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ __doctest = []
3030

3131
[dependencies]
3232
time = { version = "0.1.43", optional = true }
33-
num-integer = { version = "0.1.36", default-features = false }
3433
num-traits = { version = "0.2", default-features = false }
3534
rustc-serialize = { version = "0.3.20", optional = true }
3635
serde = { version = "1.0.99", default-features = false, optional = true }

src/format/mod.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,6 @@ fn format_inner(
512512
) -> fmt::Result {
513513
let locale = Locales::new(locale);
514514

515-
use num_integer::{div_floor, mod_floor};
516-
517515
match *item {
518516
Item::Literal(s) | Item::Space(s) => result.push_str(s),
519517
#[cfg(any(feature = "alloc", feature = "std", test))]
@@ -527,11 +525,11 @@ fn format_inner(
527525

528526
let (width, v) = match *spec {
529527
Year => (4, date.map(|d| i64::from(d.year()))),
530-
YearDiv100 => (2, date.map(|d| div_floor(i64::from(d.year()), 100))),
531-
YearMod100 => (2, date.map(|d| mod_floor(i64::from(d.year()), 100))),
528+
YearDiv100 => (2, date.map(|d| i64::from(d.year()).div_euclid(100))),
529+
YearMod100 => (2, date.map(|d| i64::from(d.year()).rem_euclid(100))),
532530
IsoYear => (4, date.map(|d| i64::from(d.iso_week().year()))),
533-
IsoYearDiv100 => (2, date.map(|d| div_floor(i64::from(d.iso_week().year()), 100))),
534-
IsoYearMod100 => (2, date.map(|d| mod_floor(i64::from(d.iso_week().year()), 100))),
531+
IsoYearDiv100 => (2, date.map(|d| i64::from(d.iso_week().year()).div_euclid(100))),
532+
IsoYearMod100 => (2, date.map(|d| i64::from(d.iso_week().year()).rem_euclid(100))),
535533
Month => (2, date.map(|d| i64::from(d.month()))),
536534
Day => (2, date.map(|d| i64::from(d.day()))),
537535
WeekFromSun => (2, date.map(|d| i64::from(week_from_sun(d)))),

src/format/parsed.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
//! A collection of parsed date and time items.
55
//! They can be constructed incrementally while being checked for consistency.
66
7-
use num_integer::div_rem;
87
use num_traits::ToPrimitive;
98

109
use super::{ParseResult, IMPOSSIBLE, NOT_ENOUGH, OUT_OF_RANGE};
@@ -311,7 +310,8 @@ impl Parsed {
311310
if y < 0 {
312311
return Err(OUT_OF_RANGE);
313312
}
314-
let (q_, r_) = div_rem(y, 100);
313+
let q_ = y / 100;
314+
let r_ = y % 100;
315315
if q.unwrap_or(q_) == q_ && r.unwrap_or(r_) == r_ {
316316
Ok(Some(y))
317317
} else {
@@ -346,8 +346,7 @@ impl Parsed {
346346
let verify_ymd = |date: NaiveDate| {
347347
let year = date.year();
348348
let (year_div_100, year_mod_100) = if year >= 0 {
349-
let (q, r) = div_rem(year, 100);
350-
(Some(q), Some(r))
349+
(Some(year / 100), Some(year % 100))
351350
} else {
352351
(None, None) // they should be empty to be consistent
353352
};
@@ -367,8 +366,7 @@ impl Parsed {
367366
let isoweek = week.week();
368367
let weekday = date.weekday();
369368
let (isoyear_div_100, isoyear_mod_100) = if isoyear >= 0 {
370-
let (q, r) = div_rem(isoyear, 100);
371-
(Some(q), Some(r))
369+
(Some(isoyear / 100), Some(isoyear % 100))
372370
} else {
373371
(None, None) // they should be empty to be consistent
374372
};

src/naive/date.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use core::convert::TryFrom;
99
use core::ops::{Add, AddAssign, RangeInclusive, Sub, SubAssign};
1010
use core::{fmt, str};
1111

12-
use num_integer::div_mod_floor;
1312
use num_traits::ToPrimitive;
1413
#[cfg(feature = "rkyv")]
1514
use rkyv::{Archive, Deserialize, Serialize};
@@ -2037,6 +2036,10 @@ impl Default for NaiveDate {
20372036
}
20382037
}
20392038

2039+
fn div_mod_floor(val: i32, div: i32) -> (i32, i32) {
2040+
(val.div_euclid(div), val.rem_euclid(div))
2041+
}
2042+
20402043
#[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))]
20412044
fn test_encodable_json<F, E>(to_string: F)
20422045
where

src/naive/datetime/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use core::fmt::Write;
99
use core::ops::{Add, AddAssign, Sub, SubAssign};
1010
use core::{fmt, str};
1111

12-
use num_integer::div_mod_floor;
1312
use num_traits::ToPrimitive;
1413
#[cfg(feature = "rkyv")]
1514
use rkyv::{Archive, Deserialize, Serialize};
@@ -210,7 +209,8 @@ impl NaiveDateTime {
210209
#[inline]
211210
#[must_use]
212211
pub fn from_timestamp_opt(secs: i64, nsecs: u32) -> Option<NaiveDateTime> {
213-
let (days, secs) = div_mod_floor(secs, 86_400);
212+
let days = secs.div_euclid(86_400);
213+
let secs = secs.rem_euclid(86_400);
214214
let date = days
215215
.to_i32()
216216
.and_then(|days| days.checked_add(719_163))

src/naive/internals.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
use crate::Weekday;
1919
use core::{fmt, i32};
20-
use num_integer::{div_rem, mod_floor};
2120
use num_traits::FromPrimitive;
2221

2322
/// The internal date representation. This also includes the packed `Mdf` value.
@@ -95,7 +94,8 @@ static YEAR_DELTAS: [u8; 401] = [
9594
];
9695

9796
pub(super) fn cycle_to_yo(cycle: u32) -> (u32, u32) {
98-
let (mut year_mod_400, mut ordinal0) = div_rem(cycle, 365);
97+
let mut year_mod_400 = cycle / 365;
98+
let mut ordinal0 = cycle % 365;
9999
let delta = u32::from(YEAR_DELTAS[year_mod_400 as usize]);
100100
if ordinal0 < delta {
101101
year_mod_400 -= 1;
@@ -116,7 +116,7 @@ impl YearFlags {
116116
#[inline]
117117
#[must_use]
118118
pub fn from_year(year: i32) -> YearFlags {
119-
let year = mod_floor(year, 400);
119+
let year = year.rem_euclid(400);
120120
YearFlags::from_year_mod_400(year)
121121
}
122122

src/naive/time/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use core::borrow::Borrow;
88
use core::ops::{Add, AddAssign, Sub, SubAssign};
99
use core::{fmt, str};
1010

11-
use num_integer::div_mod_floor;
1211
#[cfg(feature = "rkyv")]
1312
use rkyv::{Archive, Deserialize, Serialize};
1413

@@ -762,8 +761,10 @@ impl NaiveTime {
762761

763762
/// Returns a triple of the hour, minute and second numbers.
764763
fn hms(&self) -> (u32, u32, u32) {
765-
let (mins, sec) = div_mod_floor(self.secs, 60);
766-
let (hour, min) = div_mod_floor(mins, 60);
764+
let sec = self.secs % 60;
765+
let mins = self.secs / 60;
766+
let min = mins % 60;
767+
let hour = mins / 60;
767768
(hour, min, sec)
768769
}
769770

src/offset/fixed.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use core::fmt;
77
use core::ops::{Add, Sub};
88

9-
use num_integer::div_mod_floor;
109
#[cfg(feature = "rkyv")]
1110
use rkyv::{Archive, Deserialize, Serialize};
1211

@@ -142,8 +141,10 @@ impl fmt::Debug for FixedOffset {
142141
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
143142
let offset = self.local_minus_utc;
144143
let (sign, offset) = if offset < 0 { ('-', -offset) } else { ('+', offset) };
145-
let (mins, sec) = div_mod_floor(offset, 60);
146-
let (hour, min) = div_mod_floor(mins, 60);
144+
let sec = offset.rem_euclid(60);
145+
let mins = offset.div_euclid(60);
146+
let min = mins.rem_euclid(60);
147+
let hour = mins.div_euclid(60);
147148
if sec == 0 {
148149
write!(f, "{}{:02}:{:02}", sign, hour, min)
149150
} else {

src/oldtime.rs

+1-23
Original file line numberDiff line numberDiff line change
@@ -462,31 +462,9 @@ impl Error for OutOfRangeError {
462462
}
463463
}
464464

465-
// Copied from libnum
466465
#[inline]
467466
const fn div_mod_floor_64(this: i64, other: i64) -> (i64, i64) {
468-
(div_floor_64(this, other), mod_floor_64(this, other))
469-
}
470-
471-
#[inline]
472-
const fn div_floor_64(this: i64, other: i64) -> i64 {
473-
match div_rem_64(this, other) {
474-
(d, r) if (r > 0 && other < 0) || (r < 0 && other > 0) => d - 1,
475-
(d, _) => d,
476-
}
477-
}
478-
479-
#[inline]
480-
const fn mod_floor_64(this: i64, other: i64) -> i64 {
481-
match this % other {
482-
r if (r > 0 && other < 0) || (r < 0 && other > 0) => r + other,
483-
r => r,
484-
}
485-
}
486-
487-
#[inline]
488-
const fn div_rem_64(this: i64, other: i64) -> (i64, i64) {
489-
(this / other, this % other)
467+
(this.div_euclid(other), this.rem_euclid(other))
490468
}
491469

492470
#[cfg(feature = "arbitrary")]

0 commit comments

Comments
 (0)