@@ -12,12 +12,8 @@ use to_str::{ToStr,ToStrConsume};
12
12
use str;
13
13
use cast;
14
14
15
- #[ cfg( test) ]
16
- pub struct Ascii { priv chr: u8 }
17
-
18
15
/// Datatype to hold one ascii character. It is 8 bit long.
19
- #[ cfg( notest) ]
20
- #[ deriving( Clone , Eq , Ord ) ]
16
+ #[ deriving( Clone , Eq ) ]
21
17
pub struct Ascii { priv chr: u8 }
22
18
23
19
pub impl Ascii {
@@ -163,18 +159,35 @@ impl OwnedAsciiCast for ~str {
163
159
}
164
160
165
161
/// Trait for converting an ascii type to a string. Needed to convert `&[Ascii]` to `~str`
166
- pub trait ToStrAscii {
162
+ pub trait AsciiStr {
167
163
/// Convert to a string.
168
164
fn to_str_ascii ( & self ) -> ~str ;
165
+
166
+ /// Convert to vector representing a lower cased ascii string.
167
+ fn to_lower ( & self ) -> ~[ Ascii ] ;
168
+
169
+ /// Convert to vector representing a upper cased ascii string.
170
+ fn to_upper ( & self ) -> ~[ Ascii ] ;
171
+
169
172
}
170
173
171
- impl < ' self > ToStrAscii for & ' self [ Ascii ] {
174
+ impl < ' self > AsciiStr for & ' self [ Ascii ] {
172
175
#[ inline( always) ]
173
176
fn to_str_ascii ( & self ) -> ~str {
174
177
let mut cpy = self . to_owned ( ) ;
175
178
cpy. push ( 0u8 . to_ascii ( ) ) ;
176
179
unsafe { cast:: transmute ( cpy) }
177
180
}
181
+
182
+ #[ inline( always) ]
183
+ fn to_lower ( & self ) -> ~[ Ascii ] {
184
+ self . map ( |a| a. to_lower ( ) )
185
+ }
186
+
187
+ #[ inline( always) ]
188
+ fn to_upper ( & self ) -> ~[ Ascii ] {
189
+ self . map ( |a| a. to_upper ( ) )
190
+ }
178
191
}
179
192
180
193
impl ToStrConsume for ~[ Ascii ] {
@@ -186,13 +199,8 @@ impl ToStrConsume for ~[Ascii] {
186
199
}
187
200
}
188
201
189
- // NOTE: Remove stage0 marker after snapshot
190
- #[ cfg( and( test, not( stage0) ) ) ]
191
202
mod tests {
192
203
use super :: * ;
193
- use to_str:: { ToStr , ToStrConsume } ;
194
- use str;
195
- use cast;
196
204
197
205
macro_rules! v2ascii (
198
206
( [ $( $e: expr) ,* ] ) => ( [ $( Ascii { chr: $e} ) ,* ] ) ;
@@ -206,15 +214,15 @@ mod tests {
206
214
assert_eq ! ( 'A' . to_ascii( ) . to_char( ) , 'A' ) ;
207
215
assert_eq ! ( 'A' . to_ascii( ) . to_byte( ) , 65u8 ) ;
208
216
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' ) ;
217
+ assert_eq ! ( 'A' . to_ascii( ) . to_lower( ) . to_char( ) , 'a' ) ;
218
+ assert_eq ! ( 'Z' . to_ascii( ) . to_lower( ) . to_char( ) , 'z' ) ;
219
+ assert_eq ! ( 'a' . to_ascii( ) . to_upper( ) . to_char( ) , 'A' ) ;
220
+ assert_eq ! ( 'z' . to_ascii( ) . to_upper( ) . to_char( ) , 'Z' ) ;
213
221
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, '{' ) ;
222
+ assert_eq ! ( '@' . to_ascii( ) . to_lower( ) . to_char( ) , '@' ) ;
223
+ assert_eq ! ( '[' . to_ascii( ) . to_lower( ) . to_char( ) , '[' ) ;
224
+ assert_eq ! ( '`' . to_ascii( ) . to_upper( ) . to_char( ) , '`' ) ;
225
+ assert_eq ! ( '{' . to_ascii( ) . to_upper( ) . to_char( ) , '{' ) ;
218
226
}
219
227
220
228
#[ test]
@@ -225,6 +233,9 @@ mod tests {
225
233
// if chained-from directly
226
234
let v = ~[ 40u8 , 32u8 , 59u8 ] ; assert_eq ! ( v. to_ascii( ) , v2ascii!( [ 40 , 32 , 59 ] ) ) ;
227
235
let v = ~"( ; "; assert_eq!(v.to_ascii(), v2ascii!([40, 32, 59]));
236
+
237
+ assert_eq!(" abCDef& ?#". to_ascii ( ) . to_lower ( ) . to_str_ascii ( ) , ~"abcdef& ?#") ;
238
+ assert_eq!( "abCDef&?#" . to_ascii( ) . to_upper( ) . to_str_ascii( ) , ~"ABCDEF & ?#");
228
239
}
229
240
230
241
#[test]
0 commit comments