From d67bd6db91d9c29118485b770d463fa6d8a079c3 Mon Sep 17 00:00:00 2001 From: maikklein Date: Sun, 21 Jul 2013 02:37:05 +0200 Subject: [PATCH 1/2] Added Mul for ~str --- src/libstd/str.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/libstd/str.rs b/src/libstd/str.rs index 0811dab407ef5..9c26434fc1734 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -1037,11 +1037,24 @@ pub mod raw { } + #[cfg(not(test))] pub mod traits { - use ops::Add; + use ops::{Add,Mul}; use cmp::{TotalOrd, Ordering, Less, Equal, Greater, Eq, Ord, Equiv, TotalEq}; use super::{Str, eq_slice}; + + impl Mul for ~str { + + fn mul(&self,repeat: &uint) -> ~str{ + let mut repeated_string = ~""; + for repeat.times { + repeated_string = repeated_string.append((*self).clone()); + } + repeated_string + } + + } impl<'self> Add<&'self str,~str> for &'self str { #[inline] @@ -2362,6 +2375,18 @@ mod tests { use vec::{ImmutableVector, CopyableVector}; use cmp::{TotalOrd, Less, Equal, Greater}; + #[test] + fn test_repeat_string() { + let rust = ~"Rust"; + let rust_zero = rust * 0; + let rust_single = rust * 1; + let rust_two = rust * 2; + + assert!((eq(&rust_single, &rust))); + assert!((eq(&rust_zero, &~""))); + assert!((eq(&rust_two, &~"RustRust"))); + + } #[test] fn test_eq() { assert!((eq(&~"", &~""))); From 5bafa455e392dcfeafba2f5013df37d541c91273 Mon Sep 17 00:00:00 2001 From: maikklein Date: Sun, 21 Jul 2013 17:17:00 +0200 Subject: [PATCH 2/2] Adding Mul to &str, @str and ~str --- src/libstd/str.rs | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/libstd/str.rs b/src/libstd/str.rs index 9c26434fc1734..432dd87fa5fce 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -1045,15 +1045,19 @@ pub mod traits { use super::{Str, eq_slice}; impl Mul for ~str { - fn mul(&self,repeat: &uint) -> ~str{ - let mut repeated_string = ~""; - for repeat.times { - repeated_string = repeated_string.append((*self).clone()); - } - repeated_string + self.repeat(*repeat) + } + } + impl Mul for @str { + fn mul(&self,repeat: &uint) -> @str{ + self.repeat(*repeat).to_managed() + } + } + impl<'self> Mul for &'self str { + fn mul(&self,repeat: &uint) -> ~str{ + self.repeat(*repeat) } - } impl<'self> Add<&'self str,~str> for &'self str { @@ -2377,15 +2381,17 @@ mod tests { #[test] fn test_repeat_string() { - let rust = ~"Rust"; - let rust_zero = rust * 0; - let rust_single = rust * 1; - let rust_two = rust * 2; - - assert!((eq(&rust_single, &rust))); - assert!((eq(&rust_zero, &~""))); - assert!((eq(&rust_two, &~"RustRust"))); - + assert_eq!(@"foo" * 1 , @"foo"); + assert_eq!(@"foo" * 0 , @""); + assert_eq!(@"foo" * 2 , @"foofoo"); + + assert_eq!(~"foo" * 1,~"foo"); + assert_eq!(~"foo" * 0, ~""); + assert_eq!(~"foo" * 2, ~"foofoo"); + + assert_eq!("foo" * 1, ~"foo"); + assert_eq!("foo" * 0, ~""); + assert_eq!("foo" * 2, ~"foofoo"); } #[test] fn test_eq() {