@@ -110,6 +110,7 @@ This function accepts strings such as
110
110
* '', or, equivalently, '.' (understood as 0)
111
111
* '5.'
112
112
* '.5', or, equivalently, '0.5'
113
+ * 'inf', '-inf', 'NaN'
113
114
114
115
Leading and trailing whitespace are ignored.
115
116
@@ -123,6 +124,14 @@ Leading and trailing whitespace are ignored.
123
124
where `n` is the floating-point number represented by `[num]`.
124
125
" ]
125
126
fn from_str ( num : str ) -> option < float > {
127
+ if num == "inf" {
128
+ ret some ( infinity) ;
129
+ } else if num == "-inf" {
130
+ ret some ( neg_infinity) ;
131
+ } else if num == "NaN" {
132
+ ret some ( NaN ) ;
133
+ }
134
+
126
135
let mut pos = 0 u; //Current byte position in the string.
127
136
//Used to walk the string in O(n).
128
137
let len = str:: len ( num) ; //Length of the string, in bytes.
@@ -301,6 +310,15 @@ fn test_from_str() {
301
310
assert from_str ( "-.5" ) == some ( -0.5 ) ;
302
311
assert from_str ( "-.5" ) == some ( -0.5 ) ;
303
312
assert from_str ( "-5" ) == some ( -5. ) ;
313
+ assert from_str ( "-0" ) == some ( -0. ) ;
314
+ assert from_str ( "0" ) == some ( 0. ) ;
315
+ assert from_str ( "inf" ) == some ( infinity) ;
316
+ assert from_str ( "-inf" ) == some ( neg_infinity) ;
317
+ // note: NaN != NaN, hence this slightly complex test
318
+ alt from_str ( "NaN" ) {
319
+ some ( f) { assert is_NaN ( f) ; }
320
+ none { fail; }
321
+ }
304
322
305
323
assert from_str ( "" ) == none;
306
324
assert from_str ( "x" ) == none;
0 commit comments