]> git.lizzy.rs Git - rust.git/commitdiff
syntax: Avoid reallocating or copying in CodeMap::new_filemap
authorUlrik Sverdrup <root@localhost>
Fri, 1 May 2015 15:08:39 +0000 (17:08 +0200)
committerUlrik Sverdrup <root@localhost>
Fri, 1 May 2015 17:51:31 +0000 (19:51 +0200)
Avoid creating a new String when there is no BOM to strip, and
otherwises use .drain(..3) to strip the BOM using the same allocation.

src/libsyntax/codemap.rs
src/libsyntax/lib.rs

index 5e0cb647c8b41072ff2242d9c4dc9fcc7208f454..decc6f01eefff89259bbd2c33293f1e8a9b37cde 100644 (file)
@@ -543,7 +543,7 @@ pub fn new() -> CodeMap {
         }
     }
 
-    pub fn new_filemap(&self, filename: FileName, src: String) -> Rc<FileMap> {
+    pub fn new_filemap(&self, filename: FileName, mut src: String) -> Rc<FileMap> {
         let mut files = self.files.borrow_mut();
         let start_pos = match files.last() {
             None => 0,
@@ -551,13 +551,9 @@ pub fn new_filemap(&self, filename: FileName, src: String) -> Rc<FileMap> {
         };
 
         // Remove utf-8 BOM if any.
-        // FIXME #12884: no efficient/safe way to remove from the start of a string
-        // and reuse the allocation.
-        let mut src = if src.starts_with("\u{feff}") {
-            String::from(&src[3..])
-        } else {
-            String::from(&src[..])
-        };
+        if src.starts_with("\u{feff}") {
+            src.drain(..3);
+        }
 
         // Append '\n' in case it's not already there.
         // This is a workaround to prevent CodeMap.lookup_filemap_idx from
index 275400009f5aafa4b32c72acd145b421863bd57b..330fe86deeb37e43d3e4ef10f1d39c574223c1a0 100644 (file)
@@ -27,6 +27,7 @@
 
 #![feature(associated_consts)]
 #![feature(collections)]
+#![feature(collections_drain)]
 #![feature(core)]
 #![feature(libc)]
 #![feature(rustc_private)]