@@ -104,7 +104,7 @@ use iter::{Iterator, FromIterator, Extendable, range};
104104use iter:: { Filter , AdditiveIterator , Map } ;
105105use iter:: { Rev , DoubleEndedIterator , ExactSize } ;
106106use libc;
107- use num:: { Saturating , checked_next_power_of_two } ;
107+ use num:: Saturating ;
108108use option:: { None , Option , Some } ;
109109use ptr;
110110use ptr:: RawPtr ;
@@ -186,12 +186,7 @@ pub fn from_char(ch: char) -> ~str {
186186
187187/// Convert a vector of chars to a string
188188pub fn from_chars ( chs : & [ char ] ) -> ~str {
189- let mut buf = ~"";
190- buf. reserve ( chs. len ( ) ) ;
191- for ch in chs. iter ( ) {
192- buf. push_char ( * ch)
193- }
194- buf
189+ chs. iter ( ) . map ( |c| * c) . collect ( )
195190}
196191
197192#[ doc( hidden) ]
@@ -875,8 +870,7 @@ pub fn utf16_chars(v: &[u16], f: |char|) {
875870
876871/// Allocates a new string from the utf-16 slice provided
877872pub fn from_utf16 ( v : & [ u16 ] ) -> ~str {
878- let mut buf = ~"";
879- buf. reserve ( v. len ( ) ) ;
873+ let mut buf = with_capacity ( v. len ( ) ) ;
880874 utf16_chars ( v, |ch| buf. push_char ( ch) ) ;
881875 buf
882876}
@@ -2166,17 +2160,15 @@ impl<'a> StrSlice<'a> for &'a str {
21662160 }
21672161
21682162 fn escape_default( & self ) -> ~str {
2169- let mut out: ~str = ~"";
2170- out. reserve_at_least( self . len( ) ) ;
2163+ let mut out = with_capacity( self . len( ) ) ;
21712164 for c in self . chars( ) {
21722165 c. escape_default( |c| out. push_char( c) ) ;
21732166 }
21742167 out
21752168 }
21762169
21772170 fn escape_unicode( & self ) -> ~str {
2178- let mut out: ~str = ~"";
2179- out. reserve_at_least( self . len( ) ) ;
2171+ let mut out = with_capacity( self . len( ) ) ;
21802172 for c in self . chars( ) {
21812173 c. escape_unicode( |c| out. push_char( c) ) ;
21822174 }
@@ -2508,7 +2500,7 @@ pub trait OwnedStr {
25082500 ///
25092501 /// * s - A string
25102502 /// * n - The number of bytes to reserve space for
2511- fn reserve ( & mut self , n: uint) ;
2503+ fn reserve_exact ( & mut self , n: uint) ;
25122504
25132505 /// Reserves capacity for at least `n` bytes in the given string.
25142506 ///
@@ -2526,7 +2518,7 @@ pub trait OwnedStr {
25262518 ///
25272519 /// * s - A string
25282520 /// * n - The number of bytes to reserve space for
2529- fn reserve_at_least ( & mut self , n: uint) ;
2521+ fn reserve ( & mut self , n: uint) ;
25302522
25312523 /// Returns the number of single-byte characters the string can hold without
25322524 /// reallocating
@@ -2552,7 +2544,7 @@ impl OwnedStr for ~str {
25522544 #[ inline]
25532545 fn push_str_no_overallocate( & mut self , rhs: & str ) {
25542546 let new_cap = self . len( ) + rhs. len( ) ;
2555- self . reserve ( new_cap) ;
2547+ self . reserve_exact ( new_cap) ;
25562548 self . push_str( rhs) ;
25572549 }
25582550
@@ -2631,15 +2623,17 @@ impl OwnedStr for ~str {
26312623 }
26322624
26332625 #[ inline]
2634- fn reserve ( & mut self , n: uint) {
2626+ fn reserve_exact ( & mut self , n: uint) {
26352627 unsafe {
2636- raw:: as_owned_vec( self ) . reserve ( n)
2628+ raw:: as_owned_vec( self ) . reserve_exact ( n)
26372629 }
26382630 }
26392631
26402632 #[ inline]
2641- fn reserve_at_least( & mut self , n: uint) {
2642- self . reserve( checked_next_power_of_two( n) . unwrap_or( n) )
2633+ fn reserve( & mut self , n: uint) {
2634+ unsafe {
2635+ raw:: as_owned_vec( self ) . reserve( n)
2636+ }
26432637 }
26442638
26452639 #[ inline]
@@ -2711,7 +2705,7 @@ impl Extendable<char> for ~str {
27112705 fn extend<T : Iterator <char >>( & mut self , iterator: & mut T ) {
27122706 let ( lower, _) = iterator. size_hint( ) ;
27132707 let reserve = lower + self . len( ) ;
2714- self . reserve_at_least ( reserve) ;
2708+ self . reserve ( reserve) ;
27152709 for ch in * iterator {
27162710 self . push_char( ch)
27172711 }
0 commit comments