about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorTim Chevalier <chevalier@alum.wellesley.edu>2012-04-27 14:21:17 -0700
committerTim Chevalier <chevalier@alum.wellesley.edu>2012-04-30 10:44:31 -0700
commit2bb3b63ec4379b812aeceb690d78763ec55d3cbb (patch)
treef189a83d88971f52d155fb91438a4d8ad81033a4 /src/libcore
parentdc117fecde14706b3ab5fbcd64f743dba6de9e1b (diff)
downloadrust-2bb3b63ec4379b812aeceb690d78763ec55d3cbb.tar.gz
rust-2bb3b63ec4379b812aeceb690d78763ec55d3cbb.zip
Eliminate a copy in syntax::parse::new_parser_from_file
Fixing a FIXME turned out to be pretty involved. I added an io function
that returns a unique boxed string (for the contents of a file) rather than
a string, and went from there. Also made the src field of codemap a unique
boxed string. This doesn't seem to make that much difference in amount of
allocation according to valgrind (disappointingly), but I also had to introduce
a copy somewhere else pending a new snapshot, so maybe that's it.
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/core.rs2
-rw-r--r--src/libcore/io.rs18
-rw-r--r--src/libcore/str.rs21
3 files changed, 40 insertions, 1 deletions
diff --git a/src/libcore/core.rs b/src/libcore/core.rs
index 8681f7d2a1c..84c15385a7f 100644
--- a/src/libcore/core.rs
+++ b/src/libcore/core.rs
@@ -49,7 +49,7 @@ mod std {
 
 #[doc = "
 A standard function to use to indicate unreachable code. Because the
-function is guaranteed to fail typestate will correctly identify
+function is guaranteed to fail, typestate will correctly identify
 any code paths following the appearance of this function as unreachable.
 "]
 fn unreachable() -> ! {
diff --git a/src/libcore/io.rs b/src/libcore/io.rs
index 0c723fc8c63..f61b622f088 100644
--- a/src/libcore/io.rs
+++ b/src/libcore/io.rs
@@ -650,6 +650,24 @@ fn read_whole_file_str(file: str) -> result<str, str> {
     })
 }
 
+/*
+  Returns the result as a unique boxed string rather than a string
+ */
+fn read_whole_file_ref(file: str) -> result<~str, str> {
+     let f = os::as_c_charp(file, {|pathbuf|
+        os::as_c_charp("r", {|modebuf|
+            libc::fopen(pathbuf, modebuf)
+        })
+    });
+    ret if f as uint == 0u { result::err("error opening " + file) }
+        else unsafe {
+        let buf : ~mut [const u8] = ~mut [const];
+        let self = FILE_reader(f, true);
+        while (!self.eof()) { *buf += self.read_bytes(2048u); }
+        result::ok(str::unsafe::from_bytes_move(buf))
+    }
+}
+
 // FIXME implement this in a low-level way. Going through the abstractions is
 // pointless. // #2004
 fn read_whole_file(file: str) -> result<[u8], str> {
diff --git a/src/libcore/str.rs b/src/libcore/str.rs
index 12258f5eb27..1fff066c025 100644
--- a/src/libcore/str.rs
+++ b/src/libcore/str.rs
@@ -124,6 +124,19 @@ fn from_bytes(vv: [u8]) -> str unsafe {
 }
 
 #[doc = "
+Convert a unique vector of bytes (passed by move)
+   to a unique boxed UTF-8 string
+
+# Failure
+
+Fails if invalid UTF-8
+"]
+fn from_bytes_move(-vv: ~mut [const u8]) -> ~str unsafe {
+   assert is_utf8(::unsafe::reinterpret_cast(vv));
+   ret unsafe::from_bytes_move(vv);
+}
+
+#[doc = "
 Convert a byte to a UTF-8 string
 
 # Failure
@@ -1631,6 +1644,7 @@ mod unsafe {
       from_buf,
       from_c_str,
       from_bytes,
+      from_bytes_move,
       slice_bytes,
       push_byte,
       pop_byte,
@@ -1685,6 +1699,13 @@ mod unsafe {
        ret scopy;
    }
 
+   unsafe fn from_bytes_move(-v: ~mut [const u8]) -> ~str unsafe {
+     *v += [0u8];
+     let s: ~str = ::unsafe::reinterpret_cast(v);
+     ::unsafe::forget(v);
+     s
+   }
+
    #[doc = "
    Converts a byte to a string.