@@ -17,9 +17,8 @@ use cast;
17
17
use old_iter:: BaseIter ;
18
18
use iterator:: IteratorUtil ;
19
19
use vec:: { CopyableVector , ImmutableVector , OwnedVector } ;
20
- use to_bytes:: IterBytes ;
21
20
22
- /// Datatype to hold one ascii character. It wraps a `u8`, with the highest bit always zero .
21
+ /// Datatype to hold one ascii character. It is 8 bit long .
23
22
#[ deriving( Clone , Eq ) ]
24
23
pub struct Ascii { priv chr: u8 }
25
24
@@ -73,9 +72,6 @@ pub trait AsciiCast<T> {
73
72
/// Convert to an ascii type
74
73
fn to_ascii ( & self ) -> T ;
75
74
76
- /// Convert to an ascii type, not doing any range asserts
77
- unsafe fn to_ascii_nocheck ( & self ) -> T ;
78
-
79
75
/// Check if convertible to ascii
80
76
fn is_ascii ( & self ) -> bool ;
81
77
}
@@ -84,12 +80,7 @@ impl<'self> AsciiCast<&'self[Ascii]> for &'self [u8] {
84
80
#[ inline( always) ]
85
81
fn to_ascii ( & self ) -> & ' self [ Ascii ] {
86
82
assert ! ( self . is_ascii( ) ) ;
87
- unsafe { self . to_ascii_nocheck ( ) }
88
- }
89
-
90
- #[ inline( always) ]
91
- unsafe fn to_ascii_nocheck ( & self ) -> & ' self [ Ascii ] {
92
- cast:: transmute ( * self )
83
+ unsafe { cast:: transmute ( * self ) }
93
84
}
94
85
95
86
#[ inline( always) ]
@@ -105,13 +96,8 @@ impl<'self> AsciiCast<&'self[Ascii]> for &'self str {
105
96
#[ inline( always) ]
106
97
fn to_ascii ( & self ) -> & ' self [ Ascii ] {
107
98
assert ! ( self . is_ascii( ) ) ;
108
- unsafe { self . to_ascii_nocheck ( ) }
109
- }
110
-
111
- #[ inline( always) ]
112
- unsafe fn to_ascii_nocheck ( & self ) -> & ' self [ Ascii ] {
113
- let ( p, len) : ( * u8 , uint ) = cast:: transmute ( * self ) ;
114
- cast:: transmute ( ( p, len - 1 ) )
99
+ let ( p, len) : ( * u8 , uint ) = unsafe { cast:: transmute ( * self ) } ;
100
+ unsafe { cast:: transmute ( ( p, len - 1 ) ) }
115
101
}
116
102
117
103
#[ inline( always) ]
@@ -124,11 +110,6 @@ impl AsciiCast<Ascii> for u8 {
124
110
#[ inline( always) ]
125
111
fn to_ascii ( & self ) -> Ascii {
126
112
assert ! ( self . is_ascii( ) ) ;
127
- unsafe { self . to_ascii_nocheck ( ) }
128
- }
129
-
130
- #[ inline( always) ]
131
- unsafe fn to_ascii_nocheck ( & self ) -> Ascii {
132
113
Ascii { chr : * self }
133
114
}
134
115
@@ -142,11 +123,6 @@ impl AsciiCast<Ascii> for char {
142
123
#[ inline( always) ]
143
124
fn to_ascii ( & self ) -> Ascii {
144
125
assert ! ( self . is_ascii( ) ) ;
145
- unsafe { self . to_ascii_nocheck ( ) }
146
- }
147
-
148
- #[ inline( always) ]
149
- unsafe fn to_ascii_nocheck ( & self ) -> Ascii {
150
126
Ascii { chr : * self as u8 }
151
127
}
152
128
@@ -159,38 +135,26 @@ impl AsciiCast<Ascii> for char {
159
135
/// Trait for copyless casting to an ascii vector.
160
136
pub trait OwnedAsciiCast {
161
137
/// Take ownership and cast to an ascii vector without trailing zero element.
162
- fn into_ascii ( self ) -> ~[ Ascii ] ;
163
-
164
- /// Take ownership and cast to an ascii vector without trailing zero element.
165
- /// Does not perform validation checks.
166
- unsafe fn into_ascii_nocheck ( self ) -> ~[ Ascii ] ;
138
+ fn to_ascii_consume ( self ) -> ~[ Ascii ] ;
167
139
}
168
140
169
141
impl OwnedAsciiCast for ~[ u8 ] {
170
142
#[ inline( always) ]
171
- fn into_ascii ( self ) -> ~[ Ascii ] {
143
+ fn to_ascii_consume ( self ) -> ~[ Ascii ] {
172
144
assert ! ( self . is_ascii( ) ) ;
173
- unsafe { self . into_ascii_nocheck ( ) }
174
- }
175
-
176
- #[ inline( always) ]
177
- unsafe fn into_ascii_nocheck ( self ) -> ~[ Ascii ] {
178
- cast:: transmute ( self )
145
+ unsafe { cast:: transmute ( self ) }
179
146
}
180
147
}
181
148
182
149
impl OwnedAsciiCast for ~str {
183
150
#[ inline( always) ]
184
- fn into_ascii ( self ) -> ~[ Ascii ] {
151
+ fn to_ascii_consume ( self ) -> ~[ Ascii ] {
185
152
assert ! ( self . is_ascii( ) ) ;
186
- unsafe { self . into_ascii_nocheck ( ) }
187
- }
188
-
189
- #[ inline( always) ]
190
- unsafe fn into_ascii_nocheck ( self ) -> ~[ Ascii ] {
191
- let mut r: ~[ Ascii ] = cast:: transmute ( self ) ;
192
- r. pop ( ) ;
193
- r
153
+ let mut s = self ;
154
+ unsafe {
155
+ str:: raw:: pop_byte ( & mut s) ;
156
+ cast:: transmute ( s)
157
+ }
194
158
}
195
159
}
196
160
@@ -205,8 +169,6 @@ pub trait AsciiStr {
205
169
/// Convert to vector representing a upper cased ascii string.
206
170
fn to_upper ( & self ) -> ~[ Ascii ] ;
207
171
208
- /// Compares two Ascii strings ignoring case
209
- fn eq_ignore_case ( self , other : & [ Ascii ] ) -> bool ;
210
172
}
211
173
212
174
impl < ' self > AsciiStr for & ' self [ Ascii ] {
@@ -226,45 +188,20 @@ impl<'self> AsciiStr for &'self [Ascii] {
226
188
fn to_upper ( & self ) -> ~[ Ascii ] {
227
189
self . map ( |a| a. to_upper ( ) )
228
190
}
229
-
230
- #[ inline( always) ]
231
- fn eq_ignore_case ( self , other : & [ Ascii ] ) -> bool {
232
- do self . iter ( ) . zip ( other. iter ( ) ) . all |( & a, & b) | { a. eq_ignore_case ( b) }
233
- }
234
191
}
235
192
236
193
impl ToStrConsume for ~[ Ascii ] {
237
194
#[ inline( always) ]
238
- fn into_str ( self ) -> ~str {
195
+ fn to_str_consume ( self ) -> ~str {
239
196
let mut cpy = self ;
240
197
cpy. push ( 0u8 . to_ascii ( ) ) ;
241
198
unsafe { cast:: transmute ( cpy) }
242
199
}
243
200
}
244
201
245
- impl IterBytes for Ascii {
246
- #[ inline( always) ]
247
- fn iter_bytes ( & self , _lsb0 : bool , f : & fn ( buf : & [ u8 ] ) -> bool ) -> bool {
248
- f ( [ self . to_byte ( ) ] )
249
- }
250
- }
251
-
252
- /// Trait to convert to a owned byte array by consuming self
253
- pub trait ToBytesConsume {
254
- /// Converts to a owned byte array by consuming self
255
- fn into_bytes ( self ) -> ~[ u8 ] ;
256
- }
257
-
258
- impl ToBytesConsume for ~[ Ascii ] {
259
- fn into_bytes ( self ) -> ~[ u8 ] {
260
- unsafe { cast:: transmute ( self ) }
261
- }
262
- }
263
-
264
202
#[ cfg( test) ]
265
203
mod tests {
266
204
use super :: * ;
267
- use to_bytes:: ToBytes ;
268
205
269
206
macro_rules! v2ascii (
270
207
( [ $( $e: expr) ,* ] ) => ( [ $( Ascii { chr: $e} ) ,* ] ) ;
@@ -308,8 +245,6 @@ mod tests {
308
245
assert_eq!(" YMCA ".to_ascii().to_lower().to_str_ascii(), ~" ymca");
309
246
assert_eq!(" abcDEFxyz: . ; ".to_ascii().to_upper().to_str_ascii(), ~" ABCDEFXYZ : . ; ");
310
247
311
- assert!(" aBcDeF& ?#".to_ascii().eq_ignore_case(" AbCdEf & ?#".to_ascii()));
312
-
313
248
assert!(" ".is_ascii());
314
249
assert!(" a".is_ascii());
315
250
assert!(!"\u2009 " . is_ascii( ) ) ;
@@ -318,22 +253,21 @@ mod tests {
318
253
319
254
#[ test]
320
255
fn test_owned_ascii_vec ( ) {
321
- assert_eq!( ( ~"( ; ").into_ascii(), v2ascii!(~[40, 32, 59]));
322
- assert_eq!((~[40u8, 32u8, 59u8]).into_ascii(), v2ascii!(~[40, 32, 59]));
256
+ // FIXME: #4318 Compiler crashes on moving self
257
+ //assert_eq!(~"( ;".to_ascii_consume(), v2ascii!(~[40, 32, 59]));
258
+ //assert_eq!(~[40u8, 32u8, 59u8].to_ascii_consume(), v2ascii!(~[40, 32, 59]));
259
+ //assert_eq!(~"( ;".to_ascii_consume_with_null(), v2ascii!(~[40, 32, 59, 0]));
260
+ //assert_eq!(~[40u8, 32u8, 59u8].to_ascii_consume_with_null(),
261
+ // v2ascii!(~[40, 32, 59, 0]));
323
262
}
324
263
325
264
#[ test]
326
265
fn test_ascii_to_str ( ) { assert_eq!( v2ascii!( [ 40 , 32 , 59 ] ) . to_str_ascii( ) , ~"( ; "); }
327
266
328
267
#[test]
329
- fn test_ascii_into_str() {
330
- assert_eq!(v2ascii!(~[40, 32, 59]).into_str(), ~" ( ; ");
331
- }
332
-
333
- #[test]
334
- fn test_ascii_to_bytes() {
335
- assert_eq!(v2ascii!(~[40, 32, 59]).to_bytes(false), ~[40u8, 32u8, 59u8]);
336
- assert_eq!(v2ascii!(~[40, 32, 59]).into_bytes(), ~[40u8, 32u8, 59u8]);
268
+ fn test_ascii_to_str_consume() {
269
+ // FIXME: #4318 Compiler crashes on moving self
270
+ //assert_eq!(v2ascii!(~[40, 32, 59]).to_str_consume(), ~" ( ; ");
337
271
}
338
272
339
273
#[test] #[should_fail]
0 commit comments