@@ -210,6 +210,14 @@ impl fmt::Display for ParseFloatError {
210210 }
211211}
212212
213+ fn str_to_ascii_lower_eq_str ( a : & str , b : & str ) -> bool {
214+ a. len ( ) == b. len ( )
215+ && a. bytes ( ) . zip ( b. bytes ( ) ) . all ( |( a, b) | {
216+ let a_to_ascii_lower = a | ( ( ( b'A' <= a && a <= b'Z' ) as u8 ) << 5 ) ;
217+ a_to_ascii_lower == b
218+ } )
219+ }
220+
213221// FIXME: The standard library from_str_radix on floats was deprecated, so we're stuck
214222// with this implementation ourselves until we want to make a breaking change.
215223// (would have to drop it from `Num` though)
@@ -232,12 +240,18 @@ macro_rules! float_trait_impl {
232240 }
233241
234242 // Special values
235- match src {
236- "inf" => return Ok ( core:: $t:: INFINITY ) ,
237- "-inf" => return Ok ( core:: $t:: NEG_INFINITY ) ,
238- "NaN" => return Ok ( core:: $t:: NAN ) ,
239- "-NaN" => return Ok ( -core:: $t:: NAN ) ,
240- _ => { } ,
243+ if str_to_ascii_lower_eq_str( src, "inf" )
244+ || str_to_ascii_lower_eq_str( src, "infinity" )
245+ {
246+ return Ok ( core:: $t:: INFINITY ) ;
247+ } else if str_to_ascii_lower_eq_str( src, "-inf" )
248+ || str_to_ascii_lower_eq_str( src, "-infinity" )
249+ {
250+ return Ok ( core:: $t:: NEG_INFINITY ) ;
251+ } else if str_to_ascii_lower_eq_str( src, "nan" ) {
252+ return Ok ( core:: $t:: NAN ) ;
253+ } else if str_to_ascii_lower_eq_str( src, "-nan" ) {
254+ return Ok ( -core:: $t:: NAN ) ;
241255 }
242256
243257 fn slice_shift_char( src: & str ) -> Option <( char , & str ) > {
@@ -516,6 +530,28 @@ fn from_str_radix_multi_byte_fail() {
516530 assert ! ( f32 :: from_str_radix( "0.2E™1" , 10 ) . is_err( ) ) ;
517531}
518532
533+ #[ test]
534+ fn from_str_radix_ignore_case ( ) {
535+ assert_eq ! (
536+ f32 :: from_str_radix( "InF" , 16 ) . unwrap( ) ,
537+ :: core:: f32 :: INFINITY
538+ ) ;
539+ assert_eq ! (
540+ f32 :: from_str_radix( "InfinitY" , 16 ) . unwrap( ) ,
541+ :: core:: f32 :: INFINITY
542+ ) ;
543+ assert_eq ! (
544+ f32 :: from_str_radix( "-InF" , 8 ) . unwrap( ) ,
545+ :: core:: f32 :: NEG_INFINITY
546+ ) ;
547+ assert_eq ! (
548+ f32 :: from_str_radix( "-InfinitY" , 8 ) . unwrap( ) ,
549+ :: core:: f32 :: NEG_INFINITY
550+ ) ;
551+ assert ! ( f32 :: from_str_radix( "nAn" , 4 ) . unwrap( ) . is_nan( ) ) ;
552+ assert ! ( f32 :: from_str_radix( "-nAn" , 4 ) . unwrap( ) . is_nan( ) ) ;
553+ }
554+
519555#[ test]
520556fn wrapping_is_num ( ) {
521557 fn require_num < T : Num > ( _: & T ) { }
0 commit comments