1 ## Copyright (C) 1994-2012 John W. Eaton
2 ## Copyright (C) 2009 Jaroslav Hajek
4 ## This file is part of Octave.
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ## General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING. If not, see
18 ## <http://www.gnu.org/licenses/>.
21 ## @deftypefn {Function File} {} strcat (@var{s1}, @var{s2}, @dots{})
22 ## Return a string containing all the arguments concatenated
23 ## horizontally. If the arguments are cells strings, @code{strcat}
24 ## returns a cell string with the individual cells concatenated.
25 ## For numerical input, each element is converted to the
26 ## corresponding ASCII character. Trailing white space is eliminated.
31 ## s = [ "ab"; "cde" ];
41 ## s = @{ "ab"; "cde" @};
51 ## @seealso{cstrcat, char, strvcat}
56 function st = strcat (varargin)
62 ## Convert to cells of strings
64 reals = cellfun ("isreal", varargin);
66 varargin(reals) = cellfun ("char", varargin(reals), uo, false);
68 chars = cellfun ("isclass", varargin, "char");
69 allchar = all (chars);
70 varargin(chars) = cellfun ("cellstr", varargin(chars), uo, false);
71 if (! all (cellfun ("isclass", varargin, "cell")))
72 error ("strcat: inputs must be strings or cells of strings");
75 ## We don't actually need to bring all cells to common size, because
76 ## cellfun can now expand scalar cells.
77 err = common_size (varargin{:});
80 error ("strcat: arguments must be the same size, or be scalars");
83 ## Cellfun handles everything for us.
84 st = cellfun ("horzcat", varargin{:}, uo, false);
87 ## If all inputs were strings, return strings.
97 ## test the dimensionality
99 %!assert(strcat("ab ", "ab "), "abab")
100 %!assert(strcat({"ab "}, "ab "), {"ab ab"})
101 %!assert(strcat("ab ", {"ab "}), {"abab "})
102 %!assert(strcat({"ab "}, {"ab "}), {"ab ab "})
103 %!assert(strcat("", "ab"), "ab")
104 %!assert(strcat("", {"ab"}, {""}), {"ab"})
106 %!assert(strcat(["ab ";"cde"], ["ab ";"cde"]), ["abab ";"cdecde"])
108 ## test for deblanking implied trailing spaces of character input
109 %!assert((strcmp (strcat ("foo", "bar"), "foobar")
110 %! && strcmp (strcat (["a"; "bb"], ["foo"; "bar"]), ["afoo "; "bbbar"])));
112 ## test for mixing character and cell inputs
113 %!assert(all (strcmp (strcat ("a", {"bc", "de"}, "f"), {"abcf", "adef"})))
115 ## test for scalar strings with vector strings
116 %!assert(all (strcmp (strcat (["a"; "b"], "c"), ["ac"; "bc"])))
118 ## test with cells with strings of differing lengths
119 %!assert(all (strcmp (strcat ({"a", "bb"}, "ccc"), {"accc", "bbccc"})))
120 %!assert(all (strcmp (strcat ("a", {"bb", "ccc"}), {"abb", "accc"})))
124 %!assert (strcat (1, 2), strcat (char(1), char(2)))
126 %!assert (strcat ('', 2), strcat ([], char(2)))