Skip to content

Commit

Permalink
core: Optimize str::unsafe::slice_bytes. Closes rust-lang#1995
Browse files Browse the repository at this point in the history
  • Loading branch information
brson committed Mar 16, 2012
1 parent 29fa4a6 commit bcf44f8
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1331,7 +1331,16 @@ mod unsafe {
assert (begin <= end);
assert (end <= len(s));

let mut v = as_bytes(s) { |v| vec::slice(v, begin, end) };
let mut v = as_buf(s) { |sbuf|
let mut v = [];
vec::reserve(v, end - begin + 1u);
vec::as_buf(v) { |vbuf|
let src = ptr::offset(sbuf, begin);
ptr::memcpy(vbuf, src, end - begin);
}
vec::unsafe::set_len(v, end - begin);
v
};
v += [0u8];
let s: str = ::unsafe::reinterpret_cast(v);
::unsafe::leak(v);
Expand Down

0 comments on commit bcf44f8

Please sign in to comment.