diff options
| author | Mark Rousskov <mark.simulacrum@gmail.com> | 2020-02-15 18:06:39 -0500 |
|---|---|---|
| committer | Mark Rousskov <mark.simulacrum@gmail.com> | 2020-02-15 18:17:51 -0500 |
| commit | bd12cd3d2fe2fd69cff05d4b710c8020dea2cdf7 (patch) | |
| tree | b3c40b22aa5dc50853794108093a4efb447954d7 /src/libcore/fmt | |
| parent | 61d9231ff2604a0467987042d9ebf9ff9ea739b5 (diff) | |
| download | rust-bd12cd3d2fe2fd69cff05d4b710c8020dea2cdf7.tar.gz rust-bd12cd3d2fe2fd69cff05d4b710c8020dea2cdf7.zip | |
Formatter::sign is &'static str
The contents were always UTF-8 anyway, and &str has an equivalent representation to &[u8], so this should not affect performance while removing unsafety at edges. It may be worth exploring a further adjustment that stores a single byte (instead of 16) as the contents are always "", "-", or "+".
Diffstat (limited to 'src/libcore/fmt')
| -rw-r--r-- | src/libcore/fmt/mod.rs | 6 | ||||
| -rw-r--r-- | src/libcore/fmt/num.rs | 6 |
2 files changed, 6 insertions, 6 deletions
diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index 973c2f2b915..3f2c9654706 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -1356,11 +1356,11 @@ impl<'a> Formatter<'a> { let mut align = old_align; if self.sign_aware_zero_pad() { // a sign always goes first - let sign = unsafe { str::from_utf8_unchecked(formatted.sign) }; + let sign = formatted.sign; self.buf.write_str(sign)?; // remove the sign from the formatted parts - formatted.sign = b""; + formatted.sign = ""; width = width.saturating_sub(sign.len()); align = rt::v1::Alignment::Right; self.fill = '0'; @@ -1392,7 +1392,7 @@ impl<'a> Formatter<'a> { } if !formatted.sign.is_empty() { - write_bytes(self.buf, formatted.sign)?; + self.buf.write_str(formatted.sign)?; } for part in formatted.parts { match *part { diff --git a/src/libcore/fmt/num.rs b/src/libcore/fmt/num.rs index c187471fb5f..5dfd3a8ecdb 100644 --- a/src/libcore/fmt/num.rs +++ b/src/libcore/fmt/num.rs @@ -369,11 +369,11 @@ macro_rules! impl_Exp { flt2dec::Part::Copy(exp_slice) ]; let sign = if !is_nonnegative { - &b"-"[..] + "-" } else if f.sign_plus() { - &b"+"[..] + "+" } else { - &b""[..] + "" }; let formatted = flt2dec::Formatted{sign, parts}; f.pad_formatted_parts(&formatted) |
