]> git.lizzy.rs Git - rust.git/blobdiff - src/shape.rs
Merge pull request #2138 from topecongiro/comments-around-trait-bounds
[rust.git] / src / shape.rs
index b5d57997e870672dbe3f9de0e177621d99bbdbcb..fffbe2b9913ea5a392c81813e1e2eff0b21557ed 100644 (file)
@@ -8,6 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+use std::borrow::Cow;
 use std::ops::{Add, Sub};
 
 use Config;
@@ -21,6 +22,10 @@ pub struct Indent {
     pub alignment: usize,
 }
 
+// INDENT_BUFFER.len() = 80
+const INDENT_BUFFER_LEN: usize = 80;
+const INDENT_BUFFER: &str =
+    "                                                                                ";
 impl Indent {
     pub fn new(block_indent: usize, alignment: usize) -> Indent {
         Indent {
@@ -68,21 +73,25 @@ pub fn width(&self) -> usize {
         self.block_indent + self.alignment
     }
 
-    pub fn to_string(&self, config: &Config) -> String {
+    pub fn to_string(&self, config: &Config) -> Cow<'static, str> {
         let (num_tabs, num_spaces) = if config.hard_tabs() {
             (self.block_indent / config.tab_spaces(), self.alignment)
         } else {
             (0, self.width())
         };
         let num_chars = num_tabs + num_spaces;
-        let mut indent = String::with_capacity(num_chars);
-        for _ in 0..num_tabs {
-            indent.push('\t')
-        }
-        for _ in 0..num_spaces {
-            indent.push(' ')
+        if num_tabs == 0 && num_chars <= INDENT_BUFFER_LEN {
+            Cow::from(&INDENT_BUFFER[0..num_chars])
+        } else {
+            let mut indent = String::with_capacity(num_chars);
+            for _ in 0..num_tabs {
+                indent.push('\t')
+            }
+            for _ in 0..num_spaces {
+                indent.push(' ')
+            }
+            Cow::from(indent)
         }
-        indent
     }
 }