Skip to content

Commit

Permalink
Use byte string literals where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed Aug 16, 2022
1 parent d385f05 commit d358131
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/format_description/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 9 additions & 16 deletions src/formatting/formattable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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())?;

Expand Down
12 changes: 6 additions & 6 deletions src/formatting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ pub(crate) fn format_number_pad_space<
) -> Result<usize, io::Error> {
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)
Expand All @@ -217,7 +217,7 @@ pub(crate) fn format_number_pad_zero<
) -> Result<usize, io::Error> {
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)
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -478,9 +478,9 @@ fn fmt_offset_hour(
) -> Result<usize, io::Error> {
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)
Expand Down
4 changes: 2 additions & 2 deletions time-macros/src/format_description/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit d358131

Please sign in to comment.