12
12
13
13
//! Operations on ASCII strings and characters
14
14
15
- #![ experimental ]
15
+ #![ unstable = "unsure about placement and naming" ]
16
16
17
17
use core:: kinds:: Sized ;
18
18
use fmt;
@@ -31,30 +31,40 @@ pub struct Ascii { chr: u8 }
31
31
impl Ascii {
32
32
/// Converts an ascii character into a `u8`.
33
33
#[ inline]
34
- pub fn to_byte ( self ) -> u8 {
34
+ #[ unstable = "recently renamed" ]
35
+ pub fn as_byte ( & self ) -> u8 {
35
36
self . chr
36
37
}
37
38
39
+ #[ deprecated = "use as_byte" ]
40
+ pub fn to_byte ( self ) -> u8 {
41
+ self . as_byte ( )
42
+ }
43
+
38
44
/// Converts an ascii character into a `char`.
39
45
#[ inline]
40
- pub fn to_char ( self ) -> char {
46
+ #[ unstable = "recently renamed" ]
47
+ pub fn as_char ( & self ) -> char {
41
48
self . chr as char
42
49
}
43
50
44
51
/// Convert to lowercase.
45
52
#[ inline]
46
- pub fn to_lowercase ( self ) -> Ascii {
53
+ #[ stable]
54
+ pub fn to_lowercase ( & self ) -> Ascii {
47
55
Ascii { chr : ASCII_LOWER_MAP [ self . chr as uint ] }
48
56
}
49
57
50
58
/// Convert to uppercase.
51
59
#[ inline]
52
- pub fn to_uppercase ( self ) -> Ascii {
60
+ #[ stable]
61
+ pub fn to_uppercase ( & self ) -> Ascii {
53
62
Ascii { chr : ASCII_UPPER_MAP [ self . chr as uint ] }
54
63
}
55
64
56
65
/// Compares two ascii characters of equality, ignoring case.
57
66
#[ inline]
67
+ #[ deprecated = "normalize with to_lowercase" ]
58
68
pub fn eq_ignore_case ( self , other : Ascii ) -> bool {
59
69
ASCII_LOWER_MAP [ self . chr as uint ] == ASCII_LOWER_MAP [ other. chr as uint ]
60
70
}
@@ -63,66 +73,77 @@ impl Ascii {
63
73
64
74
/// Check if the character is a letter (a-z, A-Z)
65
75
#[ inline]
76
+ #[ stable]
66
77
pub fn is_alphabetic ( & self ) -> bool {
67
78
( self . chr >= 0x41 && self . chr <= 0x5A ) || ( self . chr >= 0x61 && self . chr <= 0x7A )
68
79
}
69
80
70
81
/// Check if the character is a number (0-9)
71
82
#[ inline]
83
+ #[ unstable = "may be renamed" ]
72
84
pub fn is_digit ( & self ) -> bool {
73
85
self . chr >= 0x30 && self . chr <= 0x39
74
86
}
75
87
76
88
/// Check if the character is a letter or number
77
89
#[ inline]
90
+ #[ stable]
78
91
pub fn is_alphanumeric ( & self ) -> bool {
79
92
self . is_alphabetic ( ) || self . is_digit ( )
80
93
}
81
94
82
95
/// Check if the character is a space or horizontal tab
83
96
#[ inline]
97
+ #[ experimental = "likely to be removed" ]
84
98
pub fn is_blank ( & self ) -> bool {
85
99
self . chr == b' ' || self . chr == b'\t'
86
100
}
87
101
88
102
/// Check if the character is a control character
89
103
#[ inline]
104
+ #[ stable]
90
105
pub fn is_control ( & self ) -> bool {
91
106
self . chr < 0x20 || self . chr == 0x7F
92
107
}
93
108
94
109
/// Checks if the character is printable (except space)
95
110
#[ inline]
111
+ #[ experimental = "unsure about naming, or whether this is needed" ]
96
112
pub fn is_graph ( & self ) -> bool {
97
113
( self . chr - 0x21 ) < 0x5E
98
114
}
99
115
100
116
/// Checks if the character is printable (including space)
101
117
#[ inline]
118
+ #[ unstable = "unsure about naming" ]
102
119
pub fn is_print ( & self ) -> bool {
103
120
( self . chr - 0x20 ) < 0x5F
104
121
}
105
122
106
- /// Checks if the character is lowercase
123
+ /// Checks if the character is alphabetic and lowercase
107
124
#[ inline]
125
+ #[ stable]
108
126
pub fn is_lowercase ( & self ) -> bool {
109
127
( self . chr - b'a' ) < 26
110
128
}
111
129
112
- /// Checks if the character is uppercase
130
+ /// Checks if the character is alphabetic and uppercase
113
131
#[ inline]
132
+ #[ stable]
114
133
pub fn is_uppercase ( & self ) -> bool {
115
134
( self . chr - b'A' ) < 26
116
135
}
117
136
118
137
/// Checks if the character is punctuation
119
138
#[ inline]
139
+ #[ stable]
120
140
pub fn is_punctuation ( & self ) -> bool {
121
141
self . is_graph ( ) && !self . is_alphanumeric ( )
122
142
}
123
143
124
144
/// Checks if the character is a valid hex digit
125
145
#[ inline]
146
+ #[ stable]
126
147
pub fn is_hex ( & self ) -> bool {
127
148
self . is_digit ( ) || ( ( self . chr | 32u8 ) - b'a' ) < 6
128
149
}
@@ -135,6 +156,7 @@ impl<'a> fmt::Show for Ascii {
135
156
}
136
157
137
158
/// Trait for converting into an ascii type.
159
+ #[ experimental = "may be replaced by generic conversion traits" ]
138
160
pub trait AsciiCast < T > {
139
161
/// Convert to an ascii type, panic on non-ASCII input.
140
162
#[ inline]
@@ -160,6 +182,7 @@ pub trait AsciiCast<T> {
160
182
fn is_ascii ( & self ) -> bool ;
161
183
}
162
184
185
+ #[ experimental = "may be replaced by generic conversion traits" ]
163
186
impl < ' a > AsciiCast < & ' a [ Ascii ] > for & ' a [ u8 ] {
164
187
#[ inline]
165
188
unsafe fn to_ascii_nocheck ( & self ) -> & ' a [ Ascii ] {
@@ -175,6 +198,7 @@ impl<'a> AsciiCast<&'a[Ascii]> for &'a [u8] {
175
198
}
176
199
}
177
200
201
+ #[ experimental = "may be replaced by generic conversion traits" ]
178
202
impl < ' a > AsciiCast < & ' a [ Ascii ] > for & ' a str {
179
203
#[ inline]
180
204
unsafe fn to_ascii_nocheck ( & self ) -> & ' a [ Ascii ] {
@@ -187,6 +211,7 @@ impl<'a> AsciiCast<&'a [Ascii]> for &'a str {
187
211
}
188
212
}
189
213
214
+ #[ experimental = "may be replaced by generic conversion traits" ]
190
215
impl AsciiCast < Ascii > for u8 {
191
216
#[ inline]
192
217
unsafe fn to_ascii_nocheck ( & self ) -> Ascii {
@@ -199,6 +224,7 @@ impl AsciiCast<Ascii> for u8 {
199
224
}
200
225
}
201
226
227
+ #[ experimental = "may be replaced by generic conversion traits" ]
202
228
impl AsciiCast < Ascii > for char {
203
229
#[ inline]
204
230
unsafe fn to_ascii_nocheck ( & self ) -> Ascii {
@@ -212,6 +238,7 @@ impl AsciiCast<Ascii> for char {
212
238
}
213
239
214
240
/// Trait for copyless casting to an ascii vector.
241
+ #[ experimental = "may be replaced by generic conversion traits" ]
215
242
pub trait OwnedAsciiCast {
216
243
/// Check if convertible to ascii
217
244
fn is_ascii ( & self ) -> bool ;
@@ -241,6 +268,7 @@ pub trait OwnedAsciiCast {
241
268
unsafe fn into_ascii_nocheck ( self ) -> Vec < Ascii > ;
242
269
}
243
270
271
+ #[ experimental = "may be replaced by generic conversion traits" ]
244
272
impl OwnedAsciiCast for String {
245
273
#[ inline]
246
274
fn is_ascii ( & self ) -> bool {
@@ -253,6 +281,7 @@ impl OwnedAsciiCast for String {
253
281
}
254
282
}
255
283
284
+ #[ experimental = "may be replaced by generic conversion traits" ]
256
285
impl OwnedAsciiCast for Vec < u8 > {
257
286
#[ inline]
258
287
fn is_ascii ( & self ) -> bool {
@@ -274,6 +303,7 @@ impl OwnedAsciiCast for Vec<u8> {
274
303
275
304
/// Trait for converting an ascii type to a string. Needed to convert
276
305
/// `&[Ascii]` to `&str`.
306
+ #[ experimental = "may be replaced by generic conversion traits" ]
277
307
pub trait AsciiStr for Sized ? {
278
308
/// Convert to a string.
279
309
fn as_str_ascii < ' a > ( & ' a self ) -> & ' a str ;
@@ -283,19 +313,23 @@ pub trait AsciiStr for Sized? {
283
313
fn to_lower ( & self ) -> Vec < Ascii > ;
284
314
285
315
/// Convert to vector representing a lower cased ascii string.
316
+ #[ deprecated = "use iterators instead" ]
286
317
fn to_lowercase ( & self ) -> Vec < Ascii > ;
287
318
288
319
/// Deprecated: use `to_uppercase`
289
320
#[ deprecated="renamed `to_uppercase`" ]
290
321
fn to_upper ( & self ) -> Vec < Ascii > ;
291
322
292
323
/// Convert to vector representing a upper cased ascii string.
324
+ #[ deprecated = "use iterators instead" ]
293
325
fn to_uppercase ( & self ) -> Vec < Ascii > ;
294
326
295
327
/// Compares two Ascii strings ignoring case.
328
+ #[ deprecated = "use iterators instead" ]
296
329
fn eq_ignore_case ( & self , other : & [ Ascii ] ) -> bool ;
297
330
}
298
331
332
+ #[ experimental = "may be replaced by generic conversion traits" ]
299
333
impl AsciiStr for [ Ascii ] {
300
334
#[ inline]
301
335
fn as_str_ascii < ' a > ( & ' a self ) -> & ' a str {
@@ -338,11 +372,13 @@ impl IntoString for Vec<Ascii> {
338
372
}
339
373
340
374
/// Trait to convert to an owned byte vector by consuming self
375
+ #[ experimental = "may be replaced by generic conversion traits" ]
341
376
pub trait IntoBytes {
342
377
/// Converts to an owned byte vector by consuming self
343
378
fn into_bytes ( self ) -> Vec < u8 > ;
344
379
}
345
380
381
+ #[ experimental = "may be replaced by generic conversion traits" ]
346
382
impl IntoBytes for Vec < Ascii > {
347
383
fn into_bytes ( self ) -> Vec < u8 > {
348
384
unsafe {
@@ -360,6 +396,7 @@ impl IntoBytes for Vec<Ascii> {
360
396
361
397
362
398
/// Extension methods for ASCII-subset only operations on owned strings
399
+ #[ experimental = "would prefer to do this in a more general way" ]
363
400
pub trait OwnedAsciiExt {
364
401
/// Convert the string to ASCII upper case:
365
402
/// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
@@ -373,6 +410,7 @@ pub trait OwnedAsciiExt {
373
410
}
374
411
375
412
/// Extension methods for ASCII-subset only operations on string slices
413
+ #[ experimental = "would prefer to do this in a more general way" ]
376
414
pub trait AsciiExt < T > for Sized ? {
377
415
/// Makes a copy of the string in ASCII upper case:
378
416
/// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
@@ -390,6 +428,7 @@ pub trait AsciiExt<T> for Sized? {
390
428
fn eq_ignore_ascii_case ( & self , other : & Self ) -> bool ;
391
429
}
392
430
431
+ #[ experimental = "would prefer to do this in a more general way" ]
393
432
impl AsciiExt < String > for str {
394
433
#[ inline]
395
434
fn to_ascii_upper ( & self ) -> String {
@@ -409,6 +448,7 @@ impl AsciiExt<String> for str {
409
448
}
410
449
}
411
450
451
+ #[ experimental = "would prefer to do this in a more general way" ]
412
452
impl OwnedAsciiExt for String {
413
453
#[ inline]
414
454
fn into_ascii_upper ( self ) -> String {
@@ -423,6 +463,7 @@ impl OwnedAsciiExt for String {
423
463
}
424
464
}
425
465
466
+ #[ experimental = "would prefer to do this in a more general way" ]
426
467
impl AsciiExt < Vec < u8 > > for [ u8 ] {
427
468
#[ inline]
428
469
fn to_ascii_upper ( & self ) -> Vec < u8 > {
@@ -445,6 +486,7 @@ impl AsciiExt<Vec<u8>> for [u8] {
445
486
}
446
487
}
447
488
489
+ #[ experimental = "would prefer to do this in a more general way" ]
448
490
impl OwnedAsciiExt for Vec < u8 > {
449
491
#[ inline]
450
492
fn into_ascii_upper ( mut self ) -> Vec < u8 > {
@@ -474,6 +516,7 @@ impl OwnedAsciiExt for Vec<u8> {
474
516
/// - Any other chars in the range [0x20,0x7e] are not escaped.
475
517
/// - Any other chars are given hex escapes.
476
518
/// - Unicode escapes are never generated by this function.
519
+ #[ unstable = "needs to be updated to use an iterator" ]
477
520
pub fn escape_default ( c : u8 , f: |u8|) {
478
521
match c {
479
522
b'\t' => { f ( b'\\' ) ; f ( b't' ) ; }
@@ -496,7 +539,7 @@ pub fn escape_default(c: u8, f: |u8|) {
496
539
}
497
540
}
498
541
499
- pub static ASCII_LOWER_MAP : [ u8 , ..256 ] = [
542
+ static ASCII_LOWER_MAP : [ u8 , ..256 ] = [
500
543
0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 ,
501
544
0x08 , 0x09 , 0x0a , 0x0b , 0x0c , 0x0d , 0x0e , 0x0f ,
502
545
0x10 , 0x11 , 0x12 , 0x13 , 0x14 , 0x15 , 0x16 , 0x17 ,
@@ -535,7 +578,7 @@ pub static ASCII_LOWER_MAP: [u8, ..256] = [
535
578
0xf8 , 0xf9 , 0xfa , 0xfb , 0xfc , 0xfd , 0xfe , 0xff ,
536
579
] ;
537
580
538
- pub static ASCII_UPPER_MAP : [ u8 , ..256 ] = [
581
+ static ASCII_UPPER_MAP : [ u8 , ..256 ] = [
539
582
0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 ,
540
583
0x08 , 0x09 , 0x0a , 0x0b , 0x0c , 0x0d , 0x0e , 0x0f ,
541
584
0x10 , 0x11 , 0x12 , 0x13 , 0x14 , 0x15 , 0x16 , 0x17 ,
0 commit comments