fmt::Formatter::pad
: don't call chars().count() more than one time
This commit is contained in:
parent
608b5e1c20
commit
6c9e708f4b
1 changed files with 14 additions and 9 deletions
|
@ -1421,16 +1421,21 @@ impl<'a> Formatter<'a> {
|
||||||
// If we're under the maximum length, and there's no minimum length
|
// If we're under the maximum length, and there's no minimum length
|
||||||
// requirements, then we can just emit the string
|
// requirements, then we can just emit the string
|
||||||
None => self.buf.write_str(s),
|
None => self.buf.write_str(s),
|
||||||
// If we're under the maximum width, check if we're over the minimum
|
|
||||||
// width, if so it's as easy as just emitting the string.
|
|
||||||
Some(width) if s.chars().count() >= width => self.buf.write_str(s),
|
|
||||||
// If we're under both the maximum and the minimum width, then fill
|
|
||||||
// up the minimum width with the specified string + some alignment.
|
|
||||||
Some(width) => {
|
Some(width) => {
|
||||||
let align = rt::v1::Alignment::Left;
|
let chars_count = s.chars().count();
|
||||||
let post_padding = self.padding(width - s.chars().count(), align)?;
|
// If we're under the maximum width, check if we're over the minimum
|
||||||
self.buf.write_str(s)?;
|
// width, if so it's as easy as just emitting the string.
|
||||||
post_padding.write(self.buf)
|
if chars_count >= width {
|
||||||
|
self.buf.write_str(s)
|
||||||
|
}
|
||||||
|
// If we're under both the maximum and the minimum width, then fill
|
||||||
|
// up the minimum width with the specified string + some alignment.
|
||||||
|
else {
|
||||||
|
let align = rt::v1::Alignment::Left;
|
||||||
|
let post_padding = self.padding(width - chars_count, align)?;
|
||||||
|
self.buf.write_str(s)?;
|
||||||
|
post_padding.write(self.buf)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue