about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorKevin Cantu <me@kevincantu.org>2012-02-04 14:56:42 -0800
committerBrian Anderson <banderson@mozilla.com>2012-02-05 14:56:16 -0800
commit6be25c8a0cfe28f032ce2b65f8cb07dd1a1ae148 (patch)
tree28f3f534b113b48a6d2e270db4182cbe426edba1 /src
parentd3dfe8758d674d41c3aae116385bcc2b992413ed (diff)
downloadrust-6be25c8a0cfe28f032ce2b65f8cb07dd1a1ae148.tar.gz
rust-6be25c8a0cfe28f032ce2b65f8cb07dd1a1ae148.zip
Adding str::reserve
Diffstat (limited to 'src')
-rw-r--r--src/libcore/str.rs10
-rw-r--r--src/rt/rust_builtin.cpp7
-rw-r--r--src/rt/rustrt.def.in1
3 files changed, 18 insertions, 0 deletions
diff --git a/src/libcore/str.rs b/src/libcore/str.rs
index ecb9827b85e..618fa04a314 100644
--- a/src/libcore/str.rs
+++ b/src/libcore/str.rs
@@ -97,6 +97,7 @@ export
    as_buf,
    //buf,
    sbuf,
+   reserve,
 
    unsafe;
 
@@ -105,6 +106,7 @@ export
 #[abi = "cdecl"]
 native mod rustrt {
     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
@@ -755,6 +757,7 @@ Apply a function to each character
 */
 fn map(ss: str, ff: fn(char) -> char) -> str {
     let result = "";
+    reserve(result, byte_len(ss));
 
     chars_iter(ss, {|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;
 
+// 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
 //
 // These functions may create invalid UTF-8 strings and eat your baby.
diff --git a/src/rt/rust_builtin.cpp b/src/rt/rust_builtin.cpp
index 69bc88157f0..783ca43d862 100644
--- a/src/rt/rust_builtin.cpp
+++ b/src/rt/rust_builtin.cpp
@@ -105,6 +105,13 @@ vec_reserve_shared(type_desc* ty, rust_vec** vp,
     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
  * vector must have size zero.
diff --git a/src/rt/rustrt.def.in b/src/rt/rustrt.def.in
index da6026ebb23..48736ef029b 100644
--- a/src/rt/rustrt.def.in
+++ b/src/rt/rustrt.def.in
@@ -50,6 +50,7 @@ shape_log_str
 squareroot
 start_task
 vec_reserve_shared
+str_reserve_shared
 vec_from_buf_shared
 unsupervise
 upcall_cmp_type