Skip to content

Commit bd12cd3

Browse files
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 "+".
1 parent 61d9231 commit bd12cd3

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

Diff for: src/libcore/fmt/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1356,11 +1356,11 @@ impl<'a> Formatter<'a> {
13561356
let mut align = old_align;
13571357
if self.sign_aware_zero_pad() {
13581358
// a sign always goes first
1359-
let sign = unsafe { str::from_utf8_unchecked(formatted.sign) };
1359+
let sign = formatted.sign;
13601360
self.buf.write_str(sign)?;
13611361

13621362
// remove the sign from the formatted parts
1363-
formatted.sign = b"";
1363+
formatted.sign = "";
13641364
width = width.saturating_sub(sign.len());
13651365
align = rt::v1::Alignment::Right;
13661366
self.fill = '0';
@@ -1392,7 +1392,7 @@ impl<'a> Formatter<'a> {
13921392
}
13931393

13941394
if !formatted.sign.is_empty() {
1395-
write_bytes(self.buf, formatted.sign)?;
1395+
self.buf.write_str(formatted.sign)?;
13961396
}
13971397
for part in formatted.parts {
13981398
match *part {

Diff for: src/libcore/fmt/num.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -369,11 +369,11 @@ macro_rules! impl_Exp {
369369
flt2dec::Part::Copy(exp_slice)
370370
];
371371
let sign = if !is_nonnegative {
372-
&b"-"[..]
372+
"-"
373373
} else if f.sign_plus() {
374-
&b"+"[..]
374+
"+"
375375
} else {
376-
&b""[..]
376+
""
377377
};
378378
let formatted = flt2dec::Formatted{sign, parts};
379379
f.pad_formatted_parts(&formatted)

Diff for: src/libcore/num/flt2dec/mod.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ impl<'a> Part<'a> {
237237
#[derive(Clone)]
238238
pub struct Formatted<'a> {
239239
/// A byte slice representing a sign, either `""`, `"-"` or `"+"`.
240-
pub sign: &'static [u8],
240+
pub sign: &'static str,
241241
/// Formatted parts to be rendered after a sign and optional zero padding.
242242
pub parts: &'a [Part<'a>],
243243
}
@@ -259,7 +259,7 @@ impl<'a> Formatted<'a> {
259259
if out.len() < self.sign.len() {
260260
return None;
261261
}
262-
out[..self.sign.len()].copy_from_slice(self.sign);
262+
out[..self.sign.len()].copy_from_slice(self.sign.as_bytes());
263263

264264
let mut written = self.sign.len();
265265
for part in self.parts {
@@ -402,38 +402,38 @@ pub enum Sign {
402402
}
403403

404404
/// Returns the static byte string corresponding to the sign to be formatted.
405-
/// It can be either `b""`, `b"+"` or `b"-"`.
406-
fn determine_sign(sign: Sign, decoded: &FullDecoded, negative: bool) -> &'static [u8] {
405+
/// It can be either `""`, `"+"` or `"-"`.
406+
fn determine_sign(sign: Sign, decoded: &FullDecoded, negative: bool) -> &'static str {
407407
match (*decoded, sign) {
408-
(FullDecoded::Nan, _) => b"",
409-
(FullDecoded::Zero, Sign::Minus) => b"",
408+
(FullDecoded::Nan, _) => "",
409+
(FullDecoded::Zero, Sign::Minus) => "",
410410
(FullDecoded::Zero, Sign::MinusRaw) => {
411411
if negative {
412-
b"-"
412+
"-"
413413
} else {
414-
b""
414+
""
415415
}
416416
}
417-
(FullDecoded::Zero, Sign::MinusPlus) => b"+",
417+
(FullDecoded::Zero, Sign::MinusPlus) => "+",
418418
(FullDecoded::Zero, Sign::MinusPlusRaw) => {
419419
if negative {
420-
b"-"
420+
"-"
421421
} else {
422-
b"+"
422+
"+"
423423
}
424424
}
425425
(_, Sign::Minus) | (_, Sign::MinusRaw) => {
426426
if negative {
427-
b"-"
427+
"-"
428428
} else {
429-
b""
429+
""
430430
}
431431
}
432432
(_, Sign::MinusPlus) | (_, Sign::MinusPlusRaw) => {
433433
if negative {
434-
b"-"
434+
"-"
435435
} else {
436-
b"+"
436+
"+"
437437
}
438438
}
439439
}

0 commit comments

Comments
 (0)