Mercurial > hg > octave-lyh
annotate scripts/strings/strjoin.m @ 16761:c6d61dca5acd
* strjoin.m: improve speed of joining long strings
author | Stefan Mahr <dac922@gmx.de> |
---|---|
date | Tue, 04 Jun 2013 19:40:13 +0200 |
parents | 424463a80134 |
children | 70ea511edbc4 |
rev | line source |
---|---|
16400 | 1 ## Copyright (C) 2007 Muthiah Annamalai <muthiah.annamalai@uta.edu> |
2 ## Copyright (C) 2013 Ben Abbott <bpabbott@mac.com> | |
3 ## | |
4 ## This file is part of Octave. | |
5 ## | |
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. | |
10 ## | |
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. | |
15 ## | |
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/>. | |
19 | |
20 ## -*- texinfo -*- | |
21 ## @deftypefn {Function File} {@var{str} =} strjoin (@var{cstr}) | |
22 ## @deftypefnx {Function File} {@var{str} =} strjoin (@var{cstr}, @var{delimiter}) | |
23 ## Joins the elements of the cell-string array, @var{cstr}, into a single | |
24 ## string. | |
25 ## | |
26 ## If no @var{delimiter} is specified, the elements of @var{cstr} | |
27 ## seperated by a space. | |
28 ## | |
29 ## If @var{delimiter} is specified as a string, the cell-string array is | |
30 ## joined using the string. | |
31 ## | |
32 ## If @var{delimiter} is a cell-string array whose length is one less | |
33 ## than @var{cstr}, then the elemennts of @var{cstr} are joined by | |
34 ## interleaving the cell-string elements of @var{delimiter}. | |
35 ## | |
36 ## @example | |
37 ## @group | |
38 ## strjoin (@{'Octave','Scilab','Lush','Yorick'@}, '*') | |
39 ## @result{} 'Octave*Scilab*Lush*Yorick' | |
40 ## @end group | |
41 ## @end example | |
42 ## @seealso {strsplit} | |
43 ## @end deftypefn | |
44 | |
45 function rval = strjoin (cstr, delimiter) | |
46 | |
47 if (nargin == 1) | |
48 delimiter = " "; | |
49 elseif (nargin < 1 || nargin > 2) | |
50 print_usage (); | |
51 elseif (! (iscellstr (cstr) && (ischar (delimiter) || iscellstr (delimiter)))) | |
52 print_usage (); | |
53 endif | |
54 | |
55 if (numel (cstr) == 1) | |
56 rval = cstr{1}; | |
57 return | |
58 endif | |
59 | |
60 if (ischar (delimiter)) | |
61 delimiter = {delimiter}; | |
62 end | |
63 | |
64 num = numel (cstr); | |
65 if (numel (delimiter) == 1 && num > 1) | |
66 delimiter = repmat (delimiter, 1, num); | |
67 delimiter(end) = {""}; | |
68 elseif (numel (delimiter) != num - 1) | |
69 error ("strjoin:cellstring_delimiter_mismatch", | |
70 "strjoin: the number of delimiters does not match the number of strings") | |
71 else | |
72 delimiter(end+1) = {""}; | |
73 endif | |
74 | |
16761
c6d61dca5acd
* strjoin.m: improve speed of joining long strings
Stefan Mahr <dac922@gmx.de>
parents:
16400
diff
changeset
|
75 rval = [[cstr(:).'; delimiter(:).']{:}]; |
16400 | 76 |
77 endfunction | |
78 | |
79 %!assert (strjoin ({"hello"}, "-"), "hello") | |
80 %!assert (strjoin ({"hello", "world"}), "hello world") | |
81 %!assert (strjoin ({"Octave", "Scilab", "Lush", "Yorick"}, "*"), | |
82 %! "Octave*Scilab*Lush*Yorick") | |
83 %!assert (strjoin ({"space", "comma", "dash", "semicolon", "done"}, | |
84 %! {" ", ",", "-", ";"}), "space comma,dash-semicolon;done") |