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 "+".
This commit is contained in:
parent
61d9231ff2
commit
bd12cd3d2f
3 changed files with 21 additions and 21 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -237,7 +237,7 @@ impl<'a> Part<'a> {
|
|||
#[derive(Clone)]
|
||||
pub struct Formatted<'a> {
|
||||
/// A byte slice representing a sign, either `""`, `"-"` or `"+"`.
|
||||
pub sign: &'static [u8],
|
||||
pub sign: &'static str,
|
||||
/// Formatted parts to be rendered after a sign and optional zero padding.
|
||||
pub parts: &'a [Part<'a>],
|
||||
}
|
||||
|
|
@ -259,7 +259,7 @@ impl<'a> Formatted<'a> {
|
|||
if out.len() < self.sign.len() {
|
||||
return None;
|
||||
}
|
||||
out[..self.sign.len()].copy_from_slice(self.sign);
|
||||
out[..self.sign.len()].copy_from_slice(self.sign.as_bytes());
|
||||
|
||||
let mut written = self.sign.len();
|
||||
for part in self.parts {
|
||||
|
|
@ -402,38 +402,38 @@ pub enum Sign {
|
|||
}
|
||||
|
||||
/// Returns the static byte string corresponding to the sign to be formatted.
|
||||
/// It can be either `b""`, `b"+"` or `b"-"`.
|
||||
fn determine_sign(sign: Sign, decoded: &FullDecoded, negative: bool) -> &'static [u8] {
|
||||
/// It can be either `""`, `"+"` or `"-"`.
|
||||
fn determine_sign(sign: Sign, decoded: &FullDecoded, negative: bool) -> &'static str {
|
||||
match (*decoded, sign) {
|
||||
(FullDecoded::Nan, _) => b"",
|
||||
(FullDecoded::Zero, Sign::Minus) => b"",
|
||||
(FullDecoded::Nan, _) => "",
|
||||
(FullDecoded::Zero, Sign::Minus) => "",
|
||||
(FullDecoded::Zero, Sign::MinusRaw) => {
|
||||
if negative {
|
||||
b"-"
|
||||
"-"
|
||||
} else {
|
||||
b""
|
||||
""
|
||||
}
|
||||
}
|
||||
(FullDecoded::Zero, Sign::MinusPlus) => b"+",
|
||||
(FullDecoded::Zero, Sign::MinusPlus) => "+",
|
||||
(FullDecoded::Zero, Sign::MinusPlusRaw) => {
|
||||
if negative {
|
||||
b"-"
|
||||
"-"
|
||||
} else {
|
||||
b"+"
|
||||
"+"
|
||||
}
|
||||
}
|
||||
(_, Sign::Minus) | (_, Sign::MinusRaw) => {
|
||||
if negative {
|
||||
b"-"
|
||||
"-"
|
||||
} else {
|
||||
b""
|
||||
""
|
||||
}
|
||||
}
|
||||
(_, Sign::MinusPlus) | (_, Sign::MinusPlusRaw) => {
|
||||
if negative {
|
||||
b"-"
|
||||
"-"
|
||||
} else {
|
||||
b"+"
|
||||
"+"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue