Skip to content

Commit 8415515

Browse files
committed
libcore: Make as_bytes_slice() not include the null byte
1 parent 127144b commit 8415515

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/libcore/str.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1752,8 +1752,17 @@ pure fn as_bytes<T>(s: ~str, f: fn(~[u8]) -> T) -> T {
17521752
}
17531753
}
17541754

1755+
/**
1756+
* Work with the byte buffer of a string as a byte slice.
1757+
*
1758+
* The byte slice does not include the null terminator.
1759+
*/
17551760
pure fn as_bytes_slice(s: &a/str) -> &a/[u8] {
1756-
unsafe { ::unsafe::reinterpret_cast(&s) }
1761+
unsafe {
1762+
let (ptr, len): (*u8, uint) = ::unsafe::reinterpret_cast(&s);
1763+
let outgoing_tuple: (*u8, uint) = (ptr, len - 1);
1764+
return ::unsafe::reinterpret_cast(&outgoing_tuple);
1765+
}
17571766
}
17581767

17591768
/**

0 commit comments

Comments
 (0)