14
14
15
15
macro_rules! int_module ( ( $T: ty) => (
16
16
17
- // String conversion functions and impl str -> num
18
-
19
- /// Parse a byte slice as a number in the given base
20
- ///
21
- /// Yields an `Option` because `buf` may or may not actually be parseable.
22
- ///
23
- /// # Examples
24
- ///
25
- /// ```
26
- /// let num = std::i64::parse_bytes([49,50,51,52,53,54,55,56,57], 10);
27
- /// assert!(num == Some(123456789));
28
- /// ```
29
- #[ inline]
30
- #[ experimental = "might need to return Result" ]
31
- pub fn parse_bytes( buf: & [ u8 ] , radix: uint) -> Option <$T> {
32
- strconv:: from_str_bytes_common( buf, radix, true , false , false ,
33
- strconv:: ExpNone , false , false )
34
- }
35
-
36
17
#[ experimental = "might need to return Result" ]
37
18
impl FromStr for $T {
38
19
#[ inline]
39
20
fn from_str( s: & str ) -> Option <$T> {
40
- strconv:: from_str_common( s, 10 u, true , false , false ,
41
- strconv:: ExpNone , false , false )
21
+ strconv:: from_str_radix_int( s, 10 )
42
22
}
43
23
}
44
24
45
25
#[ experimental = "might need to return Result" ]
46
26
impl FromStrRadix for $T {
47
27
#[ inline]
48
28
fn from_str_radix( s: & str , radix: uint) -> Option <$T> {
49
- strconv:: from_str_common( s, radix, true , false , false ,
50
- strconv:: ExpNone , false , false )
29
+ strconv:: from_str_radix_int( s, radix)
51
30
}
52
31
}
53
32
54
33
#[ cfg( test) ]
55
34
mod tests {
56
35
use prelude:: * ;
57
- use super :: * ;
58
-
59
- use i32 ;
60
- use str :: StrSlice ;
36
+ use num:: FromStrRadix ;
61
37
62
38
#[ test]
63
39
fn test_from_str( ) {
@@ -73,33 +49,33 @@ mod tests {
73
49
assert_eq!( from_str:: <i32 >( "-123456789" ) , Some ( -123456789 as i32 ) ) ;
74
50
assert_eq!( from_str:: <$T>( "-00100" ) , Some ( -100 as $T) ) ;
75
51
76
- assert!( from_str:: <$T>( " " ) . is_none( ) ) ;
77
- assert!( from_str:: <$T>( "x" ) . is_none( ) ) ;
52
+ assert_eq!( from_str:: <$T>( "" ) , None ) ;
53
+ assert_eq!( from_str:: <$T>( " " ) , None ) ;
54
+ assert_eq!( from_str:: <$T>( "x" ) , None ) ;
78
55
}
79
56
80
57
#[ test]
81
- fn test_parse_bytes( ) {
82
- use str :: StrSlice ;
83
- assert_eq!( parse_bytes( "123" . as_bytes( ) , 10 u) , Some ( 123 as $T) ) ;
84
- assert_eq!( parse_bytes( "1001" . as_bytes( ) , 2 u) , Some ( 9 as $T) ) ;
85
- assert_eq!( parse_bytes( "123" . as_bytes( ) , 8 u) , Some ( 83 as $T) ) ;
86
- assert_eq!( i32 :: parse_bytes( "123" . as_bytes( ) , 16 u) , Some ( 291 as i32 ) ) ;
87
- assert_eq!( i32 :: parse_bytes( "ffff" . as_bytes( ) , 16 u) , Some ( 65535 as i32 ) ) ;
88
- assert_eq!( i32 :: parse_bytes( "FFFF" . as_bytes( ) , 16 u) , Some ( 65535 as i32 ) ) ;
89
- assert_eq!( parse_bytes( "z" . as_bytes( ) , 36 u) , Some ( 35 as $T) ) ;
90
- assert_eq!( parse_bytes( "Z" . as_bytes( ) , 36 u) , Some ( 35 as $T) ) ;
91
-
92
- assert_eq!( parse_bytes( "-123" . as_bytes( ) , 10 u) , Some ( -123 as $T) ) ;
93
- assert_eq!( parse_bytes( "-1001" . as_bytes( ) , 2 u) , Some ( -9 as $T) ) ;
94
- assert_eq!( parse_bytes( "-123" . as_bytes( ) , 8 u) , Some ( -83 as $T) ) ;
95
- assert_eq!( i32 :: parse_bytes( "-123" . as_bytes( ) , 16 u) , Some ( -291 as i32 ) ) ;
96
- assert_eq!( i32 :: parse_bytes( "-ffff" . as_bytes( ) , 16 u) , Some ( -65535 as i32 ) ) ;
97
- assert_eq!( i32 :: parse_bytes( "-FFFF" . as_bytes( ) , 16 u) , Some ( -65535 as i32 ) ) ;
98
- assert_eq!( parse_bytes( "-z" . as_bytes( ) , 36 u) , Some ( -35 as $T) ) ;
99
- assert_eq!( parse_bytes( "-Z" . as_bytes( ) , 36 u) , Some ( -35 as $T) ) ;
100
-
101
- assert!( parse_bytes( "Z" . as_bytes( ) , 35 u) . is_none( ) ) ;
102
- assert!( parse_bytes( "-9" . as_bytes( ) , 2 u) . is_none( ) ) ;
58
+ fn test_from_str_radix( ) {
59
+ assert_eq!( FromStrRadix :: from_str_radix( "123" , 10 ) , Some ( 123 as $T) ) ;
60
+ assert_eq!( FromStrRadix :: from_str_radix( "1001" , 2 ) , Some ( 9 as $T) ) ;
61
+ assert_eq!( FromStrRadix :: from_str_radix( "123" , 8 ) , Some ( 83 as $T) ) ;
62
+ assert_eq!( FromStrRadix :: from_str_radix( "123" , 16 ) , Some ( 291 as i32 ) ) ;
63
+ assert_eq!( FromStrRadix :: from_str_radix( "ffff" , 16 ) , Some ( 65535 as i32 ) ) ;
64
+ assert_eq!( FromStrRadix :: from_str_radix( "FFFF" , 16 ) , Some ( 65535 as i32 ) ) ;
65
+ assert_eq!( FromStrRadix :: from_str_radix( "z" , 36 ) , Some ( 35 as $T) ) ;
66
+ assert_eq!( FromStrRadix :: from_str_radix( "Z" , 36 ) , Some ( 35 as $T) ) ;
67
+
68
+ assert_eq!( FromStrRadix :: from_str_radix( "-123" , 10 ) , Some ( -123 as $T) ) ;
69
+ assert_eq!( FromStrRadix :: from_str_radix( "-1001" , 2 ) , Some ( -9 as $T) ) ;
70
+ assert_eq!( FromStrRadix :: from_str_radix( "-123" , 8 ) , Some ( -83 as $T) ) ;
71
+ assert_eq!( FromStrRadix :: from_str_radix( "-123" , 16 ) , Some ( -291 as i32 ) ) ;
72
+ assert_eq!( FromStrRadix :: from_str_radix( "-ffff" , 16 ) , Some ( -65535 as i32 ) ) ;
73
+ assert_eq!( FromStrRadix :: from_str_radix( "-FFFF" , 16 ) , Some ( -65535 as i32 ) ) ;
74
+ assert_eq!( FromStrRadix :: from_str_radix( "-z" , 36 ) , Some ( -35 as $T) ) ;
75
+ assert_eq!( FromStrRadix :: from_str_radix( "-Z" , 36 ) , Some ( -35 as $T) ) ;
76
+
77
+ assert_eq!( FromStrRadix :: from_str_radix( "Z" , 35 ) , None :: <$T>) ;
78
+ assert_eq!( FromStrRadix :: from_str_radix( "-9" , 2 ) , None :: <$T>) ;
103
79
}
104
80
105
81
#[ test]
@@ -133,35 +109,35 @@ mod tests {
133
109
fn test_int_from_str_overflow( ) {
134
110
let mut i8_val: i8 = 127_i8 ;
135
111
assert_eq!( from_str:: <i8 >( "127" ) , Some ( i8_val) ) ;
136
- assert !( from_str:: <i8 >( "128" ) . is_none ( ) ) ;
112
+ assert_eq !( from_str:: <i8 >( "128" ) , None ) ;
137
113
138
114
i8_val += 1 as i8 ;
139
115
assert_eq!( from_str:: <i8 >( "-128" ) , Some ( i8_val) ) ;
140
- assert !( from_str:: <i8 >( "-129" ) . is_none ( ) ) ;
116
+ assert_eq !( from_str:: <i8 >( "-129" ) , None ) ;
141
117
142
118
let mut i16_val: i16 = 32_767_i16 ;
143
119
assert_eq!( from_str:: <i16 >( "32767" ) , Some ( i16_val) ) ;
144
- assert !( from_str:: <i16 >( "32768" ) . is_none ( ) ) ;
120
+ assert_eq !( from_str:: <i16 >( "32768" ) , None ) ;
145
121
146
122
i16_val += 1 as i16 ;
147
123
assert_eq!( from_str:: <i16 >( "-32768" ) , Some ( i16_val) ) ;
148
- assert !( from_str:: <i16 >( "-32769" ) . is_none ( ) ) ;
124
+ assert_eq !( from_str:: <i16 >( "-32769" ) , None ) ;
149
125
150
126
let mut i32_val: i32 = 2_147_483_647_i32 ;
151
127
assert_eq!( from_str:: <i32 >( "2147483647" ) , Some ( i32_val) ) ;
152
- assert !( from_str:: <i32 >( "2147483648" ) . is_none ( ) ) ;
128
+ assert_eq !( from_str:: <i32 >( "2147483648" ) , None ) ;
153
129
154
130
i32_val += 1 as i32 ;
155
131
assert_eq!( from_str:: <i32 >( "-2147483648" ) , Some ( i32_val) ) ;
156
- assert !( from_str:: <i32 >( "-2147483649" ) . is_none ( ) ) ;
132
+ assert_eq !( from_str:: <i32 >( "-2147483649" ) , None ) ;
157
133
158
134
let mut i64_val: i64 = 9_223_372_036_854_775_807_i64 ;
159
135
assert_eq!( from_str:: <i64 >( "9223372036854775807" ) , Some ( i64_val) ) ;
160
- assert !( from_str:: <i64 >( "9223372036854775808" ) . is_none ( ) ) ;
136
+ assert_eq !( from_str:: <i64 >( "9223372036854775808" ) , None ) ;
161
137
162
138
i64_val += 1 as i64 ;
163
139
assert_eq!( from_str:: <i64 >( "-9223372036854775808" ) , Some ( i64_val) ) ;
164
- assert !( from_str:: <i64 >( "-9223372036854775809" ) . is_none ( ) ) ;
140
+ assert_eq !( from_str:: <i64 >( "-9223372036854775809" ) , None ) ;
165
141
}
166
142
}
167
143
0 commit comments