about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPatrick Walton <pcwalton@mimiga.net>2012-08-22 17:03:57 -0700
committerPatrick Walton <pcwalton@mimiga.net>2012-08-23 18:10:16 -0700
commit83e7c869bdfadf0ed8aca92e76fc5073b63402e2 (patch)
tree32d415c026d429c0a8186ec8533238fff0354292
parentdcbeebc801e071394874843e0e4c7509a0fb46c1 (diff)
downloadrust-83e7c869bdfadf0ed8aca92e76fc5073b63402e2.tar.gz
rust-83e7c869bdfadf0ed8aca92e76fc5073b63402e2.zip
libcore: Add from_buf_len_nocopy
-rw-r--r--src/libcore/str.rs8
1 files changed, 8 insertions, 0 deletions
diff --git a/src/libcore/str.rs b/src/libcore/str.rs
index 5f1c100a155..ec61ace2f13 100644
--- a/src/libcore/str.rs
+++ b/src/libcore/str.rs
@@ -1768,6 +1768,7 @@ mod unsafe {
    export
       from_buf,
       from_buf_len,
+      from_buf_len_nocopy,
       from_c_str,
       from_c_str_len,
       from_bytes,
@@ -1799,6 +1800,13 @@ mod unsafe {
         return ::unsafe::transmute(v);
     }
 
+    /// Create a Rust string from a *u8 buffer of the given length without copying
+    unsafe fn from_buf_len_nocopy(buf: &a / *u8, len: uint) -> &a / str {
+        let v = (*buf, len + 1);
+        assert is_utf8(::unsafe::reinterpret_cast(v));
+        return ::unsafe::transmute(v);
+    }
+
     /// Create a Rust string from a null-terminated C string
     unsafe fn from_c_str(c_str: *libc::c_char) -> ~str {
         from_buf(::unsafe::reinterpret_cast(c_str))