diff options
| author | Nick Fitzgerald <fitzgen@gmail.com> | 2019-02-07 13:02:27 +0100 |
|---|---|---|
| committer | Nick Fitzgerald <fitzgen@gmail.com> | 2019-02-07 13:02:27 +0100 |
| commit | ed2157a38f6ccdfe460f2f058f60a67daac6cc5a (patch) | |
| tree | dfa597e439b48b0a7b26d75ffddfe5709f2e1f24 | |
| parent | da5a0cd69c64e0772960caee32f36b11bca1b498 (diff) | |
| download | rust-ed2157a38f6ccdfe460f2f058f60a67daac6cc5a.tar.gz rust-ed2157a38f6ccdfe460f2f058f60a67daac6cc5a.zip | |
De-duplicate write_prefix lambda in pad_integral
For smaller code size.
| -rw-r--r-- | src/libcore/fmt/mod.rs | 31 |
1 files changed, 20 insertions, 11 deletions
diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index 530b2f52c0d..dd95e3b4a7c 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -1153,38 +1153,46 @@ impl<'a> Formatter<'a> { sign = Some('+'); width += 1; } - let prefixed = self.alternate(); - if prefixed { + let prefix = if self.alternate() { width += prefix.chars().count(); - } + Some(prefix) + } else { + None + }; // Writes the sign if it exists, and then the prefix if it was requested - let write_prefix = |f: &mut Formatter| { + #[inline(never)] + fn write_prefix(f: &mut Formatter, sign: Option<char>, prefix: Option<&str>) -> Result { if let Some(c) = sign { f.buf.write_char(c)?; } - if prefixed { f.buf.write_str(prefix) } - else { Ok(()) } - }; + if let Some(prefix) = prefix { + f.buf.write_str(prefix) + } else { + Ok(()) + } + } // The `width` field is more of a `min-width` parameter at this point. match self.width { // If there's no minimum length requirements then we can just // write the bytes. None => { - write_prefix(self)?; self.buf.write_str(buf) + write_prefix(self, sign, prefix)?; + self.buf.write_str(buf) } // Check if we're over the minimum width, if so then we can also // just write the bytes. Some(min) if width >= min => { - write_prefix(self)?; self.buf.write_str(buf) + write_prefix(self, sign, prefix)?; + self.buf.write_str(buf) } // The sign and prefix goes before the padding if the fill character // is zero Some(min) if self.sign_aware_zero_pad() => { self.fill = '0'; self.align = rt::v1::Alignment::Right; - write_prefix(self)?; + write_prefix(self, sign, prefix)?; self.with_padding(min - width, rt::v1::Alignment::Right, |f| { f.buf.write_str(buf) }) @@ -1192,7 +1200,8 @@ impl<'a> Formatter<'a> { // Otherwise, the sign and prefix goes after the padding Some(min) => { self.with_padding(min - width, rt::v1::Alignment::Right, |f| { - write_prefix(f)?; f.buf.write_str(buf) + write_prefix(f, sign, prefix)?; + f.buf.write_str(buf) }) } } |
