Skip to content

Commit 0d105c0

Browse files
authored
Rollup merge of #88560 - klensy:formatter-pad-shrink, r=m-ou-se
`fmt::Formatter::pad`: don't call chars().count() more than one time First commit merges two branches of match to call chars().count() only once: that should be faster if this method hits place of 3rd (previous) branch, plus quarter shorter. Second commit fixes some clippy lints while i'm here (should it be separate PR?).
2 parents ffbce26 + f5f489b commit 0d105c0

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

library/core/src/fmt/mod.rs

+17-12
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ impl<'a> Arguments<'a> {
402402

403403
if self.args.is_empty() {
404404
pieces_length
405-
} else if self.pieces[0] == "" && pieces_length < 16 {
405+
} else if !self.pieces.is_empty() && self.pieces[0].is_empty() && pieces_length < 16 {
406406
// If the format string starts with an argument,
407407
// don't preallocate anything, unless length
408408
// of pieces is significant.
@@ -1163,7 +1163,7 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
11631163
}
11641164
// SAFETY: arg and args.args come from the same Arguments,
11651165
// which guarantees the indexes are always within bounds.
1166-
unsafe { run(&mut formatter, arg, &args.args) }?;
1166+
unsafe { run(&mut formatter, arg, args.args) }?;
11671167
idx += 1;
11681168
}
11691169
}
@@ -1409,7 +1409,7 @@ impl<'a> Formatter<'a> {
14091409
// we know that it can't panic. Use `get` + `unwrap_or` to avoid
14101410
// `unsafe` and otherwise don't emit any panic-related code
14111411
// here.
1412-
s.get(..i).unwrap_or(&s)
1412+
s.get(..i).unwrap_or(s)
14131413
} else {
14141414
&s
14151415
}
@@ -1421,16 +1421,21 @@ impl<'a> Formatter<'a> {
14211421
// If we're under the maximum length, and there's no minimum length
14221422
// requirements, then we can just emit the string
14231423
None => self.buf.write_str(s),
1424-
// If we're under the maximum width, check if we're over the minimum
1425-
// width, if so it's as easy as just emitting the string.
1426-
Some(width) if s.chars().count() >= width => self.buf.write_str(s),
1427-
// If we're under both the maximum and the minimum width, then fill
1428-
// up the minimum width with the specified string + some alignment.
14291424
Some(width) => {
1430-
let align = rt::v1::Alignment::Left;
1431-
let post_padding = self.padding(width - s.chars().count(), align)?;
1432-
self.buf.write_str(s)?;
1433-
post_padding.write(self.buf)
1425+
let chars_count = s.chars().count();
1426+
// If we're under the maximum width, check if we're over the minimum
1427+
// width, if so it's as easy as just emitting the string.
1428+
if chars_count >= width {
1429+
self.buf.write_str(s)
1430+
}
1431+
// If we're under both the maximum and the minimum width, then fill
1432+
// up the minimum width with the specified string + some alignment.
1433+
else {
1434+
let align = rt::v1::Alignment::Left;
1435+
let post_padding = self.padding(width - chars_count, align)?;
1436+
self.buf.write_str(s)?;
1437+
post_padding.write(self.buf)
1438+
}
14341439
}
14351440
}
14361441
}

0 commit comments

Comments
 (0)