changeset 15263:2136343014d5

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
author Philip Nienhuis <prnienhuis@users.sf.net>
date Thu, 30 Aug 2012 20:52:40 +0200
parents ad1a980b0cb5
children 94cdf82d4a0c
files scripts/io/strread.m scripts/io/textscan.m
diffstat 2 files changed, 17 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- 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 <format specifiers are not supported> strread ("a", "%c")
--- 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);