Moved FileMap construction to it's own constructor.

The rationale is that BOM stripping is needed for lazy source loading
for external crates, and duplication can be avoided by moving the
corresponding functionality to libsyntax_pos.
上级 3d2cff0c
......@@ -1682,6 +1682,7 @@ dependencies = [
name = "syntax_pos"
version = "0.0.0"
dependencies = [
"rustc_data_structures 0.0.0",
"serialize 0.0.0",
]
......
......@@ -27,12 +27,9 @@
use std::env;
use std::fs;
use std::hash::Hasher;
use std::io::{self, Read};
use errors::CodeMapper;
use rustc_data_structures::stable_hasher::StableHasher;
/// Return the span itself if it doesn't come from a macro expansion,
/// otherwise return the call site span up to the `enclosing_sp` by
/// following the `expn_info` chain.
......@@ -161,34 +158,13 @@ fn next_start_pos(&self) -> usize {
/// Creates a new filemap without setting its line information. If you don't
/// intend to set the line information yourself, you should use new_filemap_and_lines.
pub fn new_filemap(&self, filename: FileName, mut src: String) -> Rc<FileMap> {
pub fn new_filemap(&self, filename: FileName, src: String) -> Rc<FileMap> {
let start_pos = self.next_start_pos();
let mut files = self.files.borrow_mut();
// Remove utf-8 BOM if any.
if src.starts_with("\u{feff}") {
src.drain(..3);
}
let end_pos = start_pos + src.len();
let (filename, was_remapped) = self.path_mapping.map_prefix(filename);
let mut hasher: StableHasher<u128> = StableHasher::new();
hasher.write(src.as_bytes());
let src_hash = hasher.finish();
let filemap = Rc::new(FileMap {
name: filename,
name_was_remapped: was_remapped,
crate_of_origin: 0,
src: Some(Rc::new(src)),
src_hash: src_hash,
start_pos: Pos::from_usize(start_pos),
end_pos: Pos::from_usize(end_pos),
lines: RefCell::new(Vec::new()),
multibyte_chars: RefCell::new(Vec::new()),
});
let filemap =
Rc::new(FileMap::new(filename, was_remapped, src, Pos::from_usize(start_pos)));
files.push(filemap.clone());
......
......@@ -10,3 +10,4 @@ crate-type = ["dylib"]
[dependencies]
serialize = { path = "../libserialize" }
rustc_data_structures = { path = "../librustc_data_structures" }
......@@ -38,6 +38,11 @@
use std::rc::Rc;
use std::cmp;
use std::fmt;
use std::hash::Hasher;
use rustc_data_structures::stable_hasher::StableHasher;
extern crate rustc_data_structures;
use serialize::{Encodable, Decodable, Encoder, Decoder};
......@@ -522,6 +527,31 @@ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
}
impl FileMap {
pub fn new(name: FileName,
name_was_remapped: bool,
mut src: String,
start_pos: BytePos) -> FileMap {
remove_bom(&mut src);
let mut hasher: StableHasher<u128> = StableHasher::new();
hasher.write(src.as_bytes());
let src_hash = hasher.finish();
let end_pos = start_pos.to_usize() + src.len();
FileMap {
name: name,
name_was_remapped: name_was_remapped,
crate_of_origin: 0,
src: Some(Rc::new(src)),
src_hash: src_hash,
start_pos: start_pos,
end_pos: Pos::from_usize(end_pos),
lines: RefCell::new(Vec::new()),
multibyte_chars: RefCell::new(Vec::new()),
}
}
/// EFFECT: register a start-of-line offset in the
/// table of line-beginnings.
/// UNCHECKED INVARIANT: these offsets must be added in the right
......@@ -621,6 +651,13 @@ pub fn line_bounds(&self, line_index: usize) -> (BytePos, BytePos) {
}
}
/// Remove utf-8 BOM if any.
fn remove_bom(src: &mut String) {
if src.starts_with("\u{feff}") {
src.drain(..3);
}
}
// _____________________________________________________________________________
// Pos, BytePos, CharPos
//
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册