From 0e1a4a4da29caadba5d5cd86a5cd2587fdb0755d Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sat, 25 Aug 2012 16:53:44 -0700 Subject: libcore: rewrite vec::unsafe::from_buf in pure rust --- src/rt/rust_builtin.cpp | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) (limited to 'src/rt/rust_builtin.cpp') diff --git a/src/rt/rust_builtin.cpp b/src/rt/rust_builtin.cpp index cabc086b64f..a74f5985d36 100644 --- a/src/rt/rust_builtin.cpp +++ b/src/rt/rust_builtin.cpp @@ -148,22 +148,6 @@ str_reserve_shared(rust_vec_box** sp, reserve_vec_exact(task, sp, n_elts + 1); } -/** - * Copies elements in an unsafe buffer to the given interior vector. The - * vector must have size zero. - */ -extern "C" CDECL rust_vec_box* -vec_from_buf_shared(type_desc *ty, void *ptr, size_t count) { - rust_task *task = rust_get_current_task(); - size_t fill = ty->size * count; - rust_vec_box* v = (rust_vec_box*) - task->kernel->malloc(fill + sizeof(rust_vec_box), - "vec_from_buf"); - v->body.fill = v->body.alloc = fill; - memmove(&v->body.data[0], ptr, fill); - return v; -} - extern "C" CDECL void rust_str_push(rust_vec_box** sp, uint8_t byte) { rust_task *task = rust_get_current_task(); @@ -515,8 +499,9 @@ void tm_to_rust_tm(tm* in_tm, rust_tm* out_tm, int32_t gmtoff, out_tm->tm_nsec = nsec; if (zone != NULL) { + rust_task *task = rust_get_current_task(); size_t size = strlen(zone); - str_reserve_shared(&out_tm->tm_zone, size); + reserve_vec_exact(task, &out_tm->tm_zone, size + 1); memcpy(out_tm->tm_zone->body.data, zone, size); out_tm->tm_zone->body.fill = size + 1; out_tm->tm_zone->body.data[size] = '\0'; -- cgit 1.4.1-3-g733a5 From 7bb65848a15c2970f1b7a52e870bfc98c9c981a9 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sat, 25 Aug 2012 16:56:15 -0700 Subject: libcore: Rewrite str::unsafe::push_byte in pure rust. --- src/libcore/str.rs | 11 ++++++++--- src/rt/rust_builtin.cpp | 10 ---------- src/rt/rustrt.def.in | 1 - 3 files changed, 8 insertions(+), 14 deletions(-) (limited to 'src/rt/rust_builtin.cpp') diff --git a/src/libcore/str.rs b/src/libcore/str.rs index c7921ade27f..7e5778f0445 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -117,7 +117,6 @@ export #[abi = "cdecl"] extern mod rustrt { - fn rust_str_push(&s: ~str, ch: u8); 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). 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). 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). diff --git a/src/rt/rust_builtin.cpp b/src/rt/rust_builtin.cpp index a74f5985d36..a662141bf62 100644 --- a/src/rt/rust_builtin.cpp +++ b/src/rt/rust_builtin.cpp @@ -148,16 +148,6 @@ str_reserve_shared(rust_vec_box** sp, 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* rand_seed() { size_t size = sizeof(ub4) * RANDSIZ; diff --git a/src/rt/rustrt.def.in b/src/rt/rustrt.def.in index 141937356e6..191ebe6f0be 100644 --- a/src/rt/rustrt.def.in +++ b/src/rt/rustrt.def.in @@ -38,7 +38,6 @@ rust_getcwd rust_get_stdin rust_get_stdout rust_get_stderr -rust_str_push rust_list_files rust_log_console_on rust_log_console_off -- cgit 1.4.1-3-g733a5 From 628b94618695fdf24b5b8cc9f56af6d86efa265d Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Mon, 27 Aug 2012 07:23:55 -0700 Subject: libcore: rewrite str::reserve in pure rust. --- src/libcore/str.rs | 10 +++------- src/rt/rust_builtin.cpp | 7 ------- src/rt/rustrt.def.in | 1 - 3 files changed, 3 insertions(+), 15 deletions(-) (limited to 'src/rt/rust_builtin.cpp') diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 7e5778f0445..888d992321c 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -115,11 +115,6 @@ export StrSlice, UniqueStr; -#[abi = "cdecl"] -extern mod rustrt { - fn str_reserve_shared(&ss: ~str, nn: libc::size_t); -} - /* Section: Creating a string */ @@ -1818,8 +1813,9 @@ pure fn as_buf(s: &str, f: fn(*u8, uint) -> T) -> T { * * n - The number of bytes to reserve space for */ fn reserve(&s: ~str, n: uint) { - if capacity(s) < n { - rustrt::str_reserve_shared(s, n as size_t); + unsafe { + let v: *mut ~[u8] = ::unsafe::reinterpret_cast(ptr::addr_of(s)); + vec::reserve(*v, n + 1); } } diff --git a/src/rt/rust_builtin.cpp b/src/rt/rust_builtin.cpp index a662141bf62..f9da1c91cf0 100644 --- a/src/rt/rust_builtin.cpp +++ b/src/rt/rust_builtin.cpp @@ -141,13 +141,6 @@ vec_reserve_shared(type_desc* ty, rust_vec_box** vp, reserve_vec_exact(task, vp, n_elts * ty->size); } -extern "C" CDECL void -str_reserve_shared(rust_vec_box** sp, - size_t n_elts) { - rust_task *task = rust_get_current_task(); - reserve_vec_exact(task, sp, n_elts + 1); -} - extern "C" CDECL rust_vec* rand_seed() { size_t size = sizeof(ub4) * RANDSIZ; diff --git a/src/rt/rustrt.def.in b/src/rt/rustrt.def.in index 191ebe6f0be..8c550833d1c 100644 --- a/src/rt/rustrt.def.in +++ b/src/rt/rustrt.def.in @@ -61,7 +61,6 @@ shape_log_str start_task vec_reserve_shared_actual vec_reserve_shared -str_reserve_shared task_clear_event_reject task_wait_event task_signal_event -- cgit 1.4.1-3-g733a5