# HG changeset patch # User Philip Nienhuis # Date 1340388095 -7200 # Node ID a922f768ee09a51813d134c9a0b6c9990b762184 # Parent 21197c43f984f8a43b7a1bb749009cdf26234589 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 diff -r 21197c43f984 -r a922f768ee09 scripts/io/strread.m --- 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)); diff -r 21197c43f984 -r a922f768ee09 scripts/io/textscan.m --- 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 [C, pos] = textscan ("Hello World") %!error 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);