core: Optimize str::bytes

This compiles down to a memmove. Takes about 1/4 of the time of the old
version.
This commit is contained in:
Brian Anderson 2012-03-15 15:41:59 -07:00
parent 771177a814
commit 041c9a0863

View file

@ -311,7 +311,11 @@ Converts a string to a vector of bytes
The result vector is not null-terminated.
"]
fn bytes(s: str) -> [u8] unsafe {
as_bytes(s) { |v| vec::slice(v, 0u, vec::len(v) - 1u) }
let mut s_copy = s;
let mut v: [u8] = ::unsafe::reinterpret_cast(s_copy);
::unsafe::leak(s_copy);
vec::unsafe::set_len(v, len(s));
ret v;
}
#[doc = "Convert a string to a vector of characters"]