]> git.lizzy.rs Git - rust.git/commitdiff
std: inline str::with_capacity and vec::with_capacity
authorErick Tryzelaar <erick.tryzelaar@gmail.com>
Tue, 23 Jul 2013 16:54:28 +0000 (09:54 -0700)
committerErick Tryzelaar <erick.tryzelaar@gmail.com>
Tue, 23 Jul 2013 23:57:00 +0000 (16:57 -0700)
src/libstd/str.rs
src/libstd/vec.rs

index b869f574beb9bfd035ae3dd0583c693ec294ecbf..0d2cfa2e1e8fa660f29bf7a0c451eac7823869c1 100644 (file)
@@ -180,9 +180,7 @@ pub fn concat(&self) -> ~str {
 
         let len = self.iter().transform(|s| s.as_slice().len()).sum();
 
-        let mut s = ~"";
-
-        s.reserve(len);
+        let mut s = with_capacity(len);
 
         unsafe {
             do s.as_mut_buf |buf, _| {
@@ -678,6 +676,7 @@ pub fn from_utf16(v: &[u16]) -> ~str {
  * Allocates a new string with the specified capacity. The string returned is
  * the empty string, but has capacity for much more.
  */
+#[inline]
 pub fn with_capacity(capacity: uint) -> ~str {
     let mut buf = ~"";
     buf.reserve(capacity);
@@ -1830,10 +1829,9 @@ fn find_str(&self, needle: &str) -> Option<uint> {
     /// Given a string, make a new string with repeated copies of it.
     fn repeat(&self, nn: uint) -> ~str {
         do self.as_imm_buf |buf, len| {
-            let mut ret = ~"";
             // ignore the NULL terminator
             let len = len - 1;
-            ret.reserve(nn * len);
+            let mut ret = with_capacity(nn * len);
 
             unsafe {
                 do ret.as_mut_buf |rbuf, _len| {
index 6bbf1210a31064bad3b6364ccc9e1f31943375b7..cc41f0dbb941cfd07885a686a02e19b6a3c1a16d 100644 (file)
@@ -87,6 +87,7 @@ pub fn from_elem<T:Clone>(n_elts: uint, t: T) -> ~[T] {
 }
 
 /// Creates a new vector with a capacity of `capacity`
+#[inline]
 pub fn with_capacity<T>(capacity: uint) -> ~[T] {
     unsafe {
         if contains_managed::<T>() {