libcore: Rewrite str::unsafe::push_byte in pure rust.

This commit is contained in:
Erick Tryzelaar 2012-08-25 16:56:15 -07:00
parent 94c3975a9a
commit 7bb65848a1
3 changed files with 8 additions and 14 deletions

View File

@ -117,7 +117,6 @@ export
#[abi = "cdecl"] #[abi = "cdecl"]
extern mod rustrt { extern mod rustrt {
fn rust_str_push(&s: ~str, ch: u8);
fn str_reserve_shared(&ss: ~str, nn: libc::size_t); fn str_reserve_shared(&ss: ~str, nn: libc::size_t);
} }
@ -1998,12 +1997,18 @@ mod unsafe {
/// Appends a byte to a string. (Not UTF-8 safe). /// Appends a byte to a string. (Not UTF-8 safe).
unsafe fn push_byte(&s: ~str, b: u8) { unsafe fn push_byte(&s: ~str, b: u8) {
rustrt::rust_str_push(s, b); reserve_at_least(s, s.len() + 1);
do as_buf(s) |buf, len| {
let buf: *mut u8 = ::unsafe::reinterpret_cast(buf);
*ptr::mut_offset(buf, len) = b;
}
set_len(s, s.len() + 1);
} }
/// Appends a vector of bytes to a string. (Not UTF-8 safe). /// Appends a vector of bytes to a string. (Not UTF-8 safe).
unsafe fn push_bytes(&s: ~str, bytes: ~[u8]) { unsafe fn push_bytes(&s: ~str, bytes: ~[u8]) {
for vec::each(bytes) |byte| { rustrt::rust_str_push(s, byte); } reserve_at_least(s, s.len() + bytes.len());
for vec::each(bytes) |byte| { push_byte(s, byte); }
} }
/// Removes the last byte from a string and returns it. (Not UTF-8 safe). /// Removes the last byte from a string and returns it. (Not UTF-8 safe).

View File

@ -148,16 +148,6 @@ str_reserve_shared(rust_vec_box** sp,
reserve_vec_exact(task, sp, n_elts + 1); reserve_vec_exact(task, sp, n_elts + 1);
} }
extern "C" CDECL void
rust_str_push(rust_vec_box** sp, uint8_t byte) {
rust_task *task = rust_get_current_task();
size_t fill = (*sp)->body.fill;
reserve_vec(task, sp, fill + 1);
(*sp)->body.data[fill-1] = byte;
(*sp)->body.data[fill] = 0;
(*sp)->body.fill = fill + 1;
}
extern "C" CDECL rust_vec* extern "C" CDECL rust_vec*
rand_seed() { rand_seed() {
size_t size = sizeof(ub4) * RANDSIZ; size_t size = sizeof(ub4) * RANDSIZ;

View File

@ -38,7 +38,6 @@ rust_getcwd
rust_get_stdin rust_get_stdin
rust_get_stdout rust_get_stdout
rust_get_stderr rust_get_stderr
rust_str_push
rust_list_files rust_list_files
rust_log_console_on rust_log_console_on
rust_log_console_off rust_log_console_off