changeset 14800:a922f768ee09

textscan, strread: improved ML compatibility (bug 36398) * textscan: check for empty data string after removal of trailing EOL * strread: check for incomplete very first data line while parsing + add 2 tests
author Philip Nienhuis <prnienhuis@users.sf.net>
date Fri, 22 Jun 2012 20:01:35 +0200
parents 21197c43f984
children 3c5e6971064c
files scripts/io/strread.m scripts/io/textscan.m
diffstat 2 files changed, 25 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/io/strread.m	Fri Jun 22 19:09:30 2012 +0200
+++ b/scripts/io/strread.m	Fri Jun 22 20:01:35 2012 +0200
@@ -459,7 +459,8 @@
       ## Alternative below goes by simply parsing a first grab of words
       ## and counting words until the fmt_words array is exhausted:
       iwrd = 1; iwrdp = 0; iwrdl = length (words{iwrd});
-      for ii = 1:numel (fmt_words)
+      ii = 1;
+      while ii <= numel (fmt_words)
 
         nxt_wrd = 0;
 
@@ -521,12 +522,16 @@
 
         if (nxt_wrd)
           ++iwrd; iwrdp = 0;
-          if (ii < numel (fmt_words))
+          if (iwrd > numel (words))
+            ## Apparently EOF; assume incomplete row already at L.1 of data
+            ii = numel (fmt_words);
+          elseif (ii < numel (fmt_words))
             iwrdl = length (words{iwrd});
           endif
         endif
+        ++ii;
 
-      endfor
+      endwhile
       ## Done
       words_period = max (iwrd - 1, 1);
       num_lines = ceil (num_words / words_period);
@@ -910,6 +915,16 @@
 %! assert (c, [0.94; 0.87], 0.01)
 
 %!test
+%! [a, b] = strread (['Empty 1' char(10)], 'Empty%s %f');
+%! assert (a{1}, '1');
+%! assert (b, NaN);
+
+%!test
+%! [a, b] = strread (['Empty' char(10)], 'Empty%f %f');
+%! assert (a, NaN);
+%! assert (b, NaN);
+
+%!test
 %! # Bug #35999
 %! [a, b, c] = strread ("", "%f");
 %! assert (isempty (a));
--- a/scripts/io/textscan.m	Fri Jun 22 19:09:30 2012 +0200
+++ b/scripts/io/textscan.m	Fri Jun 22 20:01:35 2012 +0200
@@ -263,7 +263,9 @@
     endif
   elseif (eol_at_end)
      str(end-length (eol_char) + 1 : end) = "";
-  endif
+    ## A corner case: str may now be empty....
+    if (isempty (str)); return; endif
+   endif
 
   ## Call strread to make it do the real work
   C = cell (1, num_fields);
@@ -396,3 +398,7 @@
 %!error <cannot provide position information> [C, pos] = textscan ("Hello World")
 %!error <character value required> textscan ("Hello World", '%s', 'EndOfLine', 3)
 
+%! Test incomplete first data line
+%! R = textscan (['Empty1' char(10)], 'Empty%d %f');
+%! assert (R{1}, int32(1));
+%! assert (isempty(R{2}), true);