@@ -100,10 +100,7 @@ pub fn from_u32(i: u32) -> Option<char> {
100
100
#[ inline]
101
101
#[ deprecated = "use the Char::is_digit method" ]
102
102
pub fn is_digit_radix ( c : char , radix : uint ) -> bool {
103
- match to_digit ( c, radix) {
104
- Some ( _) => true ,
105
- None => false ,
106
- }
103
+ c. is_digit ( radix)
107
104
}
108
105
109
106
///
@@ -123,17 +120,7 @@ pub fn is_digit_radix(c: char, radix: uint) -> bool {
123
120
#[ inline]
124
121
#[ deprecated = "use the Char::to_digit method" ]
125
122
pub fn to_digit ( c : char , radix : uint ) -> Option < uint > {
126
- if radix > 36 {
127
- panic ! ( "to_digit: radix is too high (maximum 36)" ) ;
128
- }
129
- let val = match c {
130
- '0' ... '9' => c as uint - ( '0' as uint ) ,
131
- 'a' ... 'z' => c as uint + 10 u - ( 'a' as uint ) ,
132
- 'A' ... 'Z' => c as uint + 10 u - ( 'A' as uint ) ,
133
- _ => return None ,
134
- } ;
135
- if val < radix { Some ( val) }
136
- else { None }
123
+ c. to_digit ( radix)
137
124
}
138
125
139
126
///
@@ -178,23 +165,7 @@ pub fn from_digit(num: uint, radix: uint) -> Option<char> {
178
165
///
179
166
#[ deprecated = "use the Char::escape_unicode method" ]
180
167
pub fn escape_unicode ( c : char , f: |char|) {
181
- // avoid calling str::to_str_radix because we don't really need to allocate
182
- // here.
183
- f ( '\\' ) ;
184
- let pad = match ( ) {
185
- _ if c <= '\x7f' => { f ( 'x' ) ; 2 }
186
- _ if c <= '\uffff' => { f ( 'u' ) ; 4 }
187
- _ => { f ( 'U' ) ; 8 }
188
- } ;
189
- for offset in range_step :: < i32 > ( 4 * ( pad - 1 ) , -1 , -4 ) {
190
- let offset = offset as uint ;
191
- unsafe {
192
- match ( ( c as i32 ) >> offset) & 0xf {
193
- i @ 0 ... 9 => { f ( transmute ( '0' as i32 + i) ) ; }
194
- i => { f ( transmute ( 'a' as i32 + ( i - 10 ) ) ) ; }
195
- }
196
- }
197
- }
168
+ c. escape_unicode ( f)
198
169
}
199
170
200
171
///
@@ -211,29 +182,14 @@ pub fn escape_unicode(c: char, f: |char|) {
211
182
///
212
183
#[ deprecated = "use the Char::escape_default method" ]
213
184
pub fn escape_default ( c : char , f: |char|) {
214
- match c {
215
- '\t' => { f ( '\\' ) ; f ( 't' ) ; }
216
- '\r' => { f ( '\\' ) ; f ( 'r' ) ; }
217
- '\n' => { f ( '\\' ) ; f ( 'n' ) ; }
218
- '\\' => { f ( '\\' ) ; f ( '\\' ) ; }
219
- '\'' => { f ( '\\' ) ; f ( '\'' ) ; }
220
- '"' => { f ( '\\' ) ; f ( '"' ) ; }
221
- '\x20' ... '\x7e' => { f ( c) ; }
222
- _ => c. escape_unicode ( f) ,
223
- }
185
+ c. escape_default ( f)
224
186
}
225
187
226
188
/// Returns the amount of bytes this `char` would need if encoded in UTF-8
227
189
#[ inline]
228
190
#[ deprecated = "use the Char::len_utf8 method" ]
229
191
pub fn len_utf8_bytes ( c : char ) -> uint {
230
- let code = c as u32 ;
231
- match ( ) {
232
- _ if code < MAX_ONE_B => 1 u,
233
- _ if code < MAX_TWO_B => 2 u,
234
- _ if code < MAX_THREE_B => 3 u,
235
- _ => 4 u,
236
- }
192
+ c. len_utf8 ( )
237
193
}
238
194
239
195
/// Basic `char` manipulations.
@@ -362,13 +318,30 @@ pub trait Char {
362
318
#[ experimental = "trait is experimental" ]
363
319
impl Char for char {
364
320
#[ deprecated = "use is_digit" ]
365
- fn is_digit_radix ( & self , radix : uint ) -> bool { is_digit_radix ( * self , radix) }
321
+ fn is_digit_radix ( & self , radix : uint ) -> bool { self . is_digit ( radix) }
366
322
367
323
#[ unstable = "pending trait organization" ]
368
- fn is_digit ( & self , radix : uint ) -> bool { is_digit_radix ( * self , radix) }
324
+ fn is_digit ( & self , radix : uint ) -> bool {
325
+ match self . to_digit ( radix) {
326
+ Some ( _) => true ,
327
+ None => false ,
328
+ }
329
+ }
369
330
370
331
#[ unstable = "pending trait organization" ]
371
- fn to_digit ( & self , radix : uint ) -> Option < uint > { to_digit ( * self , radix) }
332
+ fn to_digit ( & self , radix : uint ) -> Option < uint > {
333
+ if radix > 36 {
334
+ panic ! ( "to_digit: radix is too high (maximum 36)" ) ;
335
+ }
336
+ let val = match * self {
337
+ '0' ... '9' => * self as uint - ( '0' as uint ) ,
338
+ 'a' ... 'z' => * self as uint + 10 u - ( 'a' as uint ) ,
339
+ 'A' ... 'Z' => * self as uint + 10 u - ( 'A' as uint ) ,
340
+ _ => return None ,
341
+ } ;
342
+ if val < radix { Some ( val) }
343
+ else { None }
344
+ }
372
345
373
346
#[ deprecated = "use the char::from_digit free function" ]
374
347
fn from_digit ( num : uint , radix : uint ) -> Option < char > { from_digit ( num, radix) }
@@ -378,18 +351,55 @@ impl Char for char {
378
351
fn from_u32 ( i : u32 ) -> Option < char > { from_u32 ( i) }
379
352
380
353
#[ unstable = "pending error conventions, trait organization" ]
381
- fn escape_unicode ( & self , f: |char|) { escape_unicode ( * self , f) }
354
+ fn escape_unicode ( & self , f: |char|) {
355
+ // avoid calling str::to_str_radix because we don't really need to allocate
356
+ // here.
357
+ f ( '\\' ) ;
358
+ let pad = match ( ) {
359
+ _ if * self <= '\xff' => { f ( 'x' ) ; 2 }
360
+ _ if * self <= '\uffff' => { f ( 'u' ) ; 4 }
361
+ _ => { f ( 'U' ) ; 8 }
362
+ } ;
363
+ for offset in range_step :: < i32 > ( 4 * ( pad - 1 ) , -1 , -4 ) {
364
+ let offset = offset as uint ;
365
+ unsafe {
366
+ match ( ( * self as i32 ) >> offset) & 0xf {
367
+ i @ 0 ... 9 => { f ( transmute ( '0' as i32 + i) ) ; }
368
+ i => { f ( transmute ( 'a' as i32 + ( i - 10 ) ) ) ; }
369
+ }
370
+ }
371
+ }
372
+ }
382
373
383
374
#[ unstable = "pending error conventions, trait organization" ]
384
- fn escape_default ( & self , f: |char|) { escape_default ( * self , f) }
375
+ fn escape_default ( & self , f: |char|) {
376
+ match * self {
377
+ '\t' => { f ( '\\' ) ; f ( 't' ) ; }
378
+ '\r' => { f ( '\\' ) ; f ( 'r' ) ; }
379
+ '\n' => { f ( '\\' ) ; f ( 'n' ) ; }
380
+ '\\' => { f ( '\\' ) ; f ( '\\' ) ; }
381
+ '\'' => { f ( '\\' ) ; f ( '\'' ) ; }
382
+ '"' => { f ( '\\' ) ; f ( '"' ) ; }
383
+ '\x20' ... '\x7e' => { f ( * self ) ; }
384
+ _ => self . escape_unicode ( f) ,
385
+ }
386
+ }
385
387
386
388
#[ inline]
387
389
#[ deprecated = "use len_utf8" ]
388
- fn len_utf8_bytes ( & self ) -> uint { len_utf8_bytes ( * self ) }
390
+ fn len_utf8_bytes ( & self ) -> uint { self . len_utf8 ( ) }
389
391
390
392
#[ inline]
391
393
#[ unstable = "pending trait organization" ]
392
- fn len_utf8 ( & self ) -> uint { len_utf8_bytes ( * self ) }
394
+ fn len_utf8 ( & self ) -> uint {
395
+ let code = * self as u32 ;
396
+ match ( ) {
397
+ _ if code < MAX_ONE_B => 1 u,
398
+ _ if code < MAX_TWO_B => 2 u,
399
+ _ if code < MAX_THREE_B => 3 u,
400
+ _ => 4 u,
401
+ }
402
+ }
393
403
394
404
#[ inline]
395
405
#[ unstable = "pending trait organization" ]
0 commit comments