# HG changeset patch # User Philip Nienhuis # Date 1346352760 -7200 # Node ID 2136343014d56ddd5d6f997a678ceffe76eb4bf1 # Parent ad1a980b0cb572507017f22f6b96e9a0cbaa8389 bug #37023 (wrong reading of lines starting and/or ending with whitespace) * strread.m: fix regexprep regular expression, test added, improved format specifier parsing * textscan.m: test added diff -r ad1a980b0cb5 -r 2136343014d5 scripts/io/strread.m --- a/scripts/io/strread.m Thu Aug 30 12:05:04 2012 -0400 +++ b/scripts/io/strread.m Thu Aug 30 20:52:40 2012 +0200 @@ -405,8 +405,10 @@ ## Check for single delimiter followed/preceded by whitespace if (! isempty (delimiter_str)) dlmstr = setdiff (delimiter_str, " "); - rxp_dlmwsp = sprintf ("( [%s]|[%s] )", dlmstr, dlmstr); - str = regexprep (str, rxp_dlmwsp, delimiter_str(1)); + if (! isempty (dlmstr)) + rxp_dlmwsp = sprintf ('( [%s] | [%s]|[%s] )', dlmstr, dlmstr, dlmstr); + str = regexprep (str, rxp_dlmwsp, delimiter_str(1)); + endif endif ## Wipe leading and trailing whitespace on each line (it may be ## delimiter too) @@ -528,7 +530,7 @@ ## ..or it IS found. Add inferred width of current conversion field iwrdp += index (words{iwrd}(iwrdp+1:end), fmt_words{ii+1}) - 1; endif - elseif (iwrdp < iwrdl) + elseif (iwrdp <= iwrdl) ## No bordering literal to the right => field occupies (rest of) word nxt_wrd = 1; endif @@ -956,6 +958,12 @@ %! assert (isempty (b)); %! assert (isempty (c)); +%% bug #37023 +%!test +%! [a, b] = strread (" 1. 1 \n 2 3 \n", "%f %f", "endofline", "\n"); +%! assert (a, [1; 2], 1e-15); +%! assert (b, [1; 3], 1e-15); + %% Unsupported format specifiers %!test %!error strread ("a", "%c") diff -r ad1a980b0cb5 -r 2136343014d5 scripts/io/textscan.m --- a/scripts/io/textscan.m Thu Aug 30 12:05:04 2012 -0400 +++ b/scripts/io/textscan.m Thu Aug 30 20:52:40 2012 +0200 @@ -402,3 +402,9 @@ %! R = textscan (['Empty1' char(10)], 'Empty%d %f'); %! assert (R{1}, int32 (1)); %! assert (isempty (R{2}), true); + +%% bug #37023 (actually a strread test) +%!test +%! data = textscan(" 1. 1 \n 2 3\n", '%f %f'); +%! assert (data{1}, [1; 2], 1e-15); +%! assert (data{2}, [1; 3], 1e-15);