1
Fork 0

Methodyfied the string ascii extionsion functions

Added into_owned() method for vectors
Added DoubleEnded Iterator impl to Option
Renamed nil.rs to unit.rs
This commit is contained in:
Marvin Löbel 2013-08-11 04:21:31 +02:00
parent 0efbb25a26
commit a00becd0eb
6 changed files with 87 additions and 46 deletions

View file

@ -23,7 +23,7 @@ use to_bytes::IterBytes;
use option::{Some, None};
/// Datatype to hold one ascii character. It wraps a `u8`, with the highest bit always zero.
#[deriving(Clone, Eq)]
#[deriving(Clone, Eq, Ord, TotalOrd, TotalEq)]
pub struct Ascii { priv chr: u8 }
impl Ascii {
@ -250,21 +250,40 @@ impl ToBytesConsume for ~[Ascii] {
}
}
/// Extension methods for ASCII-subset only operations on strings
pub trait StrAsciiExt {
/// Convert the string to ASCII upper case:
/// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
/// but non-ASCII letters are unchanged.
fn to_ascii_upper(&self) -> ~str;
/// Convert the string to ASCII upper case:
/// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
/// but non-ASCII letters are unchanged.
#[inline]
pub fn to_ascii_upper(string: &str) -> ~str {
map_bytes(string, ASCII_UPPER_MAP)
/// Convert the string to ASCII lower case:
/// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
/// but non-ASCII letters are unchanged.
fn to_ascii_lower(&self) -> ~str;
/// Check that two strings are an ASCII case-insensitive match.
/// Same as `to_ascii_lower(a) == to_ascii_lower(b)`,
/// but without allocating and copying temporary strings.
fn eq_ignore_ascii_case(&self, other: &str) -> bool;
}
/// Convert the string to ASCII lower case:
/// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
/// but non-ASCII letters are unchanged.
#[inline]
pub fn to_ascii_lower(string: &str) -> ~str {
map_bytes(string, ASCII_LOWER_MAP)
impl<'self> StrAsciiExt for &'self str {
#[inline]
fn to_ascii_upper(&self) -> ~str {
map_bytes(*self, ASCII_UPPER_MAP)
}
#[inline]
fn to_ascii_lower(&self) -> ~str {
map_bytes(*self, ASCII_LOWER_MAP)
}
#[inline]
fn eq_ignore_ascii_case(&self, other: &str) -> bool {
self.len() == other.len() && self.as_bytes().iter().zip(other.as_bytes().iter()).all(
|(byte_self, byte_other)| ASCII_LOWER_MAP[*byte_self] == ASCII_LOWER_MAP[*byte_other])
}
}
#[inline]
@ -283,15 +302,6 @@ fn map_bytes(string: &str, map: &'static [u8]) -> ~str {
result
}
/// Check that two strings are an ASCII case-insensitive match.
/// Same as `to_ascii_lower(a) == to_ascii_lower(b)`,
/// but without allocating and copying temporary strings.
#[inline]
pub fn eq_ignore_ascii_case(a: &str, b: &str) -> bool {
a.len() == b.len() && a.as_bytes().iter().zip(b.as_bytes().iter()).all(
|(byte_a, byte_b)| ASCII_LOWER_MAP[*byte_a] == ASCII_LOWER_MAP[*byte_b])
}
static ASCII_LOWER_MAP: &'static [u8] = &[
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
@ -453,49 +463,48 @@ mod tests {
#[test]
fn test_to_ascii_upper() {
assert_eq!(to_ascii_upper("url()URL()uRl()ürl"), ~"URL()URL()URL()üRL");
assert_eq!(to_ascii_upper("hıß"), ~"Hıß");
assert_eq!("url()URL()uRl()ürl".to_ascii_upper(), ~"URL()URL()URL()üRL");
assert_eq!("hıß".to_ascii_upper(), ~"Hıß");
let mut i = 0;
while i <= 500 {
let c = i as char;
let upper = if 'a' <= c && c <= 'z' { c + 'A' - 'a' } else { c };
assert_eq!(to_ascii_upper(from_char(i as char)), from_char(upper))
assert_eq!(from_char(i as char).to_ascii_upper(), from_char(upper))
i += 1;
}
}
#[test]
fn test_to_ascii_lower() {
assert_eq!(to_ascii_lower("url()URL()uRl()Ürl"), ~"url()url()url()Ürl");
assert_eq!("url()URL()uRl()Ürl".to_ascii_lower(), ~"url()url()url()Ürl");
// Dotted capital I, Kelvin sign, Sharp S.
assert_eq!(to_ascii_lower("ß"), ~"ß");
assert_eq!("ß".to_ascii_lower(), ~"ß");
let mut i = 0;
while i <= 500 {
let c = i as char;
let lower = if 'A' <= c && c <= 'Z' { c + 'a' - 'A' } else { c };
assert_eq!(to_ascii_lower(from_char(i as char)), from_char(lower))
assert_eq!(from_char(i as char).to_ascii_lower(), from_char(lower))
i += 1;
}
}
#[test]
fn test_eq_ignore_ascii_case() {
assert!(eq_ignore_ascii_case("url()URL()uRl()Ürl", "url()url()url()Ürl"));
assert!(!eq_ignore_ascii_case("Ürl", "ürl"));
assert!("url()URL()uRl()Ürl".eq_ignore_ascii_case("url()url()url()Ürl"));
assert!(!"Ürl".eq_ignore_ascii_case("ürl"));
// Dotted capital I, Kelvin sign, Sharp S.
assert!(eq_ignore_ascii_case("ß", "ß"));
assert!(!eq_ignore_ascii_case("İ", "i"));
assert!(!eq_ignore_ascii_case("", "k"));
assert!(!eq_ignore_ascii_case("ß", "s"));
assert!("ß".eq_ignore_ascii_case("ß"));
assert!(!"İ".eq_ignore_ascii_case("i"));
assert!(!"".eq_ignore_ascii_case("k"));
assert!(!"ß".eq_ignore_ascii_case("s"));
let mut i = 0;
while i <= 500 {
let c = i as char;
let lower = if 'A' <= c && c <= 'Z' { c + 'a' - 'A' } else { c };
assert!(eq_ignore_ascii_case(from_char(i as char), from_char(lower)));
assert!(from_char(i as char).eq_ignore_ascii_case(from_char(lower)));
i += 1;
}
}