about summary refs log tree commit diff
path: root/src/libsyntax/parse
diff options
context:
space:
mode:
authorEduard Burtescu <edy.burt@gmail.com>2015-05-13 23:26:09 +0300
committerEduard Burtescu <edy.burt@gmail.com>2015-05-14 01:47:56 +0300
commit0d50b043f748bf9a07b28a8f734327258204e26b (patch)
treedf2296f0595d53d8f611959c8baa5910401a7c61 /src/libsyntax/parse
parent6a59d1824d11b6452463fcc23ad64cd142dfa203 (diff)
downloadrust-0d50b043f748bf9a07b28a8f734327258204e26b.tar.gz
rust-0d50b043f748bf9a07b28a8f734327258204e26b.zip
syntax::parse: optimize file_to_filemap to read a string directly.
Diffstat (limited to 'src/libsyntax/parse')
-rw-r--r--src/libsyntax/parse/mod.rs28
1 files changed, 7 insertions, 21 deletions
diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs
index 0db83c6f1b1..3d27363d6ff 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<Span>)
-    -> Rc<FileMap> {
-    let err = |msg: &str| {
+                       -> Rc<FileMap> {
+    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