]> git.lizzy.rs Git - rust.git/commitdiff
std: replace str::append with a method
authorHuon Wilson <dbau.pp+github@gmail.com>
Tue, 11 Jun 2013 02:01:45 +0000 (12:01 +1000)
committerHuon Wilson <dbau.pp+github@gmail.com>
Wed, 12 Jun 2013 02:21:03 +0000 (12:21 +1000)
src/compiletest/runtest.rs
src/libstd/str.rs

index 7159e51e3b6f329d7dcd2e0e79634bee0a0c617e..6b4f1420c565582aeda0d0f819106acac372aba4 100644 (file)
@@ -254,7 +254,7 @@ fn run_debuginfo_test(config: &config, props: &TestProps, testfile: &Path) {
     }
 
     // write debugger script
-    let script_str = str::append(cmds, "\nquit\n");
+    let script_str = cmds.append("\nquit\n");
     debug!("script_str = %s", script_str);
     dump_output_file(config, testfile, script_str, "debugger.script");
 
index 9d8618e557147141dd91c317a8e99de7495bc6ee..e8145b37114ef06965e58bcf661142d3ac8d1dcf 100644 (file)
@@ -154,14 +154,6 @@ pub fn push_str(lhs: &mut ~str, rhs: &str) {
     lhs.push_str(rhs)
 }
 
-/// Concatenate two strings together
-#[inline(always)]
-pub fn append(lhs: ~str, rhs: &str) -> ~str {
-    let mut v = lhs;
-    v.push_str_no_overallocate(rhs);
-    v
-}
-
 #[allow(missing_doc)]
 pub trait StrVector {
     pub fn concat(&self) -> ~str;
@@ -1515,8 +1507,6 @@ fn test_from_buf_len() {
 #[cfg(not(test))]
 pub mod traits {
     use ops::Add;
-    use str::append;
-
     impl<'self> Add<&'self str,~str> for ~str {
         #[inline(always)]
         fn add(&self, rhs: & &'self str) -> ~str {
@@ -2100,6 +2090,7 @@ pub trait OwnedStr {
     fn push_str_no_overallocate(&mut self, rhs: &str);
     fn push_str(&mut self, rhs: &str);
     fn push_char(&mut self, c: char);
+    fn append(&self, rhs: &str) -> ~str; // FIXME #4850: this should consume self.
     fn reserve(&mut self, n: uint);
     fn reserve_at_least(&mut self, n: uint);
 }
@@ -2197,6 +2188,14 @@ fn push_char(&mut self, c: char) {
             raw::set_len(self, new_len);
         }
     }
+    /// Concatenate two strings together.
+    #[inline]
+    fn append(&self, rhs: &str) -> ~str {
+        // FIXME #4850: this should consume self, but that causes segfaults
+        let mut v = self.clone();
+        v.push_str_no_overallocate(rhs);
+        v
+    }
 
     /**
      * Reserves capacity for exactly `n` bytes in the given string, not including
@@ -2396,6 +2395,27 @@ fn test_rfind() {
         assert_eq!("ประเทศไทย中华Việt Nam".rfind(|c: char| c == '华'), Some(30u));
     }
 
+    #[test]
+    fn test_push_str() {
+        let mut s = ~"";
+        s.push_str("");
+        assert_eq!(s.slice_from(0), "");
+        s.push_str("abc");
+        assert_eq!(s.slice_from(0), "abc");
+        s.push_str("ประเทศไทย中华Việt Nam");
+        assert_eq!(s.slice_from(0), "abcประเทศไทย中华Việt Nam");
+    }
+    #[test]
+    fn test_append() {
+        let mut s = ~"";
+        s = s.append("");
+        assert_eq!(s.slice_from(0), "");
+        s = s.append("abc");
+        assert_eq!(s.slice_from(0), "abc");
+        s = s.append("ประเทศไทย中华Việt Nam");
+        assert_eq!(s.slice_from(0), "abcประเทศไทย中华Việt Nam");
+    }
+
     #[test]
     fn test_pop_char() {
         let mut data = ~"ประเทศไทย中华";