From 5e64e3f57e7af24e9c2076c3c1079974ebd47603 Mon Sep 17 00:00:00 2001 From: Eunchong Yu Date: Mon, 17 Mar 2014 15:30:27 +0900 Subject: [PATCH] Add impl IntoStr for ::std::vec_ng::Vec --- src/libstd/ascii.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/libstd/ascii.rs b/src/libstd/ascii.rs index a21393e268954..3ca08797dd1fe 100644 --- a/src/libstd/ascii.rs +++ b/src/libstd/ascii.rs @@ -20,6 +20,7 @@ use cast; use fmt; use iter::Iterator; use vec::{ImmutableVector, MutableVector, Vector}; +use vec_ng::Vec; use option::{Option, Some, None}; /// Datatype to hold one ascii character. It wraps a `u8`, with the highest bit always zero. @@ -305,6 +306,14 @@ impl IntoStr for ~[Ascii] { } } +impl IntoStr for Vec { + #[inline] + fn into_str(self) -> ~str { + let v: ~[Ascii] = self.move_iter().collect(); + unsafe { cast::transmute(v) } + } +} + /// Trait to convert to an owned byte array by consuming self pub trait IntoBytes { /// Converts to an owned byte array by consuming self @@ -473,6 +482,7 @@ mod tests { use super::*; use str::from_char; use char::from_u32; + use vec_ng::Vec; macro_rules! v2ascii ( ( [$($e:expr),*]) => (&[$(Ascii{chr:$e}),*]); @@ -480,6 +490,10 @@ mod tests { (~[$($e:expr),*]) => (~[$(Ascii{chr:$e}),*]); ) + macro_rules! vec2ascii ( + ($($e:expr),*) => (Vec::from_slice([$(Ascii{chr:$e}),*])); + ) + #[test] fn test_ascii() { assert_eq!(65u8.to_ascii().to_byte(), 65u8); @@ -535,6 +549,17 @@ mod tests { } + #[test] + fn test_ascii_vec_ng() { + assert_eq!(Vec::from_slice("abCDef&?#".to_ascii().to_lower()).into_str(), ~"abcdef&?#"); + assert_eq!(Vec::from_slice("abCDef&?#".to_ascii().to_upper()).into_str(), ~"ABCDEF&?#"); + + assert_eq!(Vec::from_slice("".to_ascii().to_lower()).into_str(), ~""); + assert_eq!(Vec::from_slice("YMCA".to_ascii().to_lower()).into_str(), ~"ymca"); + assert_eq!(Vec::from_slice("abcDEFxyz:.;".to_ascii().to_upper()).into_str(), + ~"ABCDEFXYZ:.;"); + } + #[test] fn test_owned_ascii_vec() { assert_eq!((~"( ;").into_ascii(), v2ascii!(~[40, 32, 59])); @@ -550,6 +575,7 @@ mod tests { #[test] fn test_ascii_into_str() { assert_eq!(v2ascii!(~[40, 32, 59]).into_str(), ~"( ;"); + assert_eq!(vec2ascii!(40, 32, 59).into_str(), ~"( ;"); } #[test]