From 0d50b043f748bf9a07b28a8f734327258204e26b Mon Sep 17 00:00:00 2001 From: Eduard Burtescu Date: Wed, 13 May 2015 23:26:09 +0300 Subject: [PATCH] syntax::parse: optimize file_to_filemap to read a string directly. --- src/libsyntax/parse/mod.rs | 28 +++++++--------------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 0db83c6f1b137..3d27363d6ff20 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -221,30 +221,16 @@ pub fn new_parser_from_tts<'a>(sess: &'a ParseSess, /// Given a session and a path and an optional span (for error reporting), /// add the path to the session's codemap and return the new filemap. pub fn file_to_filemap(sess: &ParseSess, path: &Path, spanopt: Option) - -> Rc { - let err = |msg: &str| { + -> Rc { + let mut contents = String::new(); + if let Err(e) = File::open(path).and_then(|mut f| f.read_to_string(&mut contents)) { + let msg = format!("couldn't read {:?}: {}", path.display(), e); match spanopt { - Some(sp) => panic!(sess.span_diagnostic.span_fatal(sp, msg)), - None => sess.span_diagnostic.handler().fatal(msg), - } - }; - let mut bytes = Vec::new(); - match File::open(path).and_then(|mut f| f.read_to_end(&mut bytes)) { - Ok(..) => {} - Err(e) => { - err(&format!("couldn't read {:?}: {}", path.display(), e)); - unreachable!(); - } - }; - match str::from_utf8(&bytes[..]).ok() { - Some(s) => { - sess.codemap().new_filemap(path.to_str().unwrap().to_string(), s.to_string()) - } - None => { - err(&format!("{:?} is not UTF-8 encoded", path.display())); - unreachable!(); + Some(sp) => panic!(sess.span_diagnostic.span_fatal(sp, &msg)), + None => sess.span_diagnostic.handler().fatal(&msg) } } + sess.codemap().new_filemap(path.to_str().unwrap().to_string(), contents) } /// Given a filemap, produce a sequence of token-trees