Mercurial > octave-antonio
comparison scripts/strings/strcat.m @ 17385:5ca5aff90ffd
strcat.m: Clarify in docstring that space isn't stripped from cell string inputs.
Add example of space-preserving cell string input to docstring.
Put input validation first in function.
* scripts/strings/strcat.m: Clarify in docstring that space isn't stripped from
cell string inputs. Add example of space-preserving cell string input to
docstring. Put input validation first in function.
author | Rik <rik@octave.org> |
---|---|
date | Fri, 06 Sep 2013 13:25:24 -0700 |
parents | 1c89599167a6 |
children | d63878346099 |
comparison
equal
deleted
inserted
replaced
17384:8c5878260636 | 17385:5ca5aff90ffd |
---|---|
18 ## <http://www.gnu.org/licenses/>. | 18 ## <http://www.gnu.org/licenses/>. |
19 | 19 |
20 ## -*- texinfo -*- | 20 ## -*- texinfo -*- |
21 ## @deftypefn {Function File} {} strcat (@var{s1}, @var{s2}, @dots{}) | 21 ## @deftypefn {Function File} {} strcat (@var{s1}, @var{s2}, @dots{}) |
22 ## Return a string containing all the arguments concatenated | 22 ## Return a string containing all the arguments concatenated |
23 ## horizontally. If the arguments are cells strings, @code{strcat} | 23 ## horizontally. If the arguments are cell strings, @code{strcat} |
24 ## returns a cell string with the individual cells concatenated. | 24 ## returns a cell string with the individual cells concatenated. |
25 ## For numerical input, each element is converted to the | 25 ## For numerical input, each element is converted to the |
26 ## corresponding ASCII character. Trailing white space for each of | 26 ## corresponding ASCII character. Trailing white space for any |
27 ## the inputs (@var{s1}, @var{s2}, @dots{}) is eliminated before they | 27 ## character string input is eliminated before the strings are |
28 ## are concatenated. | 28 ## concatenated. Note that cell string values do @strong{not} have |
29 ## whitespace trimmed. | |
29 ## | 30 ## |
30 ## For example: | 31 ## For example: |
31 ## | 32 ## |
32 ## @example | 33 ## @example |
33 ## @group | 34 ## @group |
34 ## strcat ("|", " leading space is preserved", "|") | 35 ## strcat ("|", " leading space is preserved", "|") |
35 ## @result{} | leading space is perserved| | 36 ## @result{} | leading space is preserved| |
36 ## @end group | 37 ## @end group |
37 ## @end example | 38 ## @end example |
38 ## | 39 ## |
39 ## @example | 40 ## @example |
40 ## @group | 41 ## @group |
60 ## @end group | 61 ## @end group |
61 ## @end example | 62 ## @end example |
62 ## | 63 ## |
63 ## @example | 64 ## @example |
64 ## @group | 65 ## @group |
65 ## s = @{ "ab"; "cde" @}; | 66 ## s = @{ "ab"; "cd " @}; |
66 ## strcat (s, s, s) | 67 ## strcat (s, s, s) |
67 ## @result{} | 68 ## @result{} |
68 ## @{ | 69 ## @{ |
69 ## [1,1] = ababab | 70 ## [1,1] = ababab |
70 ## [2,1] = cdecdecde | 71 ## [2,1] = cd cd cd |
71 ## @} | 72 ## @} |
72 ## @end group | 73 ## @end group |
73 ## @end example | 74 ## @end example |
74 ## | 75 ## |
75 ## @seealso{cstrcat, char, strvcat} | 76 ## @seealso{cstrcat, char, strvcat} |
77 | 78 |
78 ## Author: jwe | 79 ## Author: jwe |
79 | 80 |
80 function st = strcat (varargin) | 81 function st = strcat (varargin) |
81 | 82 |
82 if (nargin > 0) | 83 if (nargin == 0) |
83 if (nargin == 1) | 84 print_usage (); |
84 st = varargin{1}; | 85 endif |
85 elseif (nargin > 1) | |
86 ## Convert to cells of strings | |
87 uo = "uniformoutput"; | |
88 reals = cellfun ("isreal", varargin); | |
89 if (any (reals)) | |
90 varargin(reals) = cellfun ("char", varargin(reals), uo, false); | |
91 endif | |
92 chars = cellfun ("isclass", varargin, "char"); | |
93 allchar = all (chars); | |
94 varargin(chars) = cellfun ("cellstr", varargin(chars), uo, false); | |
95 if (! all (cellfun ("isclass", varargin, "cell"))) | |
96 error ("strcat: inputs must be strings or cells of strings"); | |
97 endif | |
98 | 86 |
99 ## We don't actually need to bring all cells to common size, because | 87 if (nargin == 1) |
100 ## cellfun can now expand scalar cells. | 88 st = varargin{1}; |
101 err = common_size (varargin{:}); | 89 else |
90 ## Convert to cells of strings | |
91 uo = "uniformoutput"; | |
92 reals = cellfun ("isreal", varargin); | |
93 if (any (reals)) | |
94 varargin(reals) = cellfun ("char", varargin(reals), uo, false); | |
95 endif | |
96 chars = cellfun ("isclass", varargin, "char"); | |
97 allchar = all (chars); | |
98 varargin(chars) = cellfun ("cellstr", varargin(chars), uo, false); | |
99 if (! all (cellfun ("isclass", varargin, "cell"))) | |
100 error ("strcat: inputs must be strings or cells of strings"); | |
101 endif | |
102 | 102 |
103 if (err) | 103 ## We don't actually need to bring all cells to common size, because |
104 error ("strcat: arguments must be the same size, or be scalars"); | 104 ## cellfun can now expand scalar cells. |
105 endif | 105 err = common_size (varargin{:}); |
106 | 106 |
107 ## Cellfun handles everything for us. | 107 if (err) |
108 st = cellfun ("horzcat", varargin{:}, uo, false); | 108 error ("strcat: arguments must be the same size, or be scalars"); |
109 endif | |
109 | 110 |
110 if (allchar) | 111 ## Cellfun handles everything for us. |
111 ## If all inputs were strings, return strings. | 112 st = cellfun ("horzcat", varargin{:}, uo, false); |
112 st = char (st); | 113 |
113 endif | 114 if (allchar) |
115 ## If all inputs were strings, return strings. | |
116 st = char (st); | |
114 endif | 117 endif |
115 else | |
116 print_usage (); | |
117 endif | 118 endif |
118 | 119 |
119 endfunction | 120 endfunction |
120 | 121 |
121 | 122 |