From 3c23a0a836164ed3ac1b94b526ff8f4e71571e8e Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Tue, 11 Jun 2013 12:01:45 +1000 Subject: [PATCH] std: replace str::append with a method --- src/compiletest/runtest.rs | 2 +- src/libstd/str.rs | 40 ++++++++++++++++++++++++++++---------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/compiletest/runtest.rs b/src/compiletest/runtest.rs index 7159e51e3b6..6b4f1420c56 100644 --- a/src/compiletest/runtest.rs +++ b/src/compiletest/runtest.rs @@ -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"); diff --git a/src/libstd/str.rs b/src/libstd/str.rs index 9d8618e5571..e8145b37114 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -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 @@ pub mod raw { #[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 @@ impl OwnedStr for ~str { 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 @@ mod tests { 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 = ~"ประเทศไทย中华";