@@ -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
@@ -3701,7 +3712,7 @@ mod tests {
3701
3712
#[cfg(test)]
3702
3713
mod bench {
3703
3714
use extra::test::BenchHarness;
3704
- use str ;
3715
+ use super::* ;
3705
3716
3706
3717
#[bench]
3707
3718
fn is_utf8_100_ascii(bh: &mut BenchHarness) {
@@ -3711,7 +3722,7 @@ mod bench {
3711
3722
3712
3723
assert_eq!(100, s.len());
3713
3724
do bh.iter {
3714
- str:: is_utf8(s);
3725
+ is_utf8(s);
3715
3726
}
3716
3727
}
3717
3728
@@ -3720,7 +3731,7 @@ mod bench {
3720
3731
let s = bytes!(" 𐌀𐌖𐌋𐌄𐌑𐌉ปรدولة الكويتทศไทย中华𐍅𐌿𐌻𐍆𐌹𐌻𐌰");
3721
3732
assert_eq!(100, s.len());
3722
3733
do bh.iter {
3723
- str:: is_utf8(s);
3734
+ is_utf8(s);
3724
3735
}
3725
3736
}
3726
3737
@@ -3743,4 +3754,11 @@ mod bench {
3743
3754
s. map_chars( |c| ( ( c as uint) + 1 ) as char ) ;
3744
3755
}
3745
3756
}
3757
+
3758
+ #[ bench]
3759
+ fn bench_with_capacity( bh: & mut BenchHarness ) {
3760
+ do bh. iter {
3761
+ with_capacity( 100 ) ;
3762
+ }
3763
+ }
3746
3764
}
0 commit comments