# HG changeset patch # User Philip Nienhuis # Date 1440622319 -7200 # Node ID 7fbba8c8efd5d919be9a7f05e7d7de78bd958fcf # Parent 5ce959c55cc04638c2c8ee43b30c53199df27c1f strread.m: fix nilpotent IF block for ...%sliteral... fields diff -r 5ce959c55cc0 -r 7fbba8c8efd5 scripts/io/strread.m --- a/scripts/io/strread.m Sun Aug 24 19:35:06 2014 +0530 +++ b/scripts/io/strread.m Wed Aug 26 22:51:59 2015 +0200 @@ -637,11 +637,7 @@ fwptr = [fwptr(1:ii) (++fwptr(ii+1:end))]; else - if (! idg(ii) && ! isempty (strfind (fmt_words{ii-1}, "%s"))) - ## Trailing literal. - ## If preceding format == '%s' this is an error. - warning ("strread: ambiguous '%s' specifier next to literal in column %d", icol); - elseif (idg(ii)) + if (idg(ii)) ## Current field = fixed width. Strip into icol, rest in icol+1 sw = regexp (fmt_words{ii}, '\d', "once"); ew = regexp (fmt_words{ii}, '[nfuds]') - 1; @@ -657,6 +653,10 @@ fwptr = [fwptr(1:ii) (++fwptr(ii+1:end))]; endif else + if (! isempty (strfind (fmt_words{ii-1}, "%s"))) + ## Trailing literal. Could be ambiguous if preceding format == '%s' + warning ("strread.m:\n Ambiguous '%%s' specifier immediately before literal in column %d", icol); + endif ## FIXME: this assumes char(254)/char(255) won't occur in input! clear wrds; wrds(1:2:2*numel (words(icol, jptr))) = ... @@ -1102,3 +1102,11 @@ %! str = "14 :1 z:2 z:3 z:5 z:11"; %! [a, b, c, d] = strread (str, "%f %s %*s %3s %*3s %f", "delimiter", ":"); %! assert ({a, b, c, d}, {14, {"1 z"}, {"3 z"}, 11}); + +## Allow cuddling %sliteral but warn it is ambiguous +%!test +%! [a, b] = strread ("abcxyz51\nxyz83\n##xyz101", "%s xyz %d"); +%! assert (a([1 3]), {"abc"; "##"}); +%! assert (isempty (a{2}), true); +%! assert (b, int32([51; 83; 101])); +