Mercurial > hg > octave-jordi
diff src/DLD-FUNCTIONS/str2double.cc @ 14702:a08f6e17336e
str2double.cc: Case-insensitive detection of 'Inf' (Bug #36536).
* str2double.cc (single_num): Use std::tolower, std::toupper to make
case-insenitive comparisons for 'Inf'.
* str2double.cc (extract_num): Expand code to test for imaginary unit or
'inf' when 'i' is seen in input stream.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Tue, 29 May 2012 17:36:02 -0700 (2012-05-30) |
parents | 60e5cf354d80 |
children | 5ae9f0f77635 |
line wrap: on
line diff
--- a/src/DLD-FUNCTIONS/str2double.cc +++ b/src/DLD-FUNCTIONS/str2double.cc @@ -54,15 +54,15 @@ c = is.peek (); } - if (c == 'I') + if (std::toupper (c) == 'I') { // It's infinity. is.get (); char c1 = is.get (), c2 = is.get (); - if (c1 == 'n' && c2 == 'f') + if (std::tolower (c1) == 'n' && std::tolower (c2) == 'f') { num = octave_Inf; - is.peek (); // May sets EOF bit. + is.peek (); // May set EOF bit. } else is.setstate (std::ios::failbit); // indicate that read has failed. @@ -127,13 +127,37 @@ c = is.peek (); } - // It's i*num or just i. - if (is_imag_unit (c)) + // Imaginary number (i*num or just i), or maybe 'inf'. + if (c == 'i') { - imag = true; + // possible infinity. is.get (); c = is.peek (); + if (is.eof ()) + { + // just 'i' and string is finished. Return immediately. + imag = true; + num = 1.0; + if (negative) + num = -num; + return is; + } + else + { + if (std::tolower (c) != 'n') + imag = true; + is.unget (); + } + } + else if (c == 'j') + imag = true; + + // It's i*num or just i + if (imag) + { + is.get (); + c = is.peek (); // Skip spaces after imaginary unit. while (isspace (c)) { @@ -369,8 +393,10 @@ %!assert (str2double ("NaN"), NaN) %!assert (str2double ("NA"), NA) %!assert (str2double ("Inf"), Inf) +%!assert (str2double ("iNF"), Inf) %!assert (str2double ("-Inf"), -Inf) %!assert (str2double ("Inf*i"), complex (0, Inf)) +%!assert (str2double ("iNF*i"), complex (0, Inf)) %!assert (str2double ("NaN + Inf*i"), complex (NaN, Inf)) %!assert (str2double ("Inf - Inf*i"), complex (Inf, -Inf)) %!assert (str2double ("-i*NaN - Inf"), complex (-Inf, -NaN))