Skip to content

Commit 7ca216d

Browse files
committed
Added case functions to Ascii
1 parent 61ffee7 commit 7ca216d

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

src/libcore/str/ascii.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,46 @@ pub struct Ascii { priv chr: u8 }
2222

2323
pub impl Ascii {
2424
/// Converts a ascii character into a `u8`.
25+
#[inline(always)]
2526
fn to_byte(self) -> u8 {
2627
self.chr
2728
}
2829

2930
/// Converts a ascii character into a `char`.
31+
#[inline(always)]
3032
fn to_char(self) -> char {
3133
self.chr as char
3234
}
35+
36+
/// Convert to lowercase.
37+
#[inline(always)]
38+
fn to_lower(self) -> Ascii {
39+
if self.chr >= 65 && self.chr <= 90 {
40+
Ascii{chr: self.chr | 0x20 }
41+
} else {
42+
self
43+
}
44+
}
45+
46+
/// Convert to uppercase.
47+
#[inline(always)]
48+
fn to_upper(self) -> Ascii {
49+
if self.chr >= 97 && self.chr <= 122 {
50+
Ascii{chr: self.chr & !0x20 }
51+
} else {
52+
self
53+
}
54+
}
55+
56+
// Compares two ascii characters of equality, ignoring case.
57+
#[inline(always)]
58+
fn eq_ignore_case(self, other: Ascii) -> bool {
59+
self.to_lower().chr == other.to_lower().chr
60+
}
3361
}
3462

3563
impl ToStr for Ascii {
64+
#[inline(always)]
3665
fn to_str(&self) -> ~str { str::from_bytes(['\'' as u8, self.chr, '\'' as u8]) }
3766
}
3867

@@ -46,12 +75,14 @@ pub trait AsciiCast<T> {
4675
}
4776

4877
impl<'self> AsciiCast<&'self[Ascii]> for &'self [u8] {
78+
#[inline(always)]
4979
fn to_ascii(&self) -> &'self[Ascii] {
5080
assert!(self.is_ascii());
5181

5282
unsafe{ cast::transmute(*self) }
5383
}
5484

85+
#[inline(always)]
5586
fn is_ascii(&self) -> bool {
5687
for self.each |b| {
5788
if !b.is_ascii() { return false; }
@@ -61,13 +92,15 @@ impl<'self> AsciiCast<&'self[Ascii]> for &'self [u8] {
6192
}
6293

6394
impl<'self> AsciiCast<&'self[Ascii]> for &'self str {
95+
#[inline(always)]
6496
fn to_ascii(&self) -> &'self[Ascii] {
6597
assert!(self.is_ascii());
6698

6799
let (p,len): (*u8, uint) = unsafe{ cast::transmute(*self) };
68100
unsafe{ cast::transmute((p, len - 1))}
69101
}
70102

103+
#[inline(always)]
71104
fn is_ascii(&self) -> bool {
72105
for self.each |b| {
73106
if !b.is_ascii() { return false; }
@@ -77,22 +110,27 @@ impl<'self> AsciiCast<&'self[Ascii]> for &'self str {
77110
}
78111

79112
impl AsciiCast<Ascii> for u8 {
113+
#[inline(always)]
80114
fn to_ascii(&self) -> Ascii {
81115
assert!(self.is_ascii());
82116
Ascii{ chr: *self }
83117
}
84118

119+
#[inline(always)]
85120
fn is_ascii(&self) -> bool {
86121
*self & 128 == 0u8
87122
}
88123
}
89124

90125
impl AsciiCast<Ascii> for char {
126+
127+
#[inline(always)]
91128
fn to_ascii(&self) -> Ascii {
92129
assert!(self.is_ascii());
93130
Ascii{ chr: *self as u8 }
94131
}
95132

133+
#[inline(always)]
96134
fn is_ascii(&self) -> bool {
97135
*self - ('\x7F' & *self) == '\x00'
98136
}
@@ -105,6 +143,7 @@ pub trait OwnedAsciiCast {
105143
}
106144

107145
impl OwnedAsciiCast for ~[u8] {
146+
#[inline(always)]
108147
fn to_ascii_consume(self) -> ~[Ascii] {
109148
assert!(self.is_ascii());
110149

@@ -113,6 +152,7 @@ impl OwnedAsciiCast for ~[u8] {
113152
}
114153

115154
impl OwnedAsciiCast for ~str {
155+
#[inline(always)]
116156
fn to_ascii_consume(self) -> ~[Ascii] {
117157
let mut s = self;
118158
unsafe {
@@ -129,6 +169,7 @@ pub trait ToStrAscii {
129169
}
130170

131171
impl<'self> ToStrAscii for &'self [Ascii] {
172+
#[inline(always)]
132173
fn to_str_ascii(&self) -> ~str {
133174
let mut cpy = self.to_owned();
134175
cpy.push(0u8.to_ascii());
@@ -137,6 +178,7 @@ impl<'self> ToStrAscii for &'self [Ascii] {
137178
}
138179

139180
impl ToStrConsume for ~[Ascii] {
181+
#[inline(always)]
140182
fn to_str_consume(self) -> ~str {
141183
let mut cpy = self;
142184
cpy.push(0u8.to_ascii());
@@ -163,6 +205,16 @@ mod tests {
163205
assert_eq!(65u8.to_ascii().to_char(), 'A');
164206
assert_eq!('A'.to_ascii().to_char(), 'A');
165207
assert_eq!('A'.to_ascii().to_byte(), 65u8);
208+
209+
assert_eq!('A'.to_ascii().to_lower().to_char, 'a');
210+
assert_eq!('Z'.to_ascii().to_lower().to_char, 'z');
211+
assert_eq!('a'.to_ascii().to_upper().to_char, 'A');
212+
assert_eq!('z'.to_ascii().to_upper().to_char, 'Z');
213+
214+
assert_eq!('@'.to_ascii().to_lower().to_char, '@');
215+
assert_eq!('['.to_ascii().to_lower().to_char, '[');
216+
assert_eq!('`'.to_ascii().to_upper().to_char, '`');
217+
assert_eq!('{'.to_ascii().to_upper().to_char, '{');
166218
}
167219

168220
#[test]

0 commit comments

Comments
 (0)