Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/libstd/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -305,6 +306,14 @@ impl IntoStr for ~[Ascii] {
}
}

impl IntoStr for Vec<Ascii> {
#[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
Expand Down Expand Up @@ -473,13 +482,18 @@ mod tests {
use super::*;
use str::from_char;
use char::from_u32;
use vec_ng::Vec;

macro_rules! v2ascii (
( [$($e:expr),*]) => (&[$(Ascii{chr:$e}),*]);
(&[$($e:expr),*]) => (&[$(Ascii{chr:$e}),*]);
(~[$($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);
Expand Down Expand Up @@ -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]));
Expand All @@ -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]
Expand Down