changeset 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
parents c2411bff11c6
children 52c5fb67fa5f 6d888db04e55 3b40dbc14572
files src/DLD-FUNCTIONS/str2double.cc
diffstat 1 files changed, 32 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- 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))