Skip to content

Commit

Permalink
Use write_char for writing padding characters
Browse files Browse the repository at this point in the history
Removes some unsafe *and* saves almost half a kilobyte of code size.
  • Loading branch information
fitzgen committed Feb 8, 2019
1 parent 05df9ff commit 8fea705
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 23 deletions.
30 changes: 8 additions & 22 deletions src/libcore/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1039,24 +1039,19 @@ pub fn write(output: &mut dyn Write, args: Arguments) -> Result {
/// Padding after the end of something. Returned by `Formatter::padding`.
#[must_use = "don't forget to write the post padding"]
struct PostPadding {
fill: [u8; 4],
fill_len: u32,
fill: char,
padding: usize,
}

impl PostPadding {
/// Safety relies on `fill[..fill_len]` being a valid UTF-8 char.
unsafe fn new(fill: [u8; 4], fill_len: u32, padding: usize) -> PostPadding {
PostPadding { fill, fill_len, padding }
fn new(fill: char, padding: usize) -> PostPadding {
PostPadding { fill, padding }
}

/// Write this post padding.
fn write(self, buf: &mut dyn Write) -> Result {
let fill = unsafe {
str::from_utf8_unchecked(&self.fill.get_unchecked(..self.fill_len as usize))
};
for _ in 0..self.padding {
buf.write_str(fill)?;
buf.write_char(self.fill)?;
}
Ok(())
}
Expand Down Expand Up @@ -1326,20 +1321,11 @@ impl<'a> Formatter<'a> {
rt::v1::Alignment::Center => (padding / 2, (padding + 1) / 2),
};

let mut fill = [0; 4];
let fill_len = {
let fill = self.fill.encode_utf8(&mut fill);

for _ in 0..pre_pad {
self.buf.write_str(fill)?;
}

fill.len()
};
for _ in 0..pre_pad {
self.buf.write_char(self.fill)?;
}

Ok(unsafe {
PostPadding::new(fill, fill_len as u32, post_pad)
})
Ok(PostPadding::new(self.fill, post_pad))
}

/// Takes the formatted parts and applies the padding.
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-make/wasm-stringify-ints-small/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ifeq ($(TARGET),wasm32-unknown-unknown)
all:
$(RUSTC) foo.rs -C lto -O --target wasm32-unknown-unknown
wc -c < $(TMPDIR)/foo.wasm
[ "`wc -c < $(TMPDIR)/foo.wasm`" -lt "21000" ]
[ "`wc -c < $(TMPDIR)/foo.wasm`" -lt "20500" ]
else
all:
endif

0 comments on commit 8fea705

Please sign in to comment.