File tree Expand file tree Collapse file tree 5 files changed +40
-10
lines changed
Expand file tree Collapse file tree 5 files changed +40
-10
lines changed Original file line number Diff line number Diff line change @@ -93,3 +93,12 @@ pure fn is_whitespace(c: char) -> bool {
9393 true
9494 } else if c == ch_no_break_space { true } else { false }
9595}
96+
97+ pure fn to_digit ( c : char ) -> u8 {
98+ alt c {
99+ '0' to ' 9 ' { c as u8 - ( '0' as u8 ) }
100+ 'a' to 'z' { c as u8 + 10u8 - ( 'a' as u8 ) }
101+ 'A' to 'Z' { c as u8 + 10u8 - ( 'A' as u8 ) }
102+ _ { fail; }
103+ }
104+ }
Original file line number Diff line number Diff line change @@ -112,11 +112,7 @@ fn parse_buf(buf: [u8], radix: uint) -> int {
112112 }
113113 let n = 0 ;
114114 while true {
115- let digit = alt buf[ i] as char {
116- '0' to '9' { buf[ i] - ( '0' as u8 ) }
117- 'a' to 'z' { 10u8 + buf[ i] - ( 'a' as u8 ) }
118- 'A' to 'Z' { 10u8 + buf[ i] - ( 'A' as u8 ) }
119- } ;
115+ let digit = char:: to_digit ( buf[ i] as char ) ;
120116 if ( digit as uint ) >= radix {
121117 fail;
122118 }
Original file line number Diff line number Diff line change @@ -100,11 +100,7 @@ fn parse_buf(buf: [u8], radix: uint) -> uint {
100100 let power = 1 u;
101101 let n = 0 u;
102102 while true {
103- let digit = alt buf[ i] as char {
104- '0' to '9' { buf[ i] - ( '0' as u8 ) }
105- 'a' to 'z' { 10u8 + buf[ i] - ( 'a' as u8 ) }
106- 'A' to 'Z' { 10u8 + buf[ i] - ( 'A' as u8 ) }
107- } ;
103+ let digit = char:: to_digit ( buf[ i] as char ) ;
108104 if ( digit as uint ) >= radix {
109105 fail;
110106 }
Original file line number Diff line number Diff line change 1+ use std;
2+ import std:: char;
3+
4+ #[ test]
5+ fn test_is_whitespace ( ) {
6+ assert char:: is_whitespace ( ' ' ) ;
7+ assert char:: is_whitespace ( '\u2007' ) ;
8+ assert char:: is_whitespace ( '\t' ) ;
9+ assert char:: is_whitespace ( '\n' ) ;
10+
11+ assert !char:: is_whitespace ( 'a' ) ;
12+ assert !char:: is_whitespace ( '_' ) ;
13+ assert !char:: is_whitespace ( '\u0000' ) ;
14+ }
15+
16+ #[ test]
17+ fn test_to_digit ( ) {
18+ assert ( char:: to_digit ( '0' ) == 0u8 ) ;
19+ assert ( char:: to_digit ( '1' ) == 1u8 ) ;
20+ assert ( char:: to_digit ( '2' ) == 2u8 ) ;
21+ assert ( char:: to_digit ( '9' ) == 9u8 ) ;
22+ assert ( char:: to_digit ( 'a' ) == 10u8 ) ;
23+ assert ( char:: to_digit ( 'A' ) == 10u8 ) ;
24+ assert ( char:: to_digit ( 'b' ) == 11u8 ) ;
25+ assert ( char:: to_digit ( 'B' ) == 11u8 ) ;
26+ assert ( char:: to_digit ( 'z' ) == 35u8 ) ;
27+ assert ( char:: to_digit ( 'Z' ) == 35u8 ) ;
28+ }
Original file line number Diff line number Diff line change 22
33mod bitv;
44mod box;
5+ mod char;
56mod comm;
67mod deque;
78mod either;
You can’t perform that action at this time.
0 commit comments