@@ -91,7 +91,7 @@ use iter::{Iterator, FromIterator, Extendable, range};
9191use iter:: { Filter , AdditiveIterator , Map } ;
9292use iter:: { Rev , DoubleEndedIterator , ExactSize } ;
9393use libc;
94- use num:: { Saturating , checked_next_power_of_two } ;
94+ use num:: Saturating ;
9595use option:: { None , Option , Some } ;
9696use ptr;
9797use ptr:: RawPtr ;
@@ -163,12 +163,7 @@ pub fn from_char(ch: char) -> ~str {
163163
164164/// Convert a vector of chars to a string
165165pub fn from_chars ( chs : & [ char ] ) -> ~str {
166- let mut buf = ~"";
167- buf. reserve ( chs. len ( ) ) ;
168- for ch in chs. iter ( ) {
169- buf. push_char ( * ch)
170- }
171- buf
166+ chs. iter ( ) . map ( |c| * c) . collect ( )
172167}
173168
174169#[ doc( hidden) ]
@@ -852,8 +847,7 @@ pub fn utf16_chars(v: &[u16], f: |char|) {
852847
853848/// Allocates a new string from the utf-16 slice provided
854849pub fn from_utf16 ( v : & [ u16 ] ) -> ~str {
855- let mut buf = ~"";
856- buf. reserve ( v. len ( ) ) ;
850+ let mut buf = with_capacity ( v. len ( ) ) ;
857851 utf16_chars ( v, |ch| buf. push_char ( ch) ) ;
858852 buf
859853}
@@ -2096,17 +2090,15 @@ impl<'a> StrSlice<'a> for &'a str {
20962090 }
20972091
20982092 fn escape_default( & self ) -> ~str {
2099- let mut out: ~str = ~"";
2100- out. reserve_at_least( self . len( ) ) ;
2093+ let mut out = with_capacity( self . len( ) ) ;
21012094 for c in self . chars( ) {
21022095 c. escape_default( |c| out. push_char( c) ) ;
21032096 }
21042097 out
21052098 }
21062099
21072100 fn escape_unicode( & self ) -> ~str {
2108- let mut out: ~str = ~"";
2109- out. reserve_at_least( self . len( ) ) ;
2101+ let mut out = with_capacity( self . len( ) ) ;
21102102 for c in self . chars( ) {
21112103 c. escape_unicode( |c| out. push_char( c) ) ;
21122104 }
@@ -2430,7 +2422,7 @@ pub trait OwnedStr {
24302422 ///
24312423 /// * s - A string
24322424 /// * n - The number of bytes to reserve space for
2433- fn reserve ( & mut self , n: uint) ;
2425+ fn reserve_exact ( & mut self , n: uint) ;
24342426
24352427 /// Reserves capacity for at least `n` bytes in the given string.
24362428 ///
@@ -2448,7 +2440,7 @@ pub trait OwnedStr {
24482440 ///
24492441 /// * s - A string
24502442 /// * n - The number of bytes to reserve space for
2451- fn reserve_at_least ( & mut self , n: uint) ;
2443+ fn reserve ( & mut self , n: uint) ;
24522444
24532445 /// Returns the number of single-byte characters the string can hold without
24542446 /// reallocating
@@ -2474,7 +2466,7 @@ impl OwnedStr for ~str {
24742466 #[ inline]
24752467 fn push_str_no_overallocate( & mut self , rhs: & str ) {
24762468 let new_cap = self . len( ) + rhs. len( ) ;
2477- self . reserve ( new_cap) ;
2469+ self . reserve_exact ( new_cap) ;
24782470 self . push_str( rhs) ;
24792471 }
24802472
@@ -2553,15 +2545,17 @@ impl OwnedStr for ~str {
25532545 }
25542546
25552547 #[ inline]
2556- fn reserve ( & mut self , n: uint) {
2548+ fn reserve_exact ( & mut self , n: uint) {
25572549 unsafe {
2558- raw:: as_owned_vec( self ) . reserve ( n)
2550+ raw:: as_owned_vec( self ) . reserve_exact ( n)
25592551 }
25602552 }
25612553
25622554 #[ inline]
2563- fn reserve_at_least( & mut self , n: uint) {
2564- self . reserve( checked_next_power_of_two( n) . unwrap_or( n) )
2555+ fn reserve( & mut self , n: uint) {
2556+ unsafe {
2557+ raw:: as_owned_vec( self ) . reserve( n)
2558+ }
25652559 }
25662560
25672561 #[ inline]
@@ -2619,7 +2613,7 @@ impl Extendable<char> for ~str {
26192613 fn extend<T : Iterator <char >>( & mut self , iterator: & mut T ) {
26202614 let ( lower, _) = iterator. size_hint( ) ;
26212615 let reserve = lower + self . len( ) ;
2622- self . reserve_at_least ( reserve) ;
2616+ self . reserve ( reserve) ;
26232617 for ch in * iterator {
26242618 self . push_char( ch)
26252619 }
0 commit comments