@@ -217,14 +217,14 @@ pub trait OwnedAsciiCast {
217
217
218
218
/// Take ownership and cast to an ascii vector. Fail on non-ASCII input.
219
219
#[ inline]
220
- fn into_ascii ( self ) -> ~ [ Ascii ] {
220
+ fn into_ascii ( self ) -> Vec < Ascii > {
221
221
assert ! ( self . is_ascii( ) ) ;
222
222
unsafe { self . into_ascii_nocheck ( ) }
223
223
}
224
224
225
225
/// Take ownership and cast to an ascii vector. Return None on non-ASCII input.
226
226
#[ inline]
227
- fn into_ascii_opt ( self ) -> Option < ~ [ Ascii ] > {
227
+ fn into_ascii_opt ( self ) -> Option < Vec < Ascii > > {
228
228
if self . is_ascii ( ) {
229
229
Some ( unsafe { self . into_ascii_nocheck ( ) } )
230
230
} else {
@@ -234,7 +234,7 @@ pub trait OwnedAsciiCast {
234
234
235
235
/// Take ownership and cast to an ascii vector.
236
236
/// Does not perform validation checks.
237
- unsafe fn into_ascii_nocheck ( self ) -> ~ [ Ascii ] ;
237
+ unsafe fn into_ascii_nocheck ( self ) -> Vec < Ascii > ;
238
238
}
239
239
240
240
impl OwnedAsciiCast for ~[ u8 ] {
@@ -244,8 +244,8 @@ impl OwnedAsciiCast for ~[u8] {
244
244
}
245
245
246
246
#[ inline]
247
- unsafe fn into_ascii_nocheck ( self ) -> ~ [ Ascii ] {
248
- cast:: transmute ( self )
247
+ unsafe fn into_ascii_nocheck ( self ) -> Vec < Ascii > {
248
+ cast:: transmute ( Vec :: from_slice ( self . as_slice ( ) ) )
249
249
}
250
250
}
251
251
@@ -256,7 +256,20 @@ impl OwnedAsciiCast for ~str {
256
256
}
257
257
258
258
#[ inline]
259
- unsafe fn into_ascii_nocheck ( self ) -> ~[ Ascii ] {
259
+ unsafe fn into_ascii_nocheck ( self ) -> Vec < Ascii > {
260
+ let v: ~[ u8 ] = cast:: transmute ( self ) ;
261
+ v. into_ascii_nocheck ( )
262
+ }
263
+ }
264
+
265
+ impl OwnedAsciiCast for Vec < u8 > {
266
+ #[ inline]
267
+ fn is_ascii ( & self ) -> bool {
268
+ self . as_slice ( ) . is_ascii ( )
269
+ }
270
+
271
+ #[ inline]
272
+ unsafe fn into_ascii_nocheck ( self ) -> Vec < Ascii > {
260
273
cast:: transmute ( self )
261
274
}
262
275
}
@@ -268,10 +281,10 @@ pub trait AsciiStr {
268
281
fn as_str_ascii < ' a > ( & ' a self ) -> & ' a str ;
269
282
270
283
/// Convert to vector representing a lower cased ascii string.
271
- fn to_lower ( & self ) -> ~ [ Ascii ] ;
284
+ fn to_lower ( & self ) -> Vec < Ascii > ;
272
285
273
286
/// Convert to vector representing a upper cased ascii string.
274
- fn to_upper ( & self ) -> ~ [ Ascii ] ;
287
+ fn to_upper ( & self ) -> Vec < Ascii > ;
275
288
276
289
/// Compares two Ascii strings ignoring case.
277
290
fn eq_ignore_case ( self , other : & [ Ascii ] ) -> bool ;
@@ -284,12 +297,12 @@ impl<'a> AsciiStr for &'a [Ascii] {
284
297
}
285
298
286
299
#[ inline]
287
- fn to_lower ( & self ) -> ~ [ Ascii ] {
300
+ fn to_lower ( & self ) -> Vec < Ascii > {
288
301
self . iter ( ) . map ( |a| a. to_lower ( ) ) . collect ( )
289
302
}
290
303
291
304
#[ inline]
292
- fn to_upper ( & self ) -> ~ [ Ascii ] {
305
+ fn to_upper ( & self ) -> Vec < Ascii > {
293
306
self . iter ( ) . map ( |a| a. to_upper ( ) ) . collect ( )
294
307
}
295
308
@@ -309,19 +322,21 @@ impl IntoStr for ~[Ascii] {
309
322
impl IntoStr for Vec < Ascii > {
310
323
#[ inline]
311
324
fn into_str ( self ) -> ~str {
312
- let v: ~[ Ascii ] = self . move_iter ( ) . collect ( ) ;
313
- unsafe { cast:: transmute ( v) }
325
+ unsafe {
326
+ let s: & str = cast:: transmute ( self . as_slice ( ) ) ;
327
+ s. to_owned ( )
328
+ }
314
329
}
315
330
}
316
331
317
- /// Trait to convert to an owned byte array by consuming self
332
+ /// Trait to convert to an owned byte vector by consuming self
318
333
pub trait IntoBytes {
319
- /// Converts to an owned byte array by consuming self
320
- fn into_bytes ( self ) -> ~ [ u8 ] ;
334
+ /// Converts to an owned byte vector by consuming self
335
+ fn into_bytes ( self ) -> Vec < u8 > ;
321
336
}
322
337
323
- impl IntoBytes for ~ [ Ascii ] {
324
- fn into_bytes ( self ) -> ~ [ u8 ] {
338
+ impl IntoBytes for Vec < Ascii > {
339
+ fn into_bytes ( self ) -> Vec < u8 > {
325
340
unsafe { cast:: transmute ( self ) }
326
341
}
327
342
}
@@ -404,9 +419,11 @@ unsafe fn str_map_bytes(string: ~str, map: &'static [u8]) -> ~str {
404
419
405
420
#[ inline]
406
421
unsafe fn str_copy_map_bytes ( string : & str , map : & ' static [ u8 ] ) -> ~str {
407
- let bytes = string. bytes ( ) . map ( |b| map[ b as uint ] ) . collect :: < ~[ _ ] > ( ) ;
408
-
409
- str:: raw:: from_utf8_owned ( bytes)
422
+ let mut s = string. to_owned ( ) ;
423
+ for b in str:: raw:: as_owned_vec ( & mut s) . mut_iter ( ) {
424
+ * b = map[ * b as uint ] ;
425
+ }
426
+ s
410
427
}
411
428
412
429
static ASCII_LOWER_MAP : & ' static [ u8 ] = & [
@@ -492,7 +509,6 @@ mod tests {
492
509
macro_rules! v2ascii (
493
510
( [ $( $e: expr) ,* ] ) => ( & [ $( Ascii { chr: $e} ) ,* ] ) ;
494
511
( & [ $( $e: expr) ,* ] ) => ( & [ $( Ascii { chr: $e} ) ,* ] ) ;
495
- ( ~[ $( $e: expr) ,* ] ) => ( box [ $( Ascii { chr: $e} ) ,* ] ) ;
496
512
)
497
513
498
514
macro_rules! vec2ascii (
@@ -556,20 +572,17 @@ mod tests {
556
572
557
573
#[ test]
558
574
fn test_ascii_vec_ng ( ) {
559
- assert_eq ! ( Vec :: from_slice( "abCDef&?#" . to_ascii( ) . to_lower( ) ) . into_str( ) ,
560
- "abcdef&?#" . to_owned( ) ) ;
561
- assert_eq ! ( Vec :: from_slice( "abCDef&?#" . to_ascii( ) . to_upper( ) ) . into_str( ) ,
562
- "ABCDEF&?#" . to_owned( ) ) ;
563
- assert_eq ! ( Vec :: from_slice( "" . to_ascii( ) . to_lower( ) ) . into_str( ) , "" . to_owned( ) ) ;
564
- assert_eq ! ( Vec :: from_slice( "YMCA" . to_ascii( ) . to_lower( ) ) . into_str( ) , "ymca" . to_owned( ) ) ;
565
- assert_eq ! ( Vec :: from_slice( "abcDEFxyz:.;" . to_ascii( ) . to_upper( ) ) . into_str( ) ,
566
- "ABCDEFXYZ:.;" . to_owned( ) ) ;
575
+ assert_eq ! ( "abCDef&?#" . to_ascii( ) . to_lower( ) . into_str( ) , "abcdef&?#" . to_owned( ) ) ;
576
+ assert_eq ! ( "abCDef&?#" . to_ascii( ) . to_upper( ) . into_str( ) , "ABCDEF&?#" . to_owned( ) ) ;
577
+ assert_eq ! ( "" . to_ascii( ) . to_lower( ) . into_str( ) , "" . to_owned( ) ) ;
578
+ assert_eq ! ( "YMCA" . to_ascii( ) . to_lower( ) . into_str( ) , "ymca" . to_owned( ) ) ;
579
+ assert_eq ! ( "abcDEFxyz:.;" . to_ascii( ) . to_upper( ) . into_str( ) , "ABCDEFXYZ:.;" . to_owned( ) ) ;
567
580
}
568
581
569
582
#[ test]
570
583
fn test_owned_ascii_vec ( ) {
571
- assert_eq ! ( ( "( ;" . to_owned( ) ) . into_ascii( ) , v2ascii! ( ~ [ 40 , 32 , 59 ] ) ) ;
572
- assert_eq ! ( ( box [ 40u8 , 32u8 , 59u8 ] ) . into_ascii( ) , v2ascii! ( ~ [ 40 , 32 , 59 ] ) ) ;
584
+ assert_eq ! ( ( "( ;" . to_owned( ) ) . into_ascii( ) , vec2ascii! [ 40 , 32 , 59 ] ) ;
585
+ assert_eq ! ( ( box [ 40u8 , 32u8 , 59u8 ] ) . into_ascii( ) , vec2ascii! [ 40 , 32 , 59 ] ) ;
573
586
}
574
587
575
588
#[ test]
@@ -580,13 +593,13 @@ mod tests {
580
593
581
594
#[ test]
582
595
fn test_ascii_into_str ( ) {
583
- assert_eq ! ( v2ascii! ( ~ [ 40 , 32 , 59 ] ) . into_str( ) , "( ;" . to_owned( ) ) ;
596
+ assert_eq ! ( vec2ascii! [ 40 , 32 , 59 ] . into_str( ) , "( ;" . to_owned( ) ) ;
584
597
assert_eq ! ( vec2ascii!( 40 , 32 , 59 ) . into_str( ) , "( ;" . to_owned( ) ) ;
585
598
}
586
599
587
600
#[ test]
588
601
fn test_ascii_to_bytes ( ) {
589
- assert_eq ! ( v2ascii! ( ~ [ 40 , 32 , 59 ] ) . into_bytes( ) , box [ 40u8 , 32u8 , 59u8 ] ) ;
602
+ assert_eq ! ( vec2ascii! [ 40 , 32 , 59 ] . into_bytes( ) , vec! [ 40u8 , 32u8 , 59u8 ] ) ;
590
603
}
591
604
592
605
#[ test] #[ should_fail]
@@ -625,10 +638,10 @@ mod tests {
625
638
assert_eq ! ( v. to_ascii_opt( ) , Some ( v2) ) ;
626
639
assert_eq ! ( "zoä华" . to_ascii_opt( ) , None ) ;
627
640
628
- assert_eq ! ( ( box [ 40u8 , 32u8 , 59u8 ] ) . into_ascii_opt( ) , Some ( v2ascii! ( ~ [ 40 , 32 , 59 ] ) ) ) ;
629
- assert_eq ! ( ( box [ 127u8 , 128u8 , 255u8 ] ) . into_ascii_opt( ) , None ) ;
641
+ assert_eq ! ( ( vec! [ 40u8 , 32u8 , 59u8 ] ) . into_ascii_opt( ) , Some ( vec2ascii! [ 40 , 32 , 59 ] ) ) ;
642
+ assert_eq ! ( ( vec! [ 127u8 , 128u8 , 255u8 ] ) . into_ascii_opt( ) , None ) ;
630
643
631
- assert_eq ! ( ( "( ;" . to_owned( ) ) . into_ascii_opt( ) , Some ( v2ascii! ( ~ [ 40 , 32 , 59 ] ) ) ) ;
644
+ assert_eq ! ( ( "( ;" . to_owned( ) ) . into_ascii_opt( ) , Some ( vec2ascii! [ 40 , 32 , 59 ] ) ) ;
632
645
assert_eq ! ( ( "zoä华" . to_owned( ) ) . into_ascii_opt( ) , None ) ;
633
646
}
634
647
0 commit comments