about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-07-21 15:28:38 -0700
committerbors <bors@rust-lang.org>2013-07-21 15:28:38 -0700
commitfe3f75ff8e2a7e750713295f5fa17a4abf9d9d62 (patch)
tree57fd8decc50d57cf0a7f0663df3962dadb5c00c8 /src/libstd
parente336cbf8f9e26758a2ef98ec6b7715e083fdcced (diff)
parent3509f9d5ae927781f190dda5e623d30ce34e87e0 (diff)
downloadrust-fe3f75ff8e2a7e750713295f5fa17a4abf9d9d62.tar.gz
rust-fe3f75ff8e2a7e750713295f5fa17a4abf9d9d62.zip
auto merge of #7932 : blake2-ppc/rust/str-clear, r=huonw
~str and @str need separate implementations for use in generic
functions, where it will not automatically use the impl on &str.

fixes issue #7900
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/str.rs52
1 files changed, 51 insertions, 1 deletions
diff --git a/src/libstd/str.rs b/src/libstd/str.rs
index c74c1e18e6d..97fc56d007d 100644
--- a/src/libstd/str.rs
+++ b/src/libstd/str.rs
@@ -22,7 +22,7 @@ use cast;
 use char;
 use char::Char;
 use clone::Clone;
-use container::Container;
+use container::{Container, Mutable};
 use iter::Times;
 use iterator::{Iterator, IteratorUtil, FilterIterator, AdditiveIterator, MapIterator};
 use libc;
@@ -1214,6 +1214,31 @@ impl<'self> Container for &'self str {
     }
 }
 
+impl Container for ~str {
+    #[inline]
+    fn len(&self) -> uint { self.as_slice().len() }
+    #[inline]
+    fn is_empty(&self) -> bool { self.len() == 0 }
+}
+
+impl Container for @str {
+    #[inline]
+    fn len(&self) -> uint { self.as_slice().len() }
+    #[inline]
+    fn is_empty(&self) -> bool { self.len() == 0 }
+}
+
+impl Mutable for ~str {
+    /// Remove all content, make the string empty
+    #[inline]
+    fn clear(&mut self) {
+        unsafe {
+            raw::set_len(self, 0)
+        }
+    }
+}
+
+
 #[allow(missing_doc)]
 pub trait StrSlice<'self> {
     fn contains<'a>(&self, needle: &'a str) -> bool;
@@ -2503,6 +2528,18 @@ mod tests {
     }
 
     #[test]
+    fn test_clear() {
+        let mut empty = ~"";
+        empty.clear();
+        assert_eq!("", empty.as_slice());
+        let mut data = ~"ประเทศไทย中";
+        data.clear();
+        assert_eq!("", data.as_slice());
+        data.push_char('华');
+        assert_eq!("华", data.as_slice());
+    }
+
+    #[test]
     fn test_split_within() {
         fn t(s: &str, i: uint, u: &[~str]) {
             let mut v = ~[];
@@ -3494,4 +3531,17 @@ mod tests {
         t::<@str>();
         t::<~str>();
     }
+
+    #[test]
+    fn test_str_container() {
+        fn sum_len<S: Container>(v: &[S]) -> uint {
+            v.iter().transform(|x| x.len()).sum()
+        }
+
+        let s = ~"01234";
+        assert_eq!(5, sum_len(["012", "", "34"]));
+        assert_eq!(5, sum_len([@"01", @"2", @"34", @""]));
+        assert_eq!(5, sum_len([~"01", ~"2", ~"34", ~""]));
+        assert_eq!(5, sum_len([s.as_slice()]));
+    }
 }