Skip to content

Commit da03c9d

Browse files
author
Ulrik Sverdrup
committed
syntax: Avoid reallocating or copying in CodeMap::new_filemap
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.
1 parent ee48e6d commit da03c9d

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

src/libsyntax/codemap.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -543,21 +543,17 @@ impl CodeMap {
543543
}
544544
}
545545

546-
pub fn new_filemap(&self, filename: FileName, src: String) -> Rc<FileMap> {
546+
pub fn new_filemap(&self, filename: FileName, mut src: String) -> Rc<FileMap> {
547547
let mut files = self.files.borrow_mut();
548548
let start_pos = match files.last() {
549549
None => 0,
550550
Some(last) => last.end_pos.to_usize(),
551551
};
552552

553553
// Remove utf-8 BOM if any.
554-
// FIXME #12884: no efficient/safe way to remove from the start of a string
555-
// and reuse the allocation.
556-
let mut src = if src.starts_with("\u{feff}") {
557-
String::from(&src[3..])
558-
} else {
559-
String::from(&src[..])
560-
};
554+
if src.starts_with("\u{feff}") {
555+
src.drain(..3);
556+
}
561557

562558
// Append '\n' in case it's not already there.
563559
// This is a workaround to prevent CodeMap.lookup_filemap_idx from

src/libsyntax/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#![feature(associated_consts)]
2929
#![feature(collections)]
30+
#![feature(collections_drain)]
3031
#![feature(core)]
3132
#![feature(libc)]
3233
#![feature(rustc_private)]

0 commit comments

Comments
 (0)