Mercurial > hg > octave-nkf
comparison scripts/strings/strtrunc.m @ 13306:ad9b78544a22
strtrunc.m: Add input validation and tests for length argument n.
* strtrunc.m: Add input validation and tests for length argument n.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sun, 09 Oct 2011 12:58:59 -0700 |
parents | e1524d82f8e0 |
children | 72c96de7a403 |
comparison
equal
deleted
inserted
replaced
13305:63463570d9fe | 13306:ad9b78544a22 |
---|---|
28 | 28 |
29 if (nargin != 2) | 29 if (nargin != 2) |
30 print_usage (); | 30 print_usage (); |
31 endif | 31 endif |
32 | 32 |
33 n = fix (n); | |
34 if (! isscalar (n) || n < 0) | |
35 error ("strtrunc: length N must be a positive integer (N >= 0)"); | |
36 endif | |
37 | |
33 if (ischar (s)) | 38 if (ischar (s)) |
34 if (n < columns (s)) | 39 if (n < columns (s)) |
35 s = s(:, 1:n); | 40 s = s(:, 1:n); |
36 endif | 41 endif |
37 elseif (iscellstr (s)) | 42 elseif (iscellstr (s)) |
38 ## Convoluted approach converts cellstr to char matrix, trims the character | 43 ## Convoluted approach converts cellstr to char matrix, trims the character |
39 ## matrix using indexing, and then converts back to cellstr with mat2cell. | 44 ## matrix using indexing, and then converts back to cellstr with mat2cell. |
40 ## This approach is 24X faster than using cellfun with call to strtrunc | 45 ## This approach is 24X faster than using cellfun with call to strtrunc |
41 idx = cellfun ("size", s, 2) >= n; | 46 idx = cellfun ("size", s, 2) > n; |
42 rows = cellfun ("size", s(idx), 1); | 47 rows = cellfun ("size", s(idx), 1); |
43 s(idx) = mat2cell (char (s(idx))(:, 1:n), rows); | 48 if (! isempty (rows)) |
49 s(idx) = mat2cell (char (s(idx))(:, 1:n), rows); | |
50 endif | |
44 else | 51 else |
45 error ("strtrunc: S must be a character string or a cell array of strings"); | 52 error ("strtrunc: S must be a character string or a cell array of strings"); |
46 endif | 53 endif |
47 | 54 |
48 endfunction | 55 endfunction |
50 | 57 |
51 %!assert (strtrunc("abcdefg", 4), "abcd"); | 58 %!assert (strtrunc("abcdefg", 4), "abcd"); |
52 %!assert (strtrunc("abcdefg", 10), "abcdefg"); | 59 %!assert (strtrunc("abcdefg", 10), "abcdefg"); |
53 %!assert (strtrunc(char ("abcdef", "fedcba"), 3), ["abc"; "fed"]); | 60 %!assert (strtrunc(char ("abcdef", "fedcba"), 3), ["abc"; "fed"]); |
54 %!assert (strtrunc({"abcdef", "fedcba"}, 3), {"abc", "fed"}); | 61 %!assert (strtrunc({"abcdef", "fedcba"}, 3), {"abc", "fed"}); |
55 %!assert (strtrunc({"1", "21", "321"}, 1), {"1", "2", "3"}) | 62 %!assert (strtrunc({"", "1", "21", "321"}, 1), {"", "1", "2", "3"}) |
63 %!assert (strtrunc({"1", "", "2"}, 1), {"1", "", "2"}) | |
56 %!test | 64 %!test |
57 %! cstr = {"line1"; ["line2"; "line3"]; "line4"}; | 65 %! cstr = {"line1"; ["line2"; "line3"]; "line4"}; |
58 %! y = strtrunc (cstr, 4); | 66 %! y = strtrunc (cstr, 4); |
59 %! assert (size (y), [3, 1]); | 67 %! assert (size (y), [3, 1]); |
60 %! assert (size (y{2}), [2, 4]); | 68 %! assert (size (y{2}), [2, 4]); |
62 | 70 |
63 %% Test input validation | 71 %% Test input validation |
64 %!error strtrunc () | 72 %!error strtrunc () |
65 %!error strtrunc ("abcd") | 73 %!error strtrunc ("abcd") |
66 %!error strtrunc ("abcd", 4, 5) | 74 %!error strtrunc ("abcd", 4, 5) |
75 %!error <N must be a positive integer> strtrunc ("abcd", ones (2,2)) | |
76 %!error <N must be a positive integer> strtrunc ("abcd", -1) | |
67 %!error <S must be a character string or a cell array of strings> strtrunc (1, 1) | 77 %!error <S must be a character string or a cell array of strings> strtrunc (1, 1) |
68 | 78 |