Skip to content

Commit

Permalink
syntax: Avoid reallocating or copying in CodeMap::new_filemap
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Ulrik Sverdrup committed May 1, 2015
1 parent ee48e6d commit da03c9d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/libsyntax/codemap.rs
Expand Up @@ -543,21 +543,17 @@ impl 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,
Some(last) => last.end_pos.to_usize(),
};

// 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
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/lib.rs
Expand Up @@ -27,6 +27,7 @@

#![feature(associated_consts)]
#![feature(collections)]
#![feature(collections_drain)]
#![feature(core)]
#![feature(libc)]
#![feature(rustc_private)]
Expand Down

0 comments on commit da03c9d

Please sign in to comment.