Mercurial > hg > octave-lyh
annotate src/strfns.cc @ 8463:a74871446af7
Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 12 Jan 2009 12:31:11 -0500 |
parents | ebdf1e058d85 |
children | cb48e594a441 |
rev | line source |
---|---|
807 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1994, 1995, 1996, 1997, 1999, 2002, 2003, 2004, 2005, |
4 2006, 2007 John W. Eaton | |
807 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
807 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
807 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
1192 | 25 #include <config.h> |
807 | 26 #endif |
27 | |
1355 | 28 #include <cctype> |
7528
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
29 |
8463
a74871446af7
Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents:
8462
diff
changeset
|
30 #include <queue> |
5765 | 31 #include <sstream> |
807 | 32 |
33 #include "dMatrix.h" | |
34 | |
5416 | 35 #include "Cell.h" |
1355 | 36 #include "defun.h" |
807 | 37 #include "error.h" |
38 #include "gripes.h" | |
2366 | 39 #include "ov.h" |
1355 | 40 #include "oct-obj.h" |
4457 | 41 #include "unwind-prot.h" |
807 | 42 #include "utils.h" |
43 | |
4358 | 44 DEFUN (char, args, , |
45 "-*- texinfo -*-\n\ | |
46 @deftypefn {Built-in Function} {} char (@var{x})\n\ | |
47 @deftypefnx {Built-in Function} {} char (@var{cell_array})\n\ | |
48 @deftypefnx {Built-in Function} {} char (@var{s1}, @var{s2}, @dots{})\n\ | |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
49 Create a string array from one or more numeric matrices, character\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
50 matrices or cell arrays. For numerical input, each element is converted\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
51 to the corresponding ASCII character. The arguments (and elements of\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
52 cell array(s)) are concatenated vertically.\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
53 The returned values are padded with blanks as needed to make each row\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
54 of the string array have the same length. Empty strings are not removed.\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
55 For example,\n\ |
4358 | 56 \n\ |
57 @example\n\ | |
58 @group\n\ | |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
59 char ([97, 98, 99], \"\", @{\"98\", \"99\", 100@}, [\"num\", \"bers\"])\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
60 @result{} [\"abc \"\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
61 \" \"\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
62 \"98 \"\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
63 \"99 \"\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
64 \"d \"\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
65 \"numbers\"]\n\ |
4358 | 66 @end group\n\ |
67 @end example\n\ | |
68 \n\ | |
69 @end deftypefn") | |
70 { | |
71 octave_value retval; | |
72 | |
73 int nargin = args.length (); | |
74 | |
75 if (nargin == 1) | |
5281 | 76 retval = args(0).convert_to_str (true, true, |
77 args(0).is_dq_string () ? '"' : '\''); | |
4358 | 78 else if (nargin > 1) |
79 { | |
80 int n_elts = 0; | |
81 | |
82 int max_len = 0; | |
83 | |
8463
a74871446af7
Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents:
8462
diff
changeset
|
84 std::queue<string_vector> args_as_strings; |
a74871446af7
Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents:
8462
diff
changeset
|
85 |
4358 | 86 for (int i = 0; i < nargin; i++) |
87 { | |
5707 | 88 string_vector s = args(i).all_strings (); |
4358 | 89 |
90 if (error_state) | |
91 { | |
4457 | 92 error ("char: unable to convert some args to strings"); |
4358 | 93 return retval; |
94 } | |
95 | |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
96 if (s.length () > 0) |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
97 n_elts += s.length (); |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
98 else |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
99 n_elts += 1; |
4358 | 100 |
101 int s_max_len = s.max_length (); | |
102 | |
103 if (s_max_len > max_len) | |
104 max_len = s_max_len; | |
8463
a74871446af7
Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents:
8462
diff
changeset
|
105 |
a74871446af7
Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents:
8462
diff
changeset
|
106 args_as_strings.push (s); |
4358 | 107 } |
108 | |
109 string_vector result (n_elts); | |
110 | |
111 int k = 0; | |
112 | |
113 for (int i = 0; i < nargin; i++) | |
114 { | |
8463
a74871446af7
Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents:
8462
diff
changeset
|
115 string_vector s = args_as_strings.pop (); |
4358 | 116 |
117 int n = s.length (); | |
118 | |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
119 if (n > 0) |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
120 { |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
121 for (int j = 0; j < n; j++) |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
122 { |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
123 std::string t = s[j]; |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
124 int t_len = t.length (); |
4358 | 125 |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
126 if (max_len > t_len) |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
127 t += std::string (max_len - t_len, ' '); |
4358 | 128 |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
129 result[k++] = t; |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
130 } |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
131 } |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
132 else |
8463
a74871446af7
Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents:
8462
diff
changeset
|
133 result[k++] = std::string (max_len, ' '); |
4358 | 134 } |
135 | |
5280 | 136 retval = octave_value (result, '\''); |
4358 | 137 } |
138 else | |
5823 | 139 print_usage (); |
4358 | 140 |
141 return retval; | |
142 } | |
143 | |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
144 /* |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
145 %!error <Invalid call to char> char() |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
146 %!assert (char (100) == "d"); |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
147 %!assert (all(char (100,100) == ["d";"d"])); |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
148 %!assert (all(char ({100,100}) == ["d";"d"])); |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
149 %!assert (all(char ([100,100]) == ["dd"])); |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
150 %!assert (all(char ({100,{100}}) == ["d";"d"])); |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
151 %!assert (all(char (100, [], 100) == ["d";" ";"d"])) |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
152 %!assert (all(char ({100, [], 100}) == ["d";" ";"d"])) |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
153 %!assert (all(char ({100,{100, {""}}}) == ["d";"d";" "])) |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
154 %!assert (all(char (["a";"be"], {"c", 100}) == ["a";"be";"c";"d"])) |
8372
8dff9cba15fe
move str2mat to deprecated and make it a simple wrapper around char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8353
diff
changeset
|
155 %!assert(strcmp (char ("a", "bb", "ccc"), ["a "; "bb "; "ccc"])); |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
156 */ |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
157 |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
158 DEFUN (strvcat, args, , |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
159 "-*- texinfo -*-\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
160 @deftypefn {Built-in Function} {} strvcat (@var{x})\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
161 @deftypefnx {Built-in Function} {} strvcat (@var{cell_array})\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
162 @deftypefnx {Built-in Function} {} strvcat (@var{s1}, @var{s2}, @dots{})\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
163 Create a character array from one or more numeric matrices, character\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
164 matrices or cell arrays. For numerical input, each element is converted\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
165 to the corresponding ASCII character. The arguments (and elements of\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
166 cell array(s)) are concatenated vertically.\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
167 The returned values are padded with blanks as needed to make each row\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
168 of the string array have the same length. Unlike @code{char}, empty\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
169 strings are removed.\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
170 For example,\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
171 \n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
172 @example\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
173 @group\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
174 strvcat ([97, 98, 99], \"\", @{\"98\", \"99\", 100@}, [\"num\", \"bers\"])\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
175 @result{} [\"abc \"\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
176 \"98 \"\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
177 \"99 \"\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
178 \"d \"\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
179 \"numbers\"]\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
180 @end group\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
181 @end example\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
182 \n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
183 @seealso{char, strcat, cstrcat}\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
184 @end deftypefn") |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
185 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
186 octave_value retval; |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
187 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
188 int nargin = args.length (); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
189 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
190 if (nargin > 0) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
191 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
192 int n_elts = 0; |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
193 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
194 int max_len = 0; |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
195 |
8463
a74871446af7
Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents:
8462
diff
changeset
|
196 std::queue<string_vector> args_as_strings; |
a74871446af7
Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents:
8462
diff
changeset
|
197 |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
198 for (int i = 0; i < nargin; i++) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
199 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
200 string_vector s = args(i).all_strings (); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
201 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
202 if (error_state) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
203 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
204 error ("strvcat: unable to convert some args to strings"); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
205 return retval; |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
206 } |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
207 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
208 int n = s.length (); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
209 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
210 // do not count empty strings in calculation of number of elements |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
211 if (n > 0) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
212 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
213 for (int j = 0; j < n; j++) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
214 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
215 if (s[j].length () > 0) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
216 n_elts++; |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
217 } |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
218 } |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
219 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
220 int s_max_len = s.max_length (); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
221 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
222 if (s_max_len > max_len) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
223 max_len = s_max_len; |
8463
a74871446af7
Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents:
8462
diff
changeset
|
224 |
a74871446af7
Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents:
8462
diff
changeset
|
225 args_as_strings.push (s); |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
226 } |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
227 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
228 string_vector result (n_elts); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
229 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
230 int k = 0; |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
231 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
232 for (int i = 0; i < nargin; i++) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
233 { |
8463
a74871446af7
Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents:
8462
diff
changeset
|
234 string_vector s = args_as_strings.pop (); |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
235 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
236 int n = s.length (); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
237 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
238 if (n > 0) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
239 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
240 for (int j = 0; j < n; j++) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
241 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
242 std::string t = s[j]; |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
243 if (t.length () > 0) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
244 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
245 int t_len = t.length (); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
246 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
247 if (max_len > t_len) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
248 t += std::string (max_len - t_len, ' '); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
249 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
250 result[k++] = t; |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
251 } |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
252 } |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
253 } |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
254 } |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
255 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
256 retval = octave_value (result, '\''); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
257 } |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
258 else |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
259 print_usage (); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
260 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
261 return retval; |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
262 } |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
263 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
264 /* |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
265 %!error <Invalid call to strvcat> strvcat() |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
266 %!assert (strvcat (""), ""); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
267 %!assert (strvcat (100) == "d"); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
268 %!assert (all(strvcat (100,100) == ["d";"d"])); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
269 %!assert (all(strvcat ({100,100}) == ["d";"d"])); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
270 %!assert (all(strvcat ([100,100]) == ["dd"])); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
271 %!assert (all(strvcat ({100,{100}}) == ["d";"d"])); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
272 %!assert (all(strvcat (100, [], 100) == ["d";"d"])) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
273 %!assert (all(strvcat ({100, [], 100}) == ["d";"d"])) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
274 %!assert (all(strvcat ({100,{100, {""}}}) == ["d";"d"])) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
275 %!assert (all(strvcat (["a";"be"], {"c", 100}) == ["a";"be";"c";"d"])) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
276 %!assert(strcmp (strvcat ("a", "bb", "ccc"), ["a "; "bb "; "ccc"])); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
277 */ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
278 |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
279 |
4535 | 280 DEFUN (ischar, args, , |
3361 | 281 "-*- texinfo -*-\n\ |
4535 | 282 @deftypefn {Built-in Function} {} ischar (@var{a})\n\ |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
283 Return 1 if @var{a} is a character array. Otherwise, return 0.\n\ |
3361 | 284 @end deftypefn") |
807 | 285 { |
4233 | 286 octave_value retval; |
807 | 287 |
288 int nargin = args.length (); | |
289 | |
290 if (nargin == 1 && args(0).is_defined ()) | |
4233 | 291 retval = args(0).is_string (); |
807 | 292 else |
5823 | 293 print_usage (); |
807 | 294 |
295 return retval; | |
296 } | |
297 | |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
298 /* |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
299 |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
300 %!assert (ischar ("a"), logical (1)); |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
301 %!assert (ischar (["ab";"cd"]), logical (1)); |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
302 %!assert (ischar ({"ab"}), logical (0)); |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
303 %!assert (ischar (1), logical (0)); |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
304 %!error <Invalid call to ischar.*> ischar (); |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
305 |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
306 */ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
307 |
5415 | 308 DEFUN (strcmp, args, , |
309 "-*- texinfo -*-\n\ | |
6678 | 310 @deftypefn {Built-in Function} {} strcmp (@var{s1}, @var{s2})\n\ |
5415 | 311 Return 1 if the character strings @var{s1} and @var{s2} are the same,\n\ |
312 and 0 otherwise.\n\ | |
5674 | 313 \n\ |
314 If either @var{s1} or @var{s2} is a cell array of strings, then an array\n\ | |
315 of the same size is returned, containing the values described above for\n\ | |
316 every member of the cell array. The other argument may also be a cell\n\ | |
317 array of strings (of the same size or with only one element), char matrix\n\ | |
318 or character string.\n\ | |
319 \n\ | |
320 @strong{Caution:} For compatibility with @sc{Matlab}, Octave's strcmp\n\ | |
321 function returns 1 if the character strings are equal, and 0 otherwise.\n\ | |
322 This is just the opposite of the corresponding C library function.\n\ | |
323 @seealso{strcmpi, strncmp, strncmpi}\n\ | |
5415 | 324 @end deftypefn") |
325 { | |
5531 | 326 octave_value retval; |
5415 | 327 |
5416 | 328 if (args.length () == 2) |
329 { | |
330 bool s1_string = args(0).is_string (); | |
331 bool s1_cell = args(0).is_cell (); | |
332 bool s2_string = args(1).is_string (); | |
333 bool s2_cell = args(1).is_cell (); | |
5415 | 334 |
5416 | 335 if (s1_string && s2_string) |
336 { | |
337 // Must match exactly in all dimensions. | |
338 | |
339 const dim_vector dv1 = args(0).dims (); | |
340 const dim_vector dv2 = args(1).dims (); | |
5415 | 341 |
5416 | 342 if (dv1.length () == dv2.length ()) |
343 { | |
344 for (int i = 0; i < dv1.length (); i++) | |
345 { | |
346 if (dv1(i) != dv2(i)) | |
347 { | |
348 retval = false; | |
349 return retval; | |
350 } | |
351 } | |
5415 | 352 |
5416 | 353 if (dv1(0) == 0) |
354 retval = true; | |
355 else | |
356 { | |
357 charNDArray s1 = args(0).char_array_value (); | |
358 charNDArray s2 = args(1).char_array_value (); | |
5415 | 359 |
5416 | 360 for (int i = 0; i < dv1.numel (); i++) |
361 { | |
362 if (s1(i) != s2(i)) | |
363 { | |
364 retval = false; | |
365 return retval; | |
366 } | |
367 } | |
5415 | 368 |
5416 | 369 retval = true; |
370 } | |
5415 | 371 } |
372 } | |
5416 | 373 else if ((s1_string && s2_cell) || (s1_cell && s2_string)) |
5415 | 374 { |
5416 | 375 string_vector str; |
376 Cell cell; | |
377 int r; | |
5415 | 378 |
5416 | 379 if (s1_string) |
380 { | |
381 str = args(0).all_strings (); | |
382 r = args(0).rows (); | |
383 cell = args(1).cell_value (); | |
384 } | |
385 else | |
386 { | |
387 str = args(1).all_strings (); | |
388 r = args(1).rows (); | |
389 cell = args(0).cell_value (); | |
390 } | |
391 | |
5862 | 392 if (r == 0 || r == 1) |
5416 | 393 { |
394 // Broadcast the string. | |
395 | |
396 boolNDArray output (cell.dimensions); | |
5415 | 397 |
5862 | 398 std::string s = r == 0 ? std::string () : str[0]; |
399 | |
5416 | 400 for (int i = 0; i < cell.length (); i++) |
6250 | 401 { |
402 if (cell(i).is_string ()) | |
403 output(i) = (cell(i).string_value () == s); | |
404 else | |
405 output(i) = false; | |
406 } | |
5416 | 407 |
408 retval = output; | |
409 } | |
410 else if (r > 1) | |
5415 | 411 { |
5416 | 412 if (cell.length () == 1) |
5415 | 413 { |
5416 | 414 // Broadcast the cell. |
415 | |
416 const dim_vector dv (r, 1); | |
417 boolNDArray output (dv); | |
418 | |
419 if (cell(0).is_string ()) | |
420 { | |
421 const std::string str2 = cell(0).string_value (); | |
5415 | 422 |
5416 | 423 for (int i = 0; i < r; i++) |
424 output(i) = (str[i] == str2); | |
425 } | |
426 else | |
427 { | |
428 for (int i = 0; i < r; i++) | |
429 output(i) = false; | |
430 } | |
431 | |
432 retval = output; | |
5415 | 433 } |
434 else | |
435 { | |
5416 | 436 // Must match in all dimensions. |
437 | |
438 boolNDArray output (cell.dimensions); | |
5415 | 439 |
5416 | 440 if (cell.length () == r) |
441 { | |
442 for (int i = 0; i < r; i++) | |
6250 | 443 { |
444 if (cell(i).is_string ()) | |
445 output(i) = (str[i] == cell(i).string_value ()); | |
446 else | |
447 output(i) = false; | |
448 } | |
5415 | 449 |
5416 | 450 retval = output; |
451 } | |
452 else | |
453 retval = false; | |
5415 | 454 } |
455 } | |
456 } | |
5416 | 457 else if (s1_cell && s2_cell) |
5415 | 458 { |
5416 | 459 Cell cell1; |
460 Cell cell2; | |
5415 | 461 |
5416 | 462 int r1 = args(0).numel (); |
463 int r2; | |
464 | |
465 if (r1 == 1) | |
466 { | |
467 // Make the singleton cell2. | |
5415 | 468 |
5416 | 469 cell1 = args(1).cell_value (); |
470 cell2 = args(0).cell_value (); | |
471 r1 = cell1.length (); | |
472 r2 = 1; | |
473 } | |
474 else | |
475 { | |
476 cell1 = args(0).cell_value (); | |
477 cell2 = args(1).cell_value (); | |
478 r2 = cell2.length (); | |
479 } | |
5415 | 480 |
5416 | 481 const dim_vector size1 = cell1.dimensions; |
482 const dim_vector size2 = cell2.dimensions; | |
483 | |
484 boolNDArray output (size1); | |
485 | |
486 if (r2 == 1) | |
487 { | |
488 // Broadcast cell2. | |
489 | |
490 if (! cell2(0).is_string ()) | |
5415 | 491 { |
5416 | 492 for (int i = 0; i < r1; i++) |
493 output(i) = false; | |
5415 | 494 } |
495 else | |
5416 | 496 { |
497 const std::string str2 = cell2(0).string_value (); | |
498 | |
499 for (int i = 0; i < r1; i++) | |
500 { | |
501 if (cell1(i).is_string ()) | |
502 { | |
503 const std::string str1 = cell1(i).string_value (); | |
504 output(i) = (str1 == str2); | |
505 } | |
506 else | |
507 output(i) = false; | |
508 } | |
509 } | |
5415 | 510 } |
5416 | 511 else |
512 { | |
513 if (size1 != size2) | |
514 { | |
515 error ("strcmp: nonconformant cell arrays"); | |
516 return retval; | |
517 } | |
518 | |
519 for (int i = 0; i < r1; i++) | |
520 { | |
521 if (cell1(i).is_string () && cell2(i).is_string ()) | |
522 { | |
523 const std::string str1 = cell1(i).string_value (); | |
524 const std::string str2 = cell2(i).string_value (); | |
525 output(i) = (str1 == str2); | |
526 } | |
527 else | |
528 output(i) = false; | |
529 } | |
530 } | |
531 | |
532 retval = output; | |
5415 | 533 } |
5531 | 534 else |
535 retval = false; | |
5415 | 536 } |
5416 | 537 else |
5823 | 538 print_usage (); |
5415 | 539 |
540 return retval; | |
541 } | |
542 | |
5862 | 543 /* |
544 %!shared x | |
545 %! x = char (zeros (0, 2)); | |
546 %!assert (strcmp ('', x) == false); | |
547 %!assert (strcmp (x, '') == false); | |
548 %!assert (strcmp (x, x) == true); | |
5911 | 549 ## %!assert (strcmp ({''}, x) == false); |
550 ## %!assert (strcmp ({x}, '') == false); | |
551 ## %!assert (strcmp ({x}, x) == true); | |
552 ## %!assert (strcmp ('', {x}) == false); | |
553 ## %!assert (strcmp (x, {''}) == false); | |
554 ## %!assert (strcmp (x, {x}) == true); | |
555 ## %!assert (all (strcmp ({x; x}, '') == [false; false])); | |
556 ## %!assert (all (strcmp ({x; x}, {''}) == [false; false])); | |
557 ## %!assert (all (strcmp ('', {x; x}) == [false; false])); | |
558 ## %!assert (all (strcmp ({''}, {x; x}) == [false; false])); | |
5862 | 559 %!assert (strcmp ({'foo'}, x) == false); |
560 %!assert (strcmp ({'foo'}, 'foo') == true); | |
561 %!assert (strcmp ({'foo'}, x) == false); | |
562 %!assert (strcmp (x, {'foo'}) == false); | |
563 %!assert (strcmp ('foo', {'foo'}) == true); | |
564 %!assert (strcmp (x, {'foo'}) == false); | |
565 %!shared y | |
566 %! y = char (zeros (2, 0)); | |
567 %!assert (strcmp ('', y) == false); | |
568 %!assert (strcmp (y, '') == false); | |
569 %!assert (strcmp (y, y) == true); | |
570 %!assert (all (strcmp ({''}, y) == [true; true])); | |
571 %!assert (strcmp ({y}, '') == true); | |
572 %!assert (all (strcmp ({y}, y) == [true; true])); | |
573 %!assert (all (strcmp ('', {y}) == [true; true])); | |
574 %!assert (all (strcmp (y, {''}) == [true; true])); | |
575 %!assert (all (strcmp (y, {y}) == [true; true])); | |
5911 | 576 ## %!assert (all (strcmp ({y; y}, '') == [false; false])); |
577 ## %!assert (all (strcmp ({y; y}, {''}) == [false; false])); | |
578 ## %!assert (all (strcmp ('', {y; y}) == [false; false])); | |
579 ## %!assert (all (strcmp ({''}, {y; y}) == [false; false])); | |
5862 | 580 %!assert (all (strcmp ({'foo'}, y) == [false; false])); |
581 %!assert (all (strcmp ({'foo'}, y) == [false; false])); | |
582 %!assert (all (strcmp (y, {'foo'}) == [false; false])); | |
583 %!assert (all (strcmp (y, {'foo'}) == [false; false])); | |
584 */ | |
585 | |
6250 | 586 DEFUN (strncmp, args, , |
587 "-*- texinfo -*-\n\ | |
6678 | 588 @deftypefn {Built-in Function} {} strncmp (@var{s1}, @var{s2}, @var{n})\n\ |
6250 | 589 Return 1 if the first @var{n} characters of strings @var{s1} and @var{s2} are the same,\n\ |
590 and 0 otherwise.\n\ | |
591 \n\ | |
592 @example\n\ | |
593 @group\n\ | |
594 strncmp (\"abce\", \"abcd\", 3)\n\ | |
595 @result{} 1\n\ | |
596 @end group\n\ | |
597 @end example\n\ | |
598 \n\ | |
599 If either @var{s1} or @var{s2} is a cell array of strings, then an array\n\ | |
600 of the same size is returned, containing the values described above for\n\ | |
601 every member of the cell array. The other argument may also be a cell\n\ | |
602 array of strings (of the same size or with only one element), char matrix\n\ | |
603 or character string.\n\ | |
604 \n\ | |
605 @example\n\ | |
606 @group\n\ | |
6256 | 607 strncmp (\"abce\", @{\"abcd\", \"bca\", \"abc\"@}, 3)\n\ |
6250 | 608 @result{} [1, 0, 1]\n\ |
609 @end group\n\ | |
610 @end example\n\ | |
611 \n\ | |
612 @strong{Caution:} For compatibility with @sc{Matlab}, Octave's strncmp\n\ | |
613 function returns 1 if the character strings are equal, and 0 otherwise.\n\ | |
614 This is just the opposite of the corresponding C library function.\n\ | |
615 @seealso{strncmpi, strcmp, strcmpi}\n\ | |
616 @end deftypefn") | |
617 { | |
618 octave_value retval; | |
619 | |
620 if (args.length () == 3) | |
621 { | |
622 bool s1_string = args(0).is_string (); | |
623 bool s1_cell = args(0).is_cell (); | |
624 bool s2_string = args(1).is_string (); | |
625 bool s2_cell = args(1).is_cell (); | |
626 | |
627 // Match only first n strings. | |
628 int n = args(2).int_value (); | |
629 | |
630 if (n <= 0) | |
631 { | |
632 error ("strncmp: N must be greater than 0"); | |
633 return retval; | |
634 } | |
635 | |
636 if (s1_string && s2_string) | |
637 { | |
638 // The only restriction here is that each string has equal or | |
639 // greater than n characters | |
640 | |
641 const dim_vector dv1 = args(0).dims (); | |
642 const dim_vector dv2 = args(1).dims (); | |
643 | |
644 if (dv1.numel () >= n && dv2.numel () >= n) | |
645 { | |
646 // Follow Matlab in the sense that the first n characters of | |
647 // the two strings (in column major order) need to be the same. | |
648 charNDArray s1 = args(0).char_array_value (); | |
649 charNDArray s2 = args(1).char_array_value (); | |
650 | |
651 for (int i = 0; i < n; i++) | |
652 { | |
653 if (s1(i) != s2(i)) | |
654 { | |
655 retval = false; | |
656 return retval; | |
657 } | |
658 } | |
659 | |
660 retval = true; | |
661 } | |
662 else | |
663 retval = false; | |
664 } | |
665 else if ((s1_string && s2_cell) || (s1_cell && s2_string)) | |
666 { | |
667 string_vector str; | |
668 Cell cell; | |
669 int r, c; | |
670 | |
671 if (s1_string) | |
672 { | |
673 str = args(0).all_strings (); | |
674 r = args(0).rows (); | |
675 c = args(0).columns (); | |
676 cell = args(1).cell_value (); | |
677 } | |
678 else | |
679 { | |
680 str = args(1).all_strings (); | |
681 r = args(1).rows (); | |
682 c = args(1).columns (); | |
683 cell = args(0).cell_value (); | |
684 } | |
685 | |
686 if (r == 1) | |
687 { | |
688 // Broadcast the string. | |
689 | |
690 boolNDArray output (cell.dimensions); | |
691 | |
692 if (c < n) | |
693 { | |
694 for (int i = 0; i < cell.length (); i++) | |
695 output(i) = false; | |
696 } | |
697 else | |
698 { | |
699 for (int i = 0; i < cell.length (); i++) | |
700 { | |
701 if (cell(i).is_string ()) | |
702 { | |
703 const std::string str2 = cell(i).string_value (); | |
704 | |
705 if (str2.length() >= n) | |
706 { | |
707 if (str2.compare (0, n, str[0], 0, n) == 0) | |
708 output(i) = true; | |
709 else | |
710 output(i) = false; | |
711 } | |
712 else | |
713 output(i) = false; | |
714 } | |
715 } | |
716 } | |
717 | |
718 retval = output; | |
719 } | |
720 else if (r > 1) | |
721 { | |
722 if (cell.length () == 1) | |
723 { | |
724 // Broadcast the cell. | |
725 | |
726 const dim_vector dv (r, 1); | |
727 boolNDArray output (dv); | |
728 | |
729 if (cell(0).is_string () && c >= n) | |
730 { | |
731 const std::string str2 = cell(0).string_value (); | |
732 | |
733 if (str2.length () >= n) | |
734 { | |
735 for (int i = 0; i < r; i++) | |
736 { | |
737 if (str[i].compare (0, n, str2, 0, n) == 0) | |
738 output(i) = true; | |
739 else | |
740 output(i) = false; | |
741 } | |
742 } | |
743 else | |
744 { | |
745 for (int i = 0; i < r; i++) | |
746 output(i) = false; | |
747 } | |
748 } | |
749 else | |
750 { | |
751 for (int i = 0; i < r; i++) | |
752 output(i) = false; | |
753 } | |
754 | |
755 retval = output; | |
756 } | |
757 else | |
758 { | |
759 // Must match in all dimensions. | |
760 | |
761 boolNDArray output (cell.dimensions); | |
762 | |
763 if (cell.numel () == r) | |
764 { | |
765 for (int i = 0; i < r; i++) | |
766 { | |
767 output(i) = false; | |
768 | |
769 if (cell(i).is_string () && c >= n) | |
770 { | |
771 std::string str2 = cell(i).string_value (); | |
772 | |
773 if (str2.length () >= n | |
774 && str2.compare (0, n, str[i], 0, n) == 0) | |
775 output(i) = true; | |
776 } | |
777 } | |
778 | |
779 retval = output; | |
780 } | |
781 else | |
782 { | |
783 error ("strncmp: the number of rows of the string matrix must match the number of elements in the cell"); | |
784 return retval; | |
785 } | |
786 } | |
787 } | |
788 } | |
789 else if (s1_cell && s2_cell) | |
790 { | |
791 Cell cell1; | |
792 Cell cell2; | |
793 | |
794 int r1 = args(0).numel (); | |
795 int r2; | |
796 | |
797 if (r1 == 1) | |
798 { | |
799 // Make the singleton cell2. | |
800 | |
801 cell1 = args(1).cell_value (); | |
802 cell2 = args(0).cell_value (); | |
803 r1 = cell1.length (); | |
804 r2 = 1; | |
805 } | |
806 else | |
807 { | |
808 cell1 = args(0).cell_value (); | |
809 cell2 = args(1).cell_value (); | |
810 r2 = cell2.length (); | |
811 } | |
812 | |
813 const dim_vector size1 = cell1.dimensions; | |
814 const dim_vector size2 = cell2.dimensions; | |
815 | |
816 boolNDArray output (size1); | |
817 | |
818 if (r2 == 1) | |
819 { | |
820 // Broadcast cell2. | |
821 | |
822 if (! cell2(0).is_string ()) | |
823 { | |
824 for (int i = 0; i < r1; i++) | |
825 output(i) = false; | |
826 } | |
827 else | |
828 { | |
829 const std::string str2 = cell2(0).string_value (); | |
830 | |
831 for (int i = 0; i < r1; i++) | |
832 { | |
833 if (cell1(i).is_string ()) | |
834 { | |
835 const std::string str1 = cell1(i).string_value (); | |
836 | |
837 if (str1.length () >= n && str2.length () >= n | |
838 && str1.compare (0, n, str2, 0, n) == 0) | |
839 output(i) = true; | |
840 else | |
841 output(i) = false; | |
842 } | |
843 else | |
844 output(i) = false; | |
845 } | |
846 } | |
847 } | |
848 else | |
849 { | |
850 if (size1 != size2) | |
851 { | |
852 error ("strncmp: nonconformant cell arrays"); | |
853 return retval; | |
854 } | |
855 | |
856 for (int i = 0; i < r1; i++) | |
857 { | |
858 if (cell1(i).is_string () && cell2(i).is_string ()) | |
859 { | |
860 const std::string str1 = cell1(i).string_value (); | |
861 const std::string str2 = cell2(i).string_value (); | |
862 | |
863 if (str1.length () >= n && str2.length () >= n | |
864 && str1.compare (0, n, str2, 0, n) == 0) | |
865 output(i) = true; | |
866 else | |
867 output(i) = false; | |
868 } | |
869 else | |
870 output(i) = false; | |
871 } | |
872 } | |
873 | |
874 retval = output; | |
875 } | |
876 else | |
877 retval = false; | |
878 } | |
879 else | |
880 print_usage (); | |
881 | |
882 return retval; | |
883 } | |
884 | |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
885 /* |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
886 %!error <Invalid call to strncmp.*> strncmp (); |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
887 %!error <Invalid call to strncmp.*> strncmp ("abc", "def"); |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
888 %!assert (strncmp ("abce", "abc", 3) == 1) |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
889 %!assert (strncmp (100, 100, 1) == 0) |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
890 %!assert (all (strncmp ("abce", {"abcd", "bca", "abc"}, 3) == [1, 0, 1])) |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
891 %!assert (all (strncmp ("abc", {"abcd", "bca", "abc"}, 4) == [0, 0, 0])) |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
892 %!assert (all (strncmp ({"abcd", "bca", "abc"},"abce", 3) == [1, 0, 1])) |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
893 %!assert (all (strncmp ({"abcd", "bca", "abc"},{"abcd", "bca", "abe"}, 3) == [1, 1, 0])) |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
894 */ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
895 |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
896 |
5690 | 897 DEFUN (list_in_columns, args, , |
898 "-*- texinfo -*-\n\ | |
899 @deftypefn {Built-in Function} {} list_in_columns (@var{arg}, @var{width})\n\ | |
900 Return a string containing the elements of @var{arg} listed in\n\ | |
901 columns with an overall maximum width of @var{width}. The argument\n\ | |
902 @var{arg} must be a cell array of character strings or a character array.\n\ | |
903 If @var{width} is not specified, the width of the terminal screen is used.\n\ | |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
904 Newline characters are used to break the lines in the output string.\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
905 For example:\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
906 \n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
907 @example\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
908 @group\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
909 list_in_columns (@{\"abc\", \"def\", \"ghijkl\", \"mnop\", \"qrs\", \"tuv\"@}, 20)\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
910 @result{} ans = abc mnop\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
911 def qrs\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
912 ghijkl tuv\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
913 \n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
914 whos ans\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
915 @result{}\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
916 Variables in the current scope:\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
917 \n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
918 Attr Name Size Bytes Class\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
919 ==== ==== ==== ===== =====\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
920 ans 1x37 37 char\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
921 \n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
922 Total is 37 elements using 37 bytes\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
923 @end group\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
924 @end example\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
925 \n\ |
5690 | 926 @seealso{terminal_size}\n\ |
927 @end deftypefn") | |
928 { | |
929 octave_value retval; | |
930 | |
931 int nargin = args.length (); | |
932 | |
933 if (nargin == 1 || nargin == 2) | |
934 { | |
935 string_vector s = args(0).all_strings (); | |
936 | |
937 if (! error_state) | |
938 { | |
5765 | 939 std::ostringstream buf; |
5690 | 940 |
941 if (nargin == 1) | |
942 // Let list_in_columns query terminal width. | |
943 s.list_in_columns (buf); | |
944 else | |
945 { | |
946 int width = args(1).int_value (); | |
947 | |
948 if (! error_state) | |
949 s.list_in_columns (buf, width); | |
950 else | |
951 error ("list_in_columns: expecting width to be an integer"); | |
952 } | |
953 | |
5765 | 954 retval = buf.str (); |
5690 | 955 } |
956 else | |
957 error ("list_in_columns: expecting cellstr or char array"); | |
958 } | |
959 else | |
5823 | 960 print_usage (); |
5690 | 961 |
962 return retval; | |
963 } | |
964 | |
807 | 965 /* |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
966 %!error <Invalid call to list_in_columns.*> list_in_columns (); |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
967 %!error <Invalid call to list_in_columns.*> list_in_columns (["abc", "def"], 20, 2); |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
968 %!error <invalid conversion from string to real scalar.*> list_in_columns (["abc", "def"], "a"); |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
969 %!test |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
970 %! input = {"abc", "def", "ghijkl", "mnop", "qrs", "tuv"}; |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
971 %! result = "abc mnop\ndef qrs\nghijkl tuv\n"; |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
972 %! assert (list_in_columns (input, 20) == result); |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
973 %!test |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
974 %! input = ["abc"; "def"; "ghijkl"; "mnop"; "qrs"; "tuv"]; |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
975 %! result = "abc mnop \ndef qrs \nghijkl tuv \n"; |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
976 %! assert (list_in_columns (input, 20) == result); |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
977 */ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
978 |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
979 /* |
807 | 980 ;;; Local Variables: *** |
981 ;;; mode: C++ *** | |
982 ;;; End: *** | |
983 */ |