Skip to content

Commit

Permalink
avoid int formatting as much as possible (net -70%/-68% on 2822/3339 …
Browse files Browse the repository at this point in the history
…respectively)
  • Loading branch information
conradludgate committed Oct 17, 2022
1 parent 9585f5c commit 8cc9249
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
35 changes: 23 additions & 12 deletions src/format/mod.rs
Expand Up @@ -40,7 +40,6 @@ use alloc::string::{String, ToString};
#[cfg(any(feature = "alloc", feature = "std", test))]
use core::borrow::Borrow;
use core::fmt;
#[cfg(any(feature = "alloc", feature = "std", test))]
use core::fmt::Write;
use core::str::FromStr;
#[cfg(any(feature = "std", test))]
Expand Down Expand Up @@ -790,21 +789,33 @@ fn write_rfc2822_inner(
off: FixedOffset,
locale: Locales,
) -> fmt::Result {
result.push_str(locale.short_weekdays[d.weekday().num_days_from_sunday() as usize]);
result.push_str(", ");
write_hundreds(result, d.day() as u8)?;
result.push(' ');
result.push_str(locale.short_months[d.month0() as usize]);
result.push(' ');
write_hundreds(result, (d.year() / 100) as u8)?;
write_hundreds(result, (d.year() % 100) as u8)?;
result.push(' ');
write_hundreds(result, t.hour() as u8)?;
result.push(':');
write_hundreds(result, t.minute() as u8)?;
result.push(':');
let sec = t.second() + t.nanosecond() / 1_000_000_000;
write!(
result,
"{}, {:02} {} {:04} {:02}:{:02}:{:02} ",
locale.short_weekdays[d.weekday().num_days_from_sunday() as usize],
d.day(),
locale.short_months[d.month0() as usize],
d.year(),
t.hour(),
t.minute(),
sec
)?;
write_hundreds(result, sec as u8)?;
result.push(' ');
write_local_minus_utc(result, off, false, Colons::None)
}

/// Equivalent to `{:02}` formatting for n < 100.
pub(crate) fn write_hundreds(w: &mut impl Write, n: u8) -> fmt::Result {
let tens = b'0' + n / 10;
let ones = b'0' + n % 10;
w.write_char(tens as char)?;
w.write_char(ones as char)
}

/// Tries to format given arguments with given formatting items.
/// Internally used by `DelayedFormat`.
#[cfg(any(feature = "alloc", feature = "std", test))]
Expand Down
12 changes: 10 additions & 2 deletions src/naive/date.rs
Expand Up @@ -1820,14 +1820,22 @@ impl DoubleEndedIterator for NaiveDateWeeksIterator {
/// ```
impl fmt::Debug for NaiveDate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use core::fmt::Write;

let year = self.year();
let mdf = self.mdf();
if (0..=9999).contains(&year) {
write!(f, "{:04}-{:02}-{:02}", year, mdf.month(), mdf.day())
crate::format::write_hundreds(f, (year / 100) as u8)?;
crate::format::write_hundreds(f, (year % 100) as u8)?;
} else {
// ISO 8601 requires the explicit sign for out-of-range years
write!(f, "{:+05}-{:02}-{:02}", year, mdf.month(), mdf.day())
write!(f, "{:+05}", year)?;
}

f.write_char('-')?;
crate::format::write_hundreds(f, mdf.month() as u8)?;
f.write_char('-')?;
crate::format::write_hundreds(f, mdf.day() as u8)
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/naive/time/mod.rs
Expand Up @@ -1177,7 +1177,13 @@ impl fmt::Debug for NaiveTime {
(sec, self.frac)
};

write!(f, "{:02}:{:02}:{:02}", hour, min, sec)?;
use core::fmt::Write;
crate::format::write_hundreds(f, hour as u8)?;
f.write_char(':')?;
crate::format::write_hundreds(f, min as u8)?;
f.write_char(':')?;
crate::format::write_hundreds(f, sec as u8)?;

if nano == 0 {
Ok(())
} else if nano % 1_000_000 == 0 {
Expand Down

0 comments on commit 8cc9249

Please sign in to comment.