comparison scripts/io/strread.m @ 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 5d3a684236b0
children 8f0d07f8390b
comparison
equal deleted inserted replaced
15262:ad1a980b0cb5 15263:2136343014d5
403 str = str(2:end); 403 str = str(2:end);
404 endif 404 endif
405 ## Check for single delimiter followed/preceded by whitespace 405 ## Check for single delimiter followed/preceded by whitespace
406 if (! isempty (delimiter_str)) 406 if (! isempty (delimiter_str))
407 dlmstr = setdiff (delimiter_str, " "); 407 dlmstr = setdiff (delimiter_str, " ");
408 rxp_dlmwsp = sprintf ("( [%s]|[%s] )", dlmstr, dlmstr); 408 if (! isempty (dlmstr))
409 str = regexprep (str, rxp_dlmwsp, delimiter_str(1)); 409 rxp_dlmwsp = sprintf ('( [%s] | [%s]|[%s] )', dlmstr, dlmstr, dlmstr);
410 str = regexprep (str, rxp_dlmwsp, delimiter_str(1));
411 endif
410 endif 412 endif
411 ## Wipe leading and trailing whitespace on each line (it may be 413 ## Wipe leading and trailing whitespace on each line (it may be
412 ## delimiter too) 414 ## delimiter too)
413 ## FIXME: Double strrep on str is enormously expensive of CPU time. 415 ## FIXME: Double strrep on str is enormously expensive of CPU time.
414 ## Can this be eliminated 416 ## Can this be eliminated
526 nxt_wrd = 1; 528 nxt_wrd = 1;
527 else 529 else
528 ## ..or it IS found. Add inferred width of current conversion field 530 ## ..or it IS found. Add inferred width of current conversion field
529 iwrdp += index (words{iwrd}(iwrdp+1:end), fmt_words{ii+1}) - 1; 531 iwrdp += index (words{iwrd}(iwrdp+1:end), fmt_words{ii+1}) - 1;
530 endif 532 endif
531 elseif (iwrdp < iwrdl) 533 elseif (iwrdp <= iwrdl)
532 ## No bordering literal to the right => field occupies (rest of) word 534 ## No bordering literal to the right => field occupies (rest of) word
533 nxt_wrd = 1; 535 nxt_wrd = 1;
534 endif 536 endif
535 537
536 endif 538 endif
954 %! [a, b, c] = strread ("", "%f"); 956 %! [a, b, c] = strread ("", "%f");
955 %! assert (isempty (a)); 957 %! assert (isempty (a));
956 %! assert (isempty (b)); 958 %! assert (isempty (b));
957 %! assert (isempty (c)); 959 %! assert (isempty (c));
958 960
961 %% bug #37023
962 %!test
963 %! [a, b] = strread (" 1. 1 \n 2 3 \n", "%f %f", "endofline", "\n");
964 %! assert (a, [1; 2], 1e-15);
965 %! assert (b, [1; 3], 1e-15);
966
959 %% Unsupported format specifiers 967 %% Unsupported format specifiers
960 %!test 968 %!test
961 %!error <format specifiers are not supported> strread ("a", "%c") 969 %!error <format specifiers are not supported> strread ("a", "%c")
962 %!error <format specifiers are not supported> strread ("a", "%*c %d") 970 %!error <format specifiers are not supported> strread ("a", "%*c %d")
963 %!error <format specifiers are not supported> strread ("a", "%q") 971 %!error <format specifiers are not supported> strread ("a", "%q")