]> git.lizzy.rs Git - rust.git/commitdiff
libsyntax: librustdoc: ignore utf-8 BOM in .rs files
authorLiigo Zhuang <com.liigo@gmail.com>
Tue, 18 Mar 2014 00:59:44 +0000 (08:59 +0800)
committerAlex Crichton <alex@alexcrichton.com>
Tue, 18 Mar 2014 20:49:11 +0000 (13:49 -0700)
Closes #12974

src/librustdoc/html/render.rs
src/libsyntax/codemap.rs
src/test/run-pass/utf8-bom.rs [new file with mode: 0644]

index 1ebb51cb65e9f7299a2d953a61f3325a3926e826..6ad7b7d9da1229f7b6e971f30acffdf120b8ea31 100644 (file)
@@ -463,6 +463,13 @@ fn emit_source(&mut self, filename: &str) -> io::IoResult<()> {
         };
         let contents = str::from_utf8_owned(contents).unwrap();
 
+        // Remove the utf-8 BOM if any
+        let contents = if contents.starts_with("\ufeff") {
+            contents.as_slice().slice_from(3)
+        } else {
+            contents.as_slice()
+        };
+
         // Create the intermediate directories
         let mut cur = self.dst.clone();
         let mut root_path = ~"../../";
@@ -482,7 +489,7 @@ fn emit_source(&mut self, filename: &str) -> io::IoResult<()> {
             root_path: root_path,
         };
         try!(layout::render(&mut w as &mut Writer, &self.cx.layout,
-                              &page, &(""), &Source(contents.as_slice())));
+                              &page, &(""), &Source(contents)));
         try!(w.flush());
         return Ok(());
     }
index 4bfd5391a8f10d1e1642b788cd592c85d3c4df69..d93b5803eac30ac34d2e703fd83e1719e7343469 100644 (file)
@@ -271,13 +271,22 @@ pub fn new() -> CodeMap {
         }
     }
 
-    pub fn new_filemap(&self, filename: FileName, mut src: ~str) -> Rc<FileMap> {
+    pub fn new_filemap(&self, filename: FileName, src: ~str) -> Rc<FileMap> {
         let mut files = self.files.borrow_mut();
         let start_pos = match files.get().last() {
             None => 0,
             Some(last) => last.deref().start_pos.to_uint() + last.deref().src.len(),
         };
 
+        // 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("\ufeff") {
+            src.as_slice().slice_from(3).into_owned()
+        } else {
+            src
+        };
+
         // Append '\n' in case it's not already there.
         // This is a workaround to prevent CodeMap.lookup_filemap_idx from accidentally
         // overflowing into the next filemap in case the last byte of span is also the last
diff --git a/src/test/run-pass/utf8-bom.rs b/src/test/run-pass/utf8-bom.rs
new file mode 100644 (file)
index 0000000..ccd40cb
--- /dev/null
@@ -0,0 +1,13 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// This file has utf-8 BOM, it should be compiled normally without error.
+
+pub fn main() {}