changeset 23318:d05f688ae836

dlmread: Add support for reading pure imaginary numbers (bug #50589). * dlmread.cc: Check for both 'i' and 'j' as possible comlex number indicators. If pure complex found, check if return data is already complex and if not, convert it. Add BIST test for bug #50589.
author Rik <rik@octave.org>
date Mon, 20 Mar 2017 15:32:08 -0700
parents ef94844a3c12
children f77d840f0f7c
files libinterp/corefcn/dlmread.cc
diffstat 1 files changed, 42 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/dlmread.cc	Mon Mar 20 20:48:02 2017 +0100
+++ b/libinterp/corefcn/dlmread.cc	Mon Mar 20 15:32:08 2017 -0700
@@ -426,28 +426,35 @@
                   else
                     rdata(i,j++) = x;
                 }
-              else if (std::toupper (tmp_stream.peek ()) == 'I')
-                {
-                  // This is to allow pure imaginary numbers.
-                  if (iscmplx)
-                    cdata(i,j++) = x;
-                  else
-                    rdata(i,j++) = x;
-                }
               else
                 {
-                  double y = octave_read_double (tmp_stream);
+                  int next_char = std::tolower (tmp_stream.peek ());
+                  if (next_char == 'i' || next_char == 'j')
+                    {
+                      // Process pure imaginary numbers.
+                      if (! iscmplx)
+                        {
+                          iscmplx = true;
+                          cdata = ComplexMatrix (rdata);
+                        }
 
-                  if (! iscmplx && y != 0.)
+                      cdata(i,j++) = Complex (0, x);
+                    }
+                  else
                     {
-                      iscmplx = true;
-                      cdata = ComplexMatrix (rdata);
-                    }
+                      double y = octave_read_double (tmp_stream);
 
-                  if (iscmplx)
-                    cdata(i,j++) = Complex (x, y);
-                  else
-                    rdata(i,j++) = x;
+                      if (! iscmplx && y != 0.)
+                        {
+                          iscmplx = true;
+                          cdata = ComplexMatrix (rdata);
+                        }
+
+                      if (iscmplx)
+                        cdata(i,j++) = Complex (x, y);
+                      else
+                        rdata(i,j++) = x;
+                    }
                 }
             }
           else if (iscmplx)
@@ -549,4 +556,22 @@
 %!   unlink (file);
 %! end_unwind_protect
 
+%!test <50589>
+%! file = tempname ();
+%! unwind_protect
+%!   fid = fopen (file, "wt");
+%!   fwrite (fid, "1;2;3\n");
+%!   fwrite (fid, "1i;2I;3j;4J\n");
+%!   fwrite (fid, "4;5;6\n");
+%!   fwrite (fid, "-4i;+5I;-6j;+7J\n");
+%!   fclose (fid);
+%!
+%!   assert (dlmread (file), [1, 2, 3, 0; 1i, 2i, 3i, 4i;
+%!                            4, 5, 6, 0; -4i, 5i, -6i, 7i]);
+%!   assert (dlmread (file, "", [0 0 0 3]), [1, 2, 3]);
+%!   assert (dlmread (file, "", [1 0 1 3]), [1i, 2i, 3i, 4i]);
+%! unwind_protect_cleanup
+%!   unlink (file);
+%! end_unwind_protect
+
 */