Moved ascii out of str

Removed deriving Ord, which allowed to remove the stage markers
This commit is contained in:
Marvin Löbel 2013-04-22 21:42:25 +02:00
parent df61ec2da6
commit 582a05fc95
4 changed files with 35 additions and 38 deletions

View File

@ -159,6 +159,9 @@ pub mod vec;
pub mod at_vec; pub mod at_vec;
pub mod str; pub mod str;
#[path = "str/ascii.rs"]
pub mod ascii;
pub mod ptr; pub mod ptr;
pub mod owned; pub mod owned;
pub mod managed; pub mod managed;

View File

@ -40,11 +40,7 @@ pub use path::Path;
pub use path::PosixPath; pub use path::PosixPath;
pub use path::WindowsPath; pub use path::WindowsPath;
pub use ptr::Ptr; pub use ptr::Ptr;
// NOTE: Remove markers after snapshot pub use ascii::{Ascii, AsciiCast, OwnedAsciiCast, AsciiStr};
#[cfg(stage1)]
#[cfg(stage2)]
#[cfg(stage3)]
pub use str::{Ascii, AsciiCast, OwnedAsciiCast, ToStrAscii};
pub use str::{StrSlice, OwnedStr}; pub use str::{StrSlice, OwnedStr};
pub use to_bytes::IterBytes; pub use to_bytes::IterBytes;
pub use to_str::{ToStr, ToStrConsume}; pub use to_str::{ToStr, ToStrConsume};

View File

@ -17,12 +17,6 @@
* some heavy-duty uses, try std::rope. * some heavy-duty uses, try std::rope.
*/ */
// NOTE: Remove markers after snapshot
#[cfg(stage1)]
#[cfg(stage2)]
#[cfg(stage3)]
pub use self::ascii::{Ascii, AsciiCast, OwnedAsciiCast, ToStrAscii};
use at_vec; use at_vec;
use cast; use cast;
use char; use char;
@ -40,13 +34,6 @@ use to_str::ToStr;
#[cfg(notest)] use cmp::{Eq, Ord, Equiv, TotalEq}; #[cfg(notest)] use cmp::{Eq, Ord, Equiv, TotalEq};
// NOTE: Remove markers after snapshot
#[cfg(stage1)]
#[cfg(stage2)]
#[cfg(stage3)]
#[path = "str/ascii.rs"]
mod ascii;
/* /*
Section: Creating a string Section: Creating a string
*/ */

View File

@ -12,12 +12,8 @@ use to_str::{ToStr,ToStrConsume};
use str; use str;
use cast; use cast;
#[cfg(test)]
pub struct Ascii { priv chr: u8 }
/// Datatype to hold one ascii character. It is 8 bit long. /// Datatype to hold one ascii character. It is 8 bit long.
#[cfg(notest)] #[deriving(Clone, Eq)]
#[deriving(Clone, Eq, Ord)]
pub struct Ascii { priv chr: u8 } pub struct Ascii { priv chr: u8 }
pub impl Ascii { pub impl Ascii {
@ -163,18 +159,35 @@ impl OwnedAsciiCast for ~str {
} }
/// Trait for converting an ascii type to a string. Needed to convert `&[Ascii]` to `~str` /// Trait for converting an ascii type to a string. Needed to convert `&[Ascii]` to `~str`
pub trait ToStrAscii { pub trait AsciiStr {
/// Convert to a string. /// Convert to a string.
fn to_str_ascii(&self) -> ~str; fn to_str_ascii(&self) -> ~str;
/// Convert to vector representing a lower cased ascii string.
fn to_lower(&self) -> ~[Ascii];
/// Convert to vector representing a upper cased ascii string.
fn to_upper(&self) -> ~[Ascii];
} }
impl<'self> ToStrAscii for &'self [Ascii] { impl<'self> AsciiStr for &'self [Ascii] {
#[inline(always)] #[inline(always)]
fn to_str_ascii(&self) -> ~str { fn to_str_ascii(&self) -> ~str {
let mut cpy = self.to_owned(); let mut cpy = self.to_owned();
cpy.push(0u8.to_ascii()); cpy.push(0u8.to_ascii());
unsafe {cast::transmute(cpy)} unsafe {cast::transmute(cpy)}
} }
#[inline(always)]
fn to_lower(&self) -> ~[Ascii] {
self.map(|a| a.to_lower())
}
#[inline(always)]
fn to_upper(&self) -> ~[Ascii] {
self.map(|a| a.to_upper())
}
} }
impl ToStrConsume for ~[Ascii] { impl ToStrConsume for ~[Ascii] {
@ -186,13 +199,8 @@ impl ToStrConsume for ~[Ascii] {
} }
} }
// NOTE: Remove stage0 marker after snapshot
#[cfg(and(test, not(stage0)))]
mod tests { mod tests {
use super::*; use super::*;
use to_str::{ToStr,ToStrConsume};
use str;
use cast;
macro_rules! v2ascii ( macro_rules! v2ascii (
( [$($e:expr),*]) => ( [$(Ascii{chr:$e}),*]); ( [$($e:expr),*]) => ( [$(Ascii{chr:$e}),*]);
@ -206,15 +214,15 @@ mod tests {
assert_eq!('A'.to_ascii().to_char(), 'A'); assert_eq!('A'.to_ascii().to_char(), 'A');
assert_eq!('A'.to_ascii().to_byte(), 65u8); assert_eq!('A'.to_ascii().to_byte(), 65u8);
assert_eq!('A'.to_ascii().to_lower().to_char, 'a'); assert_eq!('A'.to_ascii().to_lower().to_char(), 'a');
assert_eq!('Z'.to_ascii().to_lower().to_char, 'z'); assert_eq!('Z'.to_ascii().to_lower().to_char(), 'z');
assert_eq!('a'.to_ascii().to_upper().to_char, 'A'); assert_eq!('a'.to_ascii().to_upper().to_char(), 'A');
assert_eq!('z'.to_ascii().to_upper().to_char, 'Z'); assert_eq!('z'.to_ascii().to_upper().to_char(), 'Z');
assert_eq!('@'.to_ascii().to_lower().to_char, '@'); assert_eq!('@'.to_ascii().to_lower().to_char(), '@');
assert_eq!('['.to_ascii().to_lower().to_char, '['); assert_eq!('['.to_ascii().to_lower().to_char(), '[');
assert_eq!('`'.to_ascii().to_upper().to_char, '`'); assert_eq!('`'.to_ascii().to_upper().to_char(), '`');
assert_eq!('{'.to_ascii().to_upper().to_char, '{'); assert_eq!('{'.to_ascii().to_upper().to_char(), '{');
} }
#[test] #[test]
@ -225,6 +233,9 @@ mod tests {
// if chained-from directly // if chained-from directly
let v = ~[40u8, 32u8, 59u8]; assert_eq!(v.to_ascii(), v2ascii!([40, 32, 59])); let v = ~[40u8, 32u8, 59u8]; assert_eq!(v.to_ascii(), v2ascii!([40, 32, 59]));
let v = ~"( ;"; assert_eq!(v.to_ascii(), v2ascii!([40, 32, 59])); let v = ~"( ;"; assert_eq!(v.to_ascii(), v2ascii!([40, 32, 59]));
assert_eq!("abCDef&?#".to_ascii().to_lower().to_str_ascii(), ~"abcdef&?#");
assert_eq!("abCDef&?#".to_ascii().to_upper().to_str_ascii(), ~"ABCDEF&?#");
} }
#[test] #[test]