1414
1515macro_rules! int_module ( ( $T: ty) => (
1616
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-
3617#[ experimental = "might need to return Result" ]
3718impl FromStr for $T {
3819 #[ inline]
3920 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 )
4222 }
4323}
4424
4525#[ experimental = "might need to return Result" ]
4626impl FromStrRadix for $T {
4727 #[ inline]
4828 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)
5130 }
5231}
5332
5433#[ cfg( test) ]
5534mod tests {
5635 use prelude:: * ;
57- use super :: * ;
58-
59- use i32 ;
60- use str :: StrSlice ;
36+ use num:: FromStrRadix ;
6137
6238 #[ test]
6339 fn test_from_str( ) {
@@ -73,33 +49,33 @@ mod tests {
7349 assert_eq!( from_str:: <i32 >( "-123456789" ) , Some ( -123456789 as i32 ) ) ;
7450 assert_eq!( from_str:: <$T>( "-00100" ) , Some ( -100 as $T) ) ;
7551
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 ) ;
7855 }
7956
8057 #[ 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>) ;
10379 }
10480
10581 #[ test]
@@ -133,35 +109,35 @@ mod tests {
133109 fn test_int_from_str_overflow( ) {
134110 let mut i8_val: i8 = 127_i8 ;
135111 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 ) ;
137113
138114 i8_val += 1 as i8 ;
139115 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 ) ;
141117
142118 let mut i16_val: i16 = 32_767_i16 ;
143119 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 ) ;
145121
146122 i16_val += 1 as i16 ;
147123 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 ) ;
149125
150126 let mut i32_val: i32 = 2_147_483_647_i32 ;
151127 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 ) ;
153129
154130 i32_val += 1 as i32 ;
155131 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 ) ;
157133
158134 let mut i64_val: i64 = 9_223_372_036_854_775_807_i64 ;
159135 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 ) ;
161137
162138 i64_val += 1 as i64 ;
163139 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 ) ;
165141 }
166142}
167143
0 commit comments