Mercurial > octave-nkf
comparison scripts/io/strread.m @ 18902:2b82d2f29a7b
maint: Periodic merge of gui-release to default.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 27 Jun 2014 11:56:19 -0400 |
parents | 7bbe3658c5ef 589354cf668f |
children | c573d9c70ae5 |
comparison
equal
deleted
inserted
replaced
18899:a0fd65914811 | 18902:2b82d2f29a7b |
---|---|
188 format = "%f"; | 188 format = "%f"; |
189 endif | 189 endif |
190 | 190 |
191 if (! ischar (str) || ! ischar (format)) | 191 if (! ischar (str) || ! ischar (format)) |
192 error ("strread: STR and FORMAT arguments must be strings"); | 192 error ("strread: STR and FORMAT arguments must be strings"); |
193 endif | |
194 | |
195 if (strcmp (typeinfo (format), "sq_string")) | |
196 format = do_string_escapes (format); | |
193 endif | 197 endif |
194 | 198 |
195 ## Parse format string to compare number of conversion fields and nargout | 199 ## Parse format string to compare number of conversion fields and nargout |
196 nfields = length (strfind (format, "%")) - length (strfind (format, "%*")); | 200 nfields = length (strfind (format, "%")) - length (strfind (format, "%*")); |
197 ## If str only has numeric fields, a (default) format ("%f") will do. | 201 ## If str only has numeric fields, a (default) format ("%f") will do. |
388 ## Add eol_char to delimiter collection | 392 ## Add eol_char to delimiter collection |
389 delimiter_str = unique ([delimiter_str eol_char]); | 393 delimiter_str = unique ([delimiter_str eol_char]); |
390 ## .. and remove it from whitespace collection | 394 ## .. and remove it from whitespace collection |
391 white_spaces = strrep (white_spaces, eol_char, ''); | 395 white_spaces = strrep (white_spaces, eol_char, ''); |
392 endif | 396 endif |
397 | |
398 ii = numel (fmt_words); | |
399 while (ii > 0) | |
400 if (ismember (fmt_words{ii}, delimiter_str)(1)) | |
401 fmt_words(ii) = []; | |
402 --num_words_per_line; | |
403 endif | |
404 --ii; | |
405 endwhile | |
393 | 406 |
394 pad_out = 0; | 407 pad_out = 0; |
395 ## Trim whitespace if needed | 408 ## Trim whitespace if needed |
396 if (! isempty (white_spaces)) | 409 if (! isempty (white_spaces)) |
397 ## Check if trailing "\n" might signal padding output arrays to equal size | 410 ## Check if trailing "\n" might signal padding output arrays to equal size |
972 | 985 |
973 %% Test for no output arg (interactive use) | 986 %% Test for no output arg (interactive use) |
974 %!test | 987 %!test |
975 %! assert (strread (",2,,4\n5,,7,", "", "delimiter", ","), [NaN; 2; NaN; 4; 5; NaN; 7]); | 988 %! assert (strread (",2,,4\n5,,7,", "", "delimiter", ","), [NaN; 2; NaN; 4; 5; NaN; 7]); |
976 | 989 |
990 %% Test #1 bug #42609 | |
991 %!test | |
992 %! [a, b, c] = strread ("1 2 3\n4 5 6\n7 8 9\n", "%f %f %f\n"); | |
993 %! assert (a, [1; 4; 7]); | |
994 %! assert (b, [2; 5; 8]); | |
995 %! assert (c, [3; 6; 9]); | |
996 | |
997 %% Test #2 bug #42609 | |
998 %!test | |
999 %! [a, b, c] = strread ("1 2\n3\n4 5\n6\n7 8\n9\n", "%f %f\n%f"); | |
1000 %! assert (a, [1;4;7]); | |
1001 %! assert (b, [2; 5; 8]); | |
1002 %! assert (c, [3; 6; 9]); | |
1003 | |
1004 %% Test #3 bug #42609 | |
1005 %!test | |
1006 %! [a, b, c] = strread ("1 2 3\n4 5 6\n7 8 9\n", '%f %f %f\n'); | |
1007 %! assert (a, [1; 4; 7]); | |
1008 %! assert (b, [2; 5; 8]); | |
1009 %! assert (c, [3; 6; 9]); | |
1010 | |
1011 %% Test #3 bug #42609 | |
1012 %!test | |
1013 %! [a, b, c] = strread ("1 2\n3\n4 5\n6\n7 8\n9\n", '%f %f\n%f'); | |
1014 %! assert (a, [1;4;7]); | |
1015 %! assert (b, [2; 5; 8]); | |
1016 %! assert (c, [3; 6; 9]); | |
1017 | |
977 %% Unsupported format specifiers | 1018 %% Unsupported format specifiers |
978 %!test | 1019 %!test |
979 %!error <format specifiers are not supported> strread ("a", "%c") | 1020 %!error <format specifiers are not supported> strread ("a", "%c") |
980 %!error <format specifiers are not supported> strread ("a", "%*c %d") | 1021 %!error <format specifiers are not supported> strread ("a", "%*c %d") |
981 %!error <format specifiers are not supported> strread ("a", "%q") | 1022 %!error <format specifiers are not supported> strread ("a", "%q") |