1
Fork 0

Adding str::reserve

This commit is contained in:
Kevin Cantu 2012-02-04 14:56:42 -08:00 committed by Brian Anderson
parent d3dfe8758d
commit 6be25c8a0c
3 changed files with 18 additions and 0 deletions

View file

@ -97,6 +97,7 @@ export
as_buf, as_buf,
//buf, //buf,
sbuf, sbuf,
reserve,
unsafe; unsafe;
@ -105,6 +106,7 @@ export
#[abi = "cdecl"] #[abi = "cdecl"]
native mod rustrt { native mod rustrt {
fn rust_str_push(&s: str, ch: u8); fn rust_str_push(&s: str, ch: u8);
fn str_reserve_shared(&ss: str, nn: ctypes::size_t);
} }
// FIXME: add pure to a lot of functions // FIXME: add pure to a lot of functions
@ -755,6 +757,7 @@ Apply a function to each character
*/ */
fn map(ss: str, ff: fn(char) -> char) -> str { fn map(ss: str, ff: fn(char) -> char) -> str {
let result = ""; let result = "";
reserve(result, byte_len(ss));
chars_iter(ss, {|cc| chars_iter(ss, {|cc|
str::push_char(result, ff(cc)); str::push_char(result, ff(cc));
@ -1302,6 +1305,13 @@ An unsafe buffer of bytes. Corresponds to a C char pointer.
*/ */
type sbuf = *u8; type sbuf = *u8;
// Function: reserve
//
// Allocate more memory for a string, up to `nn` + 1 bytes
fn reserve(&ss: str, nn: uint) {
rustrt::str_reserve_shared(ss, nn);
}
// Module: unsafe // Module: unsafe
// //
// These functions may create invalid UTF-8 strings and eat your baby. // These functions may create invalid UTF-8 strings and eat your baby.

View file

@ -105,6 +105,13 @@ vec_reserve_shared(type_desc* ty, rust_vec** vp,
reserve_vec_exact(task, vp, n_elts * ty->size); reserve_vec_exact(task, vp, n_elts * ty->size);
} }
extern "C" CDECL void
str_reserve_shared(rust_vec** sp,
size_t n_elts) {
rust_task *task = rust_task_thread::get_task();
reserve_vec_exact(task, sp, n_elts + 1);
}
/** /**
* Copies elements in an unsafe buffer to the given interior vector. The * Copies elements in an unsafe buffer to the given interior vector. The
* vector must have size zero. * vector must have size zero.

View file

@ -50,6 +50,7 @@ shape_log_str
squareroot squareroot
start_task start_task
vec_reserve_shared vec_reserve_shared
str_reserve_shared
vec_from_buf_shared vec_from_buf_shared
unsupervise unsupervise
upcall_cmp_type upcall_cmp_type