]> git.lizzy.rs Git - rust.git/commitdiff
Touch up print_string
authorDavid Tolnay <dtolnay@gmail.com>
Thu, 20 Jan 2022 02:46:49 +0000 (18:46 -0800)
committerDavid Tolnay <dtolnay@gmail.com>
Thu, 20 Jan 2022 03:04:33 +0000 (19:04 -0800)
compiler/rustc_ast_pretty/src/pp.rs

index dadfac06847f47b22f2d5a4d9878fef344db2e3a..11114b5322077a425b7a6e78aa12dc1cefa6972c 100644 (file)
 use ring::RingBuffer;
 use std::borrow::Cow;
 use std::collections::VecDeque;
+use std::iter;
 
 /// How to break. Described in more detail in the module docs.
 #[derive(Clone, Copy, PartialEq)]
@@ -425,10 +426,6 @@ fn print_break(&mut self, token: BreakToken, size: isize) {
     }
 
     fn print_string(&mut self, string: &str) {
-        let len = string.len() as isize;
-        // assert!(len <= space);
-        self.space -= len;
-
         // Write the pending indent. A more concise way of doing this would be:
         //
         //   write!(self.out, "{: >n$}", "", n = self.pending_indentation as usize)?;
@@ -436,9 +433,11 @@ fn print_string(&mut self, string: &str) {
         // But that is significantly slower. This code is sufficiently hot, and indents can get
         // sufficiently large, that the difference is significant on some workloads.
         self.out.reserve(self.pending_indentation as usize);
-        self.out.extend(std::iter::repeat(' ').take(self.pending_indentation as usize));
+        self.out.extend(iter::repeat(' ').take(self.pending_indentation as usize));
         self.pending_indentation = 0;
+
         self.out.push_str(string);
+        self.space -= string.len() as isize;
     }
 
     // Convenience functions to talk to the printer.