@@ -22,7 +22,7 @@ use cast;
22
22
use char;
23
23
use char:: Char ;
24
24
use clone:: Clone ;
25
- use container:: Container ;
25
+ use container:: { Container , Mutable } ;
26
26
use iter:: Times ;
27
27
use iterator:: { Iterator , IteratorUtil , FilterIterator , AdditiveIterator , MapIterator } ;
28
28
use libc;
@@ -1211,6 +1211,31 @@ impl<'self> Container for &'self str {
1211
1211
}
1212
1212
}
1213
1213
1214
+ impl Container for ~str {
1215
+ #[inline]
1216
+ fn len(&self) -> uint { self.as_slice().len() }
1217
+ #[inline]
1218
+ fn is_empty(&self) -> bool { self.len() == 0 }
1219
+ }
1220
+
1221
+ impl Container for @str {
1222
+ #[inline]
1223
+ fn len(&self) -> uint { self.as_slice().len() }
1224
+ #[inline]
1225
+ fn is_empty(&self) -> bool { self.len() == 0 }
1226
+ }
1227
+
1228
+ impl Mutable for ~str {
1229
+ /// Remove all content, make the string empty
1230
+ #[inline]
1231
+ fn clear(&mut self) {
1232
+ unsafe {
1233
+ raw::set_len(self, 0)
1234
+ }
1235
+ }
1236
+ }
1237
+
1238
+
1214
1239
#[allow(missing_doc)]
1215
1240
pub trait StrSlice<'self> {
1216
1241
fn contains<'a>(&self, needle: &'a str) -> bool;
@@ -2495,6 +2520,18 @@ mod tests {
2495
2520
assert_eq!(~" 华ประเทศไทย中", data);
2496
2521
}
2497
2522
2523
+ #[test]
2524
+ fn test_clear() {
2525
+ let mut empty = ~" ";
2526
+ empty.clear();
2527
+ assert_eq!(" ", empty.as_slice());
2528
+ let mut data = ~" ประเทศไทย中";
2529
+ data.clear();
2530
+ assert_eq!(" ", data.as_slice());
2531
+ data.push_char('华');
2532
+ assert_eq!(" 华", data.as_slice());
2533
+ }
2534
+
2498
2535
#[test]
2499
2536
fn test_split_within() {
2500
2537
fn t(s: &str, i: uint, u: &[~str]) {
@@ -3487,4 +3524,17 @@ mod tests {
3487
3524
t:: <@str >( ) ;
3488
3525
t:: <~str >( ) ;
3489
3526
}
3527
+
3528
+ #[ test]
3529
+ fn test_str_container( ) {
3530
+ fn sum_len<S : Container >( v: & [ S ] ) -> uint {
3531
+ v. iter( ) . transform( |x| x. len( ) ) . sum( )
3532
+ }
3533
+
3534
+ let s = ~"01234 ";
3535
+ assert_eq!(5, sum_len([" 012 ", " ", " 34 "]));
3536
+ assert_eq!(5, sum_len([@" 01 ", @" 2 ", @" 34 ", @" "]));
3537
+ assert_eq!(5, sum_len([~" 01 ", ~" 2 ", ~" 34 ", ~" "] ) ) ;
3538
+ assert_eq!( 5 , sum_len( [ s. as_slice( ) ] ) ) ;
3539
+ }
3490
3540
}
0 commit comments