about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPatrick Walton <pcwalton@mimiga.net>2012-09-04 15:23:30 -0700
committerPatrick Walton <pcwalton@mimiga.net>2012-09-04 15:23:30 -0700
commit8415515cbee679ddd2a17bbb152da10f1d947359 (patch)
tree050e73bcc3261c643616b3f090500331f4fa8116
parent127144bf38f5bf81112bb304efb95a80d54bcaac (diff)
downloadrust-8415515cbee679ddd2a17bbb152da10f1d947359.tar.gz
rust-8415515cbee679ddd2a17bbb152da10f1d947359.zip
libcore: Make as_bytes_slice() not include the null byte
-rw-r--r--src/libcore/str.rs11
1 files changed, 10 insertions, 1 deletions
diff --git a/src/libcore/str.rs b/src/libcore/str.rs
index c4506941488..869981bdf67 100644
--- a/src/libcore/str.rs
+++ b/src/libcore/str.rs
@@ -1752,8 +1752,17 @@ pure fn as_bytes<T>(s: ~str, f: fn(~[u8]) -> T) -> T {
     }
 }
 
+/**
+ * Work with the byte buffer of a string as a byte slice.
+ *
+ * The byte slice does not include the null terminator.
+ */
 pure fn as_bytes_slice(s: &a/str) -> &a/[u8] {
-    unsafe { ::unsafe::reinterpret_cast(&s) }
+    unsafe {
+        let (ptr, len): (*u8, uint) = ::unsafe::reinterpret_cast(&s);
+        let outgoing_tuple: (*u8, uint) = (ptr, len - 1);
+        return ::unsafe::reinterpret_cast(&outgoing_tuple);
+    }
 }
 
 /**