From d3581310451d26077224c3858297f22ad9f1924d Mon Sep 17 00:00:00 2001 From: Jacob Pratt Date: Tue, 16 Aug 2022 01:05:46 -0400 Subject: [PATCH] Use byte string literals where possible --- src/format_description/parse.rs | 4 ++-- src/formatting/formattable.rs | 25 ++++++++------------- src/formatting/mod.rs | 12 +++++----- time-macros/src/format_description/parse.rs | 4 ++-- 4 files changed, 19 insertions(+), 26 deletions(-) diff --git a/src/format_description/parse.rs b/src/format_description/parse.rs index a1d42ba10..d526dcb1b 100644 --- a/src/format_description/parse.rs +++ b/src/format_description/parse.rs @@ -54,12 +54,12 @@ fn parse_item<'a>( if let [b'[', b'[', remaining @ ..] = s { *index += 2; return Ok(ParsedItem { - item: FormatItem::Literal(&[b'[']), + item: FormatItem::Literal(b"["), remaining, }); }; - if s.starts_with(&[b'[']) { + if s.starts_with(b"[") { if let Some(bracket_index) = s.iter().position(|&c| c == b']') { *index += 1; // opening bracket let ret_val = ParsedItem { diff --git a/src/formatting/formattable.rs b/src/formatting/formattable.rs index a8cddda63..273d3b963 100644 --- a/src/formatting/formattable.rs +++ b/src/formatting/formattable.rs @@ -180,21 +180,21 @@ impl sealed::Sealed for Rfc3339 { } bytes += format_number_pad_zero::<4, _, _>(output, year as u32)?; - bytes += write(output, &[b'-'])?; + bytes += write(output, b"-")?; bytes += format_number_pad_zero::<2, _, _>(output, date.month() as u8)?; - bytes += write(output, &[b'-'])?; + bytes += write(output, b"-")?; bytes += format_number_pad_zero::<2, _, _>(output, date.day())?; - bytes += write(output, &[b'T'])?; + bytes += write(output, b"T")?; bytes += format_number_pad_zero::<2, _, _>(output, time.hour())?; - bytes += write(output, &[b':'])?; + bytes += write(output, b":")?; bytes += format_number_pad_zero::<2, _, _>(output, time.minute())?; - bytes += write(output, &[b':'])?; + bytes += write(output, b":")?; bytes += format_number_pad_zero::<2, _, _>(output, time.second())?; #[allow(clippy::if_not_else)] if time.nanosecond() != 0 { let nanos = time.nanosecond(); - bytes += write(output, &[b'.'])?; + bytes += write(output, b".")?; bytes += if nanos % 10 != 0 { format_number_pad_zero::<9, _, _>(output, nanos) } else if (nanos / 10) % 10 != 0 { @@ -217,20 +217,13 @@ impl sealed::Sealed for Rfc3339 { } if offset == UtcOffset::UTC { - bytes += write(output, &[b'Z'])?; + bytes += write(output, b"Z")?; return Ok(bytes); } - bytes += write( - output, - if offset.is_negative() { - &[b'-'] - } else { - &[b'+'] - }, - )?; + bytes += write(output, if offset.is_negative() { b"-" } else { b"+" })?; bytes += format_number_pad_zero::<2, _, _>(output, offset.whole_hours().unsigned_abs())?; - bytes += write(output, &[b':'])?; + bytes += write(output, b":")?; bytes += format_number_pad_zero::<2, _, _>(output, offset.minutes_past_hour().unsigned_abs())?; diff --git a/src/formatting/mod.rs b/src/formatting/mod.rs index 19e001289..e5409cc58 100644 --- a/src/formatting/mod.rs +++ b/src/formatting/mod.rs @@ -198,7 +198,7 @@ pub(crate) fn format_number_pad_space< ) -> Result { let mut bytes = 0; for _ in 0..(WIDTH.saturating_sub(value.num_digits())) { - bytes += write(output, &[b' '])?; + bytes += write(output, b" ")?; } bytes += write(output, itoa::Buffer::new().format(value).as_bytes())?; Ok(bytes) @@ -217,7 +217,7 @@ pub(crate) fn format_number_pad_zero< ) -> Result { let mut bytes = 0; for _ in 0..(WIDTH.saturating_sub(value.num_digits())) { - bytes += write(output, &[b'0'])?; + bytes += write(output, b"0")?; } bytes += write(output, itoa::Buffer::new().format(value).as_bytes())?; Ok(bytes) @@ -371,9 +371,9 @@ fn fmt_year( let mut bytes = 0; if repr != modifier::YearRepr::LastTwo { if full_year < 0 { - bytes += write(output, &[b'-'])?; + bytes += write(output, b"-")?; } else if sign_is_mandatory || cfg!(feature = "large-dates") && full_year >= 10_000 { - bytes += write(output, &[b'+'])?; + bytes += write(output, b"+")?; } } bytes += format_number(output, value.unsigned_abs(), padding)?; @@ -478,9 +478,9 @@ fn fmt_offset_hour( ) -> Result { let mut bytes = 0; if offset.is_negative() { - bytes += write(output, &[b'-'])?; + bytes += write(output, b"-")?; } else if sign_is_mandatory { - bytes += write(output, &[b'+'])?; + bytes += write(output, b"+")?; } bytes += format_number::<2, _, _>(output, offset.whole_hours().unsigned_abs(), padding)?; Ok(bytes) diff --git a/time-macros/src/format_description/parse.rs b/time-macros/src/format_description/parse.rs index 79a39395f..19c7bf608 100644 --- a/time-macros/src/format_description/parse.rs +++ b/time-macros/src/format_description/parse.rs @@ -43,12 +43,12 @@ fn parse_item<'a>( if let [b'[', b'[', remaining @ ..] = s { *index += 2; return Ok(ParsedItem { - item: FormatItem::Literal(&[b'[']), + item: FormatItem::Literal(b"["), remaining, }); }; - if s.starts_with(&[b'[']) { + if s.starts_with(b"[") { if let Some(bracket_index) = s.iter().position(|&c| c == b']') { *index += 1; // opening bracket let ret_val = ParsedItem {