12
12
13
13
//! Operations on ASCII strings and characters
14
14
15
- #![ experimental]
15
+ #![ unstable = "unsure about placement and naming" ]
16
+ #![ allow( deprecated) ]
16
17
17
18
use core:: kinds:: Sized ;
18
19
use fmt;
@@ -31,30 +32,47 @@ pub struct Ascii { chr: u8 }
31
32
impl Ascii {
32
33
/// Converts an ascii character into a `u8`.
33
34
#[ inline]
34
- pub fn to_byte ( self ) -> u8 {
35
+ #[ unstable = "recently renamed" ]
36
+ pub fn as_byte ( & self ) -> u8 {
35
37
self . chr
36
38
}
37
39
40
+ /// Deprecated: use `as_byte` isntead.
41
+ #[ deprecated = "use as_byte" ]
42
+ pub fn to_byte ( self ) -> u8 {
43
+ self . as_byte ( )
44
+ }
45
+
38
46
/// Converts an ascii character into a `char`.
39
47
#[ inline]
40
- pub fn to_char ( self ) -> char {
48
+ #[ unstable = "recently renamed" ]
49
+ pub fn as_char ( & self ) -> char {
41
50
self . chr as char
42
51
}
43
52
53
+ /// Deprecated: use `as_char` isntead.
54
+ #[ deprecated = "use as_char" ]
55
+ pub fn to_char ( self ) -> char {
56
+ self . as_char ( )
57
+ }
58
+
44
59
/// Convert to lowercase.
45
60
#[ inline]
46
- pub fn to_lowercase ( self ) -> Ascii {
61
+ #[ stable]
62
+ pub fn to_lowercase ( & self ) -> Ascii {
47
63
Ascii { chr : ASCII_LOWER_MAP [ self . chr as uint ] }
48
64
}
49
65
50
66
/// Convert to uppercase.
51
67
#[ inline]
52
- pub fn to_uppercase ( self ) -> Ascii {
68
+ #[ stable]
69
+ pub fn to_uppercase ( & self ) -> Ascii {
53
70
Ascii { chr : ASCII_UPPER_MAP [ self . chr as uint ] }
54
71
}
55
72
56
73
/// Compares two ascii characters of equality, ignoring case.
57
74
#[ inline]
75
+ #[ deprecated = "normalize with to_lowercase" ]
58
76
pub fn eq_ignore_case ( self , other : Ascii ) -> bool {
59
77
ASCII_LOWER_MAP [ self . chr as uint ] == ASCII_LOWER_MAP [ other. chr as uint ]
60
78
}
@@ -63,66 +81,77 @@ impl Ascii {
63
81
64
82
/// Check if the character is a letter (a-z, A-Z)
65
83
#[ inline]
84
+ #[ stable]
66
85
pub fn is_alphabetic ( & self ) -> bool {
67
86
( self . chr >= 0x41 && self . chr <= 0x5A ) || ( self . chr >= 0x61 && self . chr <= 0x7A )
68
87
}
69
88
70
89
/// Check if the character is a number (0-9)
71
90
#[ inline]
91
+ #[ unstable = "may be renamed" ]
72
92
pub fn is_digit ( & self ) -> bool {
73
93
self . chr >= 0x30 && self . chr <= 0x39
74
94
}
75
95
76
96
/// Check if the character is a letter or number
77
97
#[ inline]
98
+ #[ stable]
78
99
pub fn is_alphanumeric ( & self ) -> bool {
79
100
self . is_alphabetic ( ) || self . is_digit ( )
80
101
}
81
102
82
103
/// Check if the character is a space or horizontal tab
83
104
#[ inline]
105
+ #[ experimental = "likely to be removed" ]
84
106
pub fn is_blank ( & self ) -> bool {
85
107
self . chr == b' ' || self . chr == b'\t'
86
108
}
87
109
88
110
/// Check if the character is a control character
89
111
#[ inline]
112
+ #[ stable]
90
113
pub fn is_control ( & self ) -> bool {
91
114
self . chr < 0x20 || self . chr == 0x7F
92
115
}
93
116
94
117
/// Checks if the character is printable (except space)
95
118
#[ inline]
119
+ #[ experimental = "unsure about naming, or whether this is needed" ]
96
120
pub fn is_graph ( & self ) -> bool {
97
121
( self . chr - 0x21 ) < 0x5E
98
122
}
99
123
100
124
/// Checks if the character is printable (including space)
101
125
#[ inline]
126
+ #[ unstable = "unsure about naming" ]
102
127
pub fn is_print ( & self ) -> bool {
103
128
( self . chr - 0x20 ) < 0x5F
104
129
}
105
130
106
- /// Checks if the character is lowercase
131
+ /// Checks if the character is alphabetic and lowercase
107
132
#[ inline]
133
+ #[ stable]
108
134
pub fn is_lowercase ( & self ) -> bool {
109
135
( self . chr - b'a' ) < 26
110
136
}
111
137
112
- /// Checks if the character is uppercase
138
+ /// Checks if the character is alphabetic and uppercase
113
139
#[ inline]
140
+ #[ stable]
114
141
pub fn is_uppercase ( & self ) -> bool {
115
142
( self . chr - b'A' ) < 26
116
143
}
117
144
118
145
/// Checks if the character is punctuation
119
146
#[ inline]
147
+ #[ stable]
120
148
pub fn is_punctuation ( & self ) -> bool {
121
149
self . is_graph ( ) && !self . is_alphanumeric ( )
122
150
}
123
151
124
152
/// Checks if the character is a valid hex digit
125
153
#[ inline]
154
+ #[ stable]
126
155
pub fn is_hex ( & self ) -> bool {
127
156
self . is_digit ( ) || ( ( self . chr | 32u8 ) - b'a' ) < 6
128
157
}
@@ -135,6 +164,7 @@ impl<'a> fmt::Show for Ascii {
135
164
}
136
165
137
166
/// Trait for converting into an ascii type.
167
+ #[ experimental = "may be replaced by generic conversion traits" ]
138
168
pub trait AsciiCast < T > {
139
169
/// Convert to an ascii type, panic on non-ASCII input.
140
170
#[ inline]
@@ -160,6 +190,7 @@ pub trait AsciiCast<T> {
160
190
fn is_ascii ( & self ) -> bool ;
161
191
}
162
192
193
+ #[ experimental = "may be replaced by generic conversion traits" ]
163
194
impl < ' a > AsciiCast < & ' a [ Ascii ] > for & ' a [ u8 ] {
164
195
#[ inline]
165
196
unsafe fn to_ascii_nocheck ( & self ) -> & ' a [ Ascii ] {
@@ -175,6 +206,7 @@ impl<'a> AsciiCast<&'a[Ascii]> for &'a [u8] {
175
206
}
176
207
}
177
208
209
+ #[ experimental = "may be replaced by generic conversion traits" ]
178
210
impl < ' a > AsciiCast < & ' a [ Ascii ] > for & ' a str {
179
211
#[ inline]
180
212
unsafe fn to_ascii_nocheck ( & self ) -> & ' a [ Ascii ] {
@@ -187,6 +219,7 @@ impl<'a> AsciiCast<&'a [Ascii]> for &'a str {
187
219
}
188
220
}
189
221
222
+ #[ experimental = "may be replaced by generic conversion traits" ]
190
223
impl AsciiCast < Ascii > for u8 {
191
224
#[ inline]
192
225
unsafe fn to_ascii_nocheck ( & self ) -> Ascii {
@@ -199,6 +232,7 @@ impl AsciiCast<Ascii> for u8 {
199
232
}
200
233
}
201
234
235
+ #[ experimental = "may be replaced by generic conversion traits" ]
202
236
impl AsciiCast < Ascii > for char {
203
237
#[ inline]
204
238
unsafe fn to_ascii_nocheck ( & self ) -> Ascii {
@@ -212,6 +246,7 @@ impl AsciiCast<Ascii> for char {
212
246
}
213
247
214
248
/// Trait for copyless casting to an ascii vector.
249
+ #[ experimental = "may be replaced by generic conversion traits" ]
215
250
pub trait OwnedAsciiCast {
216
251
/// Check if convertible to ascii
217
252
fn is_ascii ( & self ) -> bool ;
@@ -241,6 +276,7 @@ pub trait OwnedAsciiCast {
241
276
unsafe fn into_ascii_nocheck ( self ) -> Vec < Ascii > ;
242
277
}
243
278
279
+ #[ experimental = "may be replaced by generic conversion traits" ]
244
280
impl OwnedAsciiCast for String {
245
281
#[ inline]
246
282
fn is_ascii ( & self ) -> bool {
@@ -253,6 +289,7 @@ impl OwnedAsciiCast for String {
253
289
}
254
290
}
255
291
292
+ #[ experimental = "may be replaced by generic conversion traits" ]
256
293
impl OwnedAsciiCast for Vec < u8 > {
257
294
#[ inline]
258
295
fn is_ascii ( & self ) -> bool {
@@ -274,6 +311,7 @@ impl OwnedAsciiCast for Vec<u8> {
274
311
275
312
/// Trait for converting an ascii type to a string. Needed to convert
276
313
/// `&[Ascii]` to `&str`.
314
+ #[ experimental = "may be replaced by generic conversion traits" ]
277
315
pub trait AsciiStr for Sized ? {
278
316
/// Convert to a string.
279
317
fn as_str_ascii < ' a > ( & ' a self ) -> & ' a str ;
@@ -283,19 +321,23 @@ pub trait AsciiStr for Sized? {
283
321
fn to_lower ( & self ) -> Vec < Ascii > ;
284
322
285
323
/// Convert to vector representing a lower cased ascii string.
324
+ #[ deprecated = "use iterators instead" ]
286
325
fn to_lowercase ( & self ) -> Vec < Ascii > ;
287
326
288
327
/// Deprecated: use `to_uppercase`
289
328
#[ deprecated="renamed `to_uppercase`" ]
290
329
fn to_upper ( & self ) -> Vec < Ascii > ;
291
330
292
331
/// Convert to vector representing a upper cased ascii string.
332
+ #[ deprecated = "use iterators instead" ]
293
333
fn to_uppercase ( & self ) -> Vec < Ascii > ;
294
334
295
335
/// Compares two Ascii strings ignoring case.
336
+ #[ deprecated = "use iterators instead" ]
296
337
fn eq_ignore_case ( & self , other : & [ Ascii ] ) -> bool ;
297
338
}
298
339
340
+ #[ experimental = "may be replaced by generic conversion traits" ]
299
341
impl AsciiStr for [ Ascii ] {
300
342
#[ inline]
301
343
fn as_str_ascii < ' a > ( & ' a self ) -> & ' a str {
@@ -336,11 +378,13 @@ impl IntoString for Vec<Ascii> {
336
378
}
337
379
338
380
/// Trait to convert to an owned byte vector by consuming self
381
+ #[ experimental = "may be replaced by generic conversion traits" ]
339
382
pub trait IntoBytes {
340
383
/// Converts to an owned byte vector by consuming self
341
384
fn into_bytes ( self ) -> Vec < u8 > ;
342
385
}
343
386
387
+ #[ experimental = "may be replaced by generic conversion traits" ]
344
388
impl IntoBytes for Vec < Ascii > {
345
389
fn into_bytes ( self ) -> Vec < u8 > {
346
390
unsafe {
@@ -358,6 +402,7 @@ impl IntoBytes for Vec<Ascii> {
358
402
359
403
360
404
/// Extension methods for ASCII-subset only operations on owned strings
405
+ #[ experimental = "would prefer to do this in a more general way" ]
361
406
pub trait OwnedAsciiExt {
362
407
/// Convert the string to ASCII upper case:
363
408
/// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
@@ -371,6 +416,7 @@ pub trait OwnedAsciiExt {
371
416
}
372
417
373
418
/// Extension methods for ASCII-subset only operations on string slices
419
+ #[ experimental = "would prefer to do this in a more general way" ]
374
420
pub trait AsciiExt < T > for Sized ? {
375
421
/// Makes a copy of the string in ASCII upper case:
376
422
/// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
@@ -388,6 +434,7 @@ pub trait AsciiExt<T> for Sized? {
388
434
fn eq_ignore_ascii_case ( & self , other : & Self ) -> bool ;
389
435
}
390
436
437
+ #[ experimental = "would prefer to do this in a more general way" ]
391
438
impl AsciiExt < String > for str {
392
439
#[ inline]
393
440
fn to_ascii_upper ( & self ) -> String {
@@ -407,6 +454,7 @@ impl AsciiExt<String> for str {
407
454
}
408
455
}
409
456
457
+ #[ experimental = "would prefer to do this in a more general way" ]
410
458
impl OwnedAsciiExt for String {
411
459
#[ inline]
412
460
fn into_ascii_upper ( self ) -> String {
@@ -421,6 +469,7 @@ impl OwnedAsciiExt for String {
421
469
}
422
470
}
423
471
472
+ #[ experimental = "would prefer to do this in a more general way" ]
424
473
impl AsciiExt < Vec < u8 > > for [ u8 ] {
425
474
#[ inline]
426
475
fn to_ascii_upper ( & self ) -> Vec < u8 > {
@@ -443,6 +492,7 @@ impl AsciiExt<Vec<u8>> for [u8] {
443
492
}
444
493
}
445
494
495
+ #[ experimental = "would prefer to do this in a more general way" ]
446
496
impl OwnedAsciiExt for Vec < u8 > {
447
497
#[ inline]
448
498
fn into_ascii_upper ( mut self ) -> Vec < u8 > {
@@ -472,6 +522,7 @@ impl OwnedAsciiExt for Vec<u8> {
472
522
/// - Any other chars in the range [0x20,0x7e] are not escaped.
473
523
/// - Any other chars are given hex escapes.
474
524
/// - Unicode escapes are never generated by this function.
525
+ #[ unstable = "needs to be updated to use an iterator" ]
475
526
pub fn escape_default ( c : u8 , f: |u8|) {
476
527
match c {
477
528
b'\t' => { f ( b'\\' ) ; f ( b't' ) ; }
@@ -494,7 +545,7 @@ pub fn escape_default(c: u8, f: |u8|) {
494
545
}
495
546
}
496
547
497
- pub static ASCII_LOWER_MAP : [ u8 , ..256 ] = [
548
+ static ASCII_LOWER_MAP : [ u8 , ..256 ] = [
498
549
0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 ,
499
550
0x08 , 0x09 , 0x0a , 0x0b , 0x0c , 0x0d , 0x0e , 0x0f ,
500
551
0x10 , 0x11 , 0x12 , 0x13 , 0x14 , 0x15 , 0x16 , 0x17 ,
@@ -533,7 +584,7 @@ pub static ASCII_LOWER_MAP: [u8, ..256] = [
533
584
0xf8 , 0xf9 , 0xfa , 0xfb , 0xfc , 0xfd , 0xfe , 0xff ,
534
585
] ;
535
586
536
- pub static ASCII_UPPER_MAP : [ u8 , ..256 ] = [
587
+ static ASCII_UPPER_MAP : [ u8 , ..256 ] = [
537
588
0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 ,
538
589
0x08 , 0x09 , 0x0a , 0x0b , 0x0c , 0x0d , 0x0e , 0x0f ,
539
590
0x10 , 0x11 , 0x12 , 0x13 , 0x14 , 0x15 , 0x16 , 0x17 ,
0 commit comments