changeset 22693:644dcdfa0348

strread.m: fix processing open comment styles (bug #49454). * strread.m: New variable cmt_eol to track the eol character for comments, defaults to "\n". Add input validation to override cmt_eol if "endofline" option is specifically set. Add BIST test.
author Philip Nienhuis <prnienhuis@users.sf.net>
date Fri, 28 Oct 2016 12:12:59 +0200
parents 5e2cb8fbfec7
children b100f76b991d
files scripts/io/strread.m
diffstat 1 files changed, 23 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/io/strread.m	Fri Oct 28 08:31:10 2016 -0700
+++ b/scripts/io/strread.m	Fri Oct 28 12:12:59 2016 +0200
@@ -235,6 +235,9 @@
 
   ## Parse options.  First initialize defaults
   comment_flag = false;
+  open_comment = false;
+  ## Default line ending.  FIXME: we ignore old Macintosh CR eol.
+  cmt_eol = "\n";
   delimiter_str = "";
   empty_str = "";
   eol_char = "";
@@ -253,15 +256,19 @@
           case "c"
             [comment_start, comment_end] = deal ("/*", "*/");
           case "c++"
-            [comment_start, comment_end] = deal ("//", "eol_char");
+            [comment_start, comment_end] = deal ("//", "cmt_eol");
+            open_comment = true;
           case "shell"
-            [comment_start, comment_end] = deal ("#" , "eol_char");
+            [comment_start, comment_end] = deal ("#" , "cmt_eol");
+            open_comment = true;
           case "matlab"
-            [comment_start, comment_end] = deal ("%" , "eol_char");
+            [comment_start, comment_end] = deal ("%" , "cmt_eol");
+            open_comment = true;
           otherwise
             if (ischar (varargin{n+1})
                 || (numel (varargin{n+1}) == 1 && iscellstr (varargin{n+1})))
-              [comment_start, comment_end] = deal (char (varargin{n+1}), "eol_char");
+              [comment_start, comment_end] = deal (char (varargin{n+1}), "cmt_eol");
+            open_comment = true;
             elseif (iscellstr (varargin{n+1}) && numel (varargin{n+1}) == 2)
               [comment_start, comment_end] = deal (varargin{n+1}{:});
             else
@@ -291,6 +298,8 @@
         if (strcmp (typeinfo (eol_char), "sq_string"))
           eol_char = do_string_escapes (eol_char);
         endif
+        cmt_eol = eol_char;
+        open_comment = false;
       case "returnonerror"
         err_action = varargin{n+1};
       case "multipledelimsasone"
@@ -361,10 +370,13 @@
 
   ## Remove comments in str
   if (comment_flag)
-    ## Expand 'eol_char' here, after option processing which may have set value
-    comment_end = strrep (comment_end, "eol_char", eol_char);
+    ## Expand 'cmt_eol' here, after option processing which may have set value
+    comment_end = strrep (comment_end, "cmt_eol", cmt_eol);
     cstart = strfind (str, comment_start);
     cstop  = strfind (str, comment_end);
+    if (open_comment)
+      cstop -= 1;
+    endif
     ## Treat end of string as additional comment stop
     if (isempty (cstop) || cstop(end) != length (str))
       cstop(end+1) = length (str);
@@ -883,6 +895,11 @@
 %!                  "commentstyle", "shell"), ...
 %!         {"Hello"; "World!"});
 
+%!test <49454>
+%! assert (strread ("hello%foo\nworld, another%bar\r\nday", "%s", ...
+%!                  "commentstyle", "matlab", "delimiter", " ,"),...
+%!         {"hello"; "world"; "another"; "day"});
+
 %!test
 %! str = sprintf ("Tom 100 miles/hr\nDick 90 miles/hr\nHarry 80 miles/hr");
 %! fmt = "%s %f miles/hr";