Skip to content

Commit

Permalink
Rollup merge of #100934 - a1phyr:improve_fmt_PadAdapter, r=Mark-Simul…
Browse files Browse the repository at this point in the history
…acrum

Remove a panicking branch from `fmt::builders::PadAdapter`
  • Loading branch information
Dylan-DPC authored Aug 29, 2022
2 parents 348e859 + 3aa6fe3 commit 90c1a8d
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions library/core/src/fmt/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,14 @@ impl<'buf, 'state> PadAdapter<'buf, 'state> {
}

impl fmt::Write for PadAdapter<'_, '_> {
fn write_str(&mut self, mut s: &str) -> fmt::Result {
while !s.is_empty() {
fn write_str(&mut self, s: &str) -> fmt::Result {
for s in s.split_inclusive('\n') {
if self.state.on_newline {
self.buf.write_str(" ")?;
}

let split = match s.find('\n') {
Some(pos) => {
self.state.on_newline = true;
pos + 1
}
None => {
self.state.on_newline = false;
s.len()
}
};
self.buf.write_str(&s[..split])?;
s = &s[split..];
self.state.on_newline = s.ends_with('\n');
self.buf.write_str(s)?;
}

Ok(())
Expand Down

0 comments on commit 90c1a8d

Please sign in to comment.