Rollup merge of #100934 - a1phyr:improve_fmt_PadAdapter, r=Mark-Simulacrum

Remove a panicking branch from `fmt::builders::PadAdapter`
This commit is contained in:
Dylan DPC 2022-08-29 16:49:44 +05:30 committed by GitHub
commit 141728fc61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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(())