Skip to content

Commit 20e178c

Browse files
liigoalexcrichton
authored andcommitted
libsyntax: librustdoc: ignore utf-8 BOM in .rs files
Closes #12974
1 parent 8f7a797 commit 20e178c

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/librustdoc/html/render.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,13 @@ impl<'a> SourceCollector<'a> {
463463
};
464464
let contents = str::from_utf8_owned(contents).unwrap();
465465

466+
// Remove the utf-8 BOM if any
467+
let contents = if contents.starts_with("\ufeff") {
468+
contents.as_slice().slice_from(3)
469+
} else {
470+
contents.as_slice()
471+
};
472+
466473
// Create the intermediate directories
467474
let mut cur = self.dst.clone();
468475
let mut root_path = ~"../../";
@@ -482,7 +489,7 @@ impl<'a> SourceCollector<'a> {
482489
root_path: root_path,
483490
};
484491
try!(layout::render(&mut w as &mut Writer, &self.cx.layout,
485-
&page, &(""), &Source(contents.as_slice())));
492+
&page, &(""), &Source(contents)));
486493
try!(w.flush());
487494
return Ok(());
488495
}

src/libsyntax/codemap.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -271,13 +271,22 @@ impl CodeMap {
271271
}
272272
}
273273

274-
pub fn new_filemap(&self, filename: FileName, mut src: ~str) -> Rc<FileMap> {
274+
pub fn new_filemap(&self, filename: FileName, src: ~str) -> Rc<FileMap> {
275275
let mut files = self.files.borrow_mut();
276276
let start_pos = match files.get().last() {
277277
None => 0,
278278
Some(last) => last.deref().start_pos.to_uint() + last.deref().src.len(),
279279
};
280280

281+
// Remove utf-8 BOM if any.
282+
// FIXME #12884: no efficient/safe way to remove from the start of a string
283+
// and reuse the allocation.
284+
let mut src = if src.starts_with("\ufeff") {
285+
src.as_slice().slice_from(3).into_owned()
286+
} else {
287+
src
288+
};
289+
281290
// Append '\n' in case it's not already there.
282291
// This is a workaround to prevent CodeMap.lookup_filemap_idx from accidentally
283292
// overflowing into the next filemap in case the last byte of span is also the last

src/test/run-pass/utf8-bom.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// This file has utf-8 BOM, it should be compiled normally without error.
12+
13+
pub fn main() {}

0 commit comments

Comments
 (0)