From: Erick Tryzelaar Date: Tue, 23 Jul 2013 16:54:28 +0000 (-0700) Subject: std: inline str::with_capacity and vec::with_capacity X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=bbedbc04507ea3a1e87905c4b37c3460b5de1746;p=rust.git std: inline str::with_capacity and vec::with_capacity --- diff --git a/src/libstd/str.rs b/src/libstd/str.rs index b869f574beb..0d2cfa2e1e8 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -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 { /// 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| { diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 6bbf1210a31..cc41f0dbb94 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -87,6 +87,7 @@ pub fn from_elem(n_elts: uint, t: T) -> ~[T] { } /// Creates a new vector with a capacity of `capacity` +#[inline] pub fn with_capacity(capacity: uint) -> ~[T] { unsafe { if contains_managed::() {