# HG changeset patch # User Rik # Date 1338338162 25200 # Node ID a08f6e17336ef7a0316f9082cdf1dda65441bc44 # Parent c2411bff11c6339a3660e7102ed468140cfc7867 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. diff -r c2411bff11c6 -r a08f6e17336e src/DLD-FUNCTIONS/str2double.cc --- a/src/DLD-FUNCTIONS/str2double.cc Mon May 28 09:40:03 2012 -0700 +++ b/src/DLD-FUNCTIONS/str2double.cc Tue May 29 17:36:02 2012 -0700 @@ -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))