@@ -807,13 +807,24 @@ pub fn from_utf16(v: &[u16]) -> ~str {
807
807
808
808
/// Allocates a new string with the specified capacity. The string returned is
809
809
/// the empty string, but has capacity for much more.
810
+ #[ cfg( stage0) ]
810
811
#[ inline]
811
812
pub fn with_capacity ( capacity : uint ) -> ~str {
812
813
let mut buf = ~"";
813
814
buf. reserve ( capacity) ;
814
815
buf
815
816
}
816
817
818
+ /// Allocates a new string with the specified capacity. The string returned is
819
+ /// the empty string, but has capacity for much more.
820
+ #[ cfg( not( stage0) ) ]
821
+ #[ inline]
822
+ pub fn with_capacity ( capacity : uint ) -> ~str {
823
+ unsafe {
824
+ cast:: transmute ( vec:: with_capacity :: < ~[ u8 ] > ( capacity) )
825
+ }
826
+ }
827
+
817
828
/// As char_len but for a slice of a string
818
829
///
819
830
/// # Arguments
@@ -3700,7 +3711,7 @@ mod tests {
3700
3711
#[cfg(test)]
3701
3712
mod bench {
3702
3713
use extra::test::BenchHarness;
3703
- use str ;
3714
+ use super::* ;
3704
3715
3705
3716
#[bench]
3706
3717
fn is_utf8_100_ascii(bh: &mut BenchHarness) {
@@ -3710,7 +3721,7 @@ mod bench {
3710
3721
3711
3722
assert_eq!(100, s.len());
3712
3723
do bh.iter {
3713
- str:: is_utf8(s);
3724
+ is_utf8(s);
3714
3725
}
3715
3726
}
3716
3727
@@ -3719,7 +3730,7 @@ mod bench {
3719
3730
let s = bytes!(" 𐌀𐌖𐌋𐌄𐌑𐌉ปรدولة الكويتทศไทย中华𐍅𐌿𐌻𐍆𐌹𐌻𐌰");
3720
3731
assert_eq!(100, s.len());
3721
3732
do bh.iter {
3722
- str:: is_utf8(s);
3733
+ is_utf8(s);
3723
3734
}
3724
3735
}
3725
3736
@@ -3742,4 +3753,11 @@ mod bench {
3742
3753
s. map_chars( |c| ( ( c as uint) + 1 ) as char ) ;
3743
3754
}
3744
3755
}
3756
+
3757
+ #[ bench]
3758
+ fn bench_with_capacity( bh: & mut BenchHarness ) {
3759
+ do bh. iter {
3760
+ with_capacity( 100 ) ;
3761
+ }
3762
+ }
3745
3763
}
0 commit comments