Mercurial > hg > octave-lyh
annotate src/strfns.cc @ 9036:58604c45ca74
Cleanup of data types related documentation
Files: data.texi, numbers.texi, strings.texi, container.texi
Use two spaces after a period for increased readibility in Info browser
Spellcheck
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Sat, 21 Mar 2009 15:52:41 -0700 |
parents | eb63fbe60fab |
children | 7c02ec148a3c |
rev | line source |
---|---|
807 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1994, 1995, 1996, 1997, 1999, 2002, 2003, 2004, 2005, |
8920 | 4 2006, 2007, 2008, 2009 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 { | |
8464
cb48e594a441
Fchar, Fstrvcat: use queue correctly
John W. Eaton <jwe@octave.org>
parents:
8463
diff
changeset
|
115 string_vector s = args_as_strings.front (); |
cb48e594a441
Fchar, Fstrvcat: use queue correctly
John W. Eaton <jwe@octave.org>
parents:
8463
diff
changeset
|
116 args_as_strings.pop (); |
4358 | 117 |
118 int n = s.length (); | |
119 | |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
120 if (n > 0) |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
121 { |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
122 for (int j = 0; j < n; j++) |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
123 { |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
124 std::string t = s[j]; |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
125 int t_len = t.length (); |
4358 | 126 |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
127 if (max_len > t_len) |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
128 t += std::string (max_len - t_len, ' '); |
4358 | 129 |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
130 result[k++] = t; |
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 } |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
133 else |
8463
a74871446af7
Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents:
8462
diff
changeset
|
134 result[k++] = std::string (max_len, ' '); |
4358 | 135 } |
136 | |
5280 | 137 retval = octave_value (result, '\''); |
4358 | 138 } |
139 else | |
5823 | 140 print_usage (); |
4358 | 141 |
142 return retval; | |
143 } | |
144 | |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
145 /* |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
146 %!error <Invalid call to char> char() |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
147 %!assert (char (100) == "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}) == ["d";"d"])); |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
150 %!assert (all(char ([100,100]) == ["dd"])); |
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 ({100,{100, {""}}}) == ["d";"d";" "])) |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
155 %!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
|
156 %!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
|
157 */ |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
158 |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
159 DEFUN (strvcat, args, , |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
160 "-*- texinfo -*-\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
161 @deftypefn {Built-in Function} {} strvcat (@var{x})\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
162 @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
|
163 @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
|
164 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
|
165 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
|
166 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
|
167 cell array(s)) are concatenated vertically.\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
168 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
|
169 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
|
170 strings are removed.\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
171 For example,\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
172 \n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
173 @example\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
174 @group\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
175 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
|
176 @result{} [\"abc \"\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
177 \"98 \"\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
178 \"99 \"\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
179 \"d \"\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
180 \"numbers\"]\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
181 @end group\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
182 @end example\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
183 \n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
184 @seealso{char, strcat, cstrcat}\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
185 @end deftypefn") |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
186 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
187 octave_value retval; |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
188 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
189 int nargin = args.length (); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
190 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
191 if (nargin > 0) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
192 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
193 int n_elts = 0; |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
194 |
8809
732cb0236488
strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents:
8524
diff
changeset
|
195 size_t max_len = 0; |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
196 |
8463
a74871446af7
Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents:
8462
diff
changeset
|
197 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
|
198 |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
199 for (int i = 0; i < nargin; i++) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
200 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
201 string_vector s = args(i).all_strings (); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
202 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
203 if (error_state) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
204 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
205 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
|
206 return retval; |
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 |
8809
732cb0236488
strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents:
8524
diff
changeset
|
209 size_t n = s.length (); |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
210 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
211 // 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
|
212 if (n > 0) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
213 { |
8809
732cb0236488
strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents:
8524
diff
changeset
|
214 for (size_t j = 0; j < n; j++) |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
215 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
216 if (s[j].length () > 0) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
217 n_elts++; |
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 |
8809
732cb0236488
strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents:
8524
diff
changeset
|
221 size_t s_max_len = s.max_length (); |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
222 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
223 if (s_max_len > max_len) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
224 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
|
225 |
a74871446af7
Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents:
8462
diff
changeset
|
226 args_as_strings.push (s); |
8462
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 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
229 string_vector result (n_elts); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
230 |
8809
732cb0236488
strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents:
8524
diff
changeset
|
231 octave_idx_type k = 0; |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
232 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
233 for (int i = 0; i < nargin; i++) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
234 { |
8464
cb48e594a441
Fchar, Fstrvcat: use queue correctly
John W. Eaton <jwe@octave.org>
parents:
8463
diff
changeset
|
235 string_vector s = args_as_strings.front (); |
cb48e594a441
Fchar, Fstrvcat: use queue correctly
John W. Eaton <jwe@octave.org>
parents:
8463
diff
changeset
|
236 args_as_strings.pop (); |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
237 |
8809
732cb0236488
strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents:
8524
diff
changeset
|
238 size_t n = s.length (); |
8462
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 if (n > 0) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
241 { |
8809
732cb0236488
strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents:
8524
diff
changeset
|
242 for (size_t j = 0; j < n; j++) |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
243 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
244 std::string t = s[j]; |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
245 if (t.length () > 0) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
246 { |
8809
732cb0236488
strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents:
8524
diff
changeset
|
247 size_t t_len = t.length (); |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
248 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
249 if (max_len > t_len) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
250 t += std::string (max_len - t_len, ' '); |
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 result[k++] = t; |
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 } |
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 retval = octave_value (result, '\''); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
259 } |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
260 else |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
261 print_usage (); |
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 return retval; |
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 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
266 /* |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
267 %!error <Invalid call to strvcat> strvcat() |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
268 %!assert (strvcat (""), ""); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
269 %!assert (strvcat (100) == "d"); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
270 %!assert (all(strvcat (100,100) == ["d";"d"])); |
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]) == ["dd"])); |
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 ({100, [], 100}) == ["d";"d"])) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
276 %!assert (all(strvcat ({100,{100, {""}}}) == ["d";"d"])) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
277 %!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
|
278 %!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
|
279 */ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
280 |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
281 |
4535 | 282 DEFUN (ischar, args, , |
3361 | 283 "-*- texinfo -*-\n\ |
4535 | 284 @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
|
285 Return 1 if @var{a} is a character array. Otherwise, return 0.\n\ |
3361 | 286 @end deftypefn") |
807 | 287 { |
4233 | 288 octave_value retval; |
807 | 289 |
290 int nargin = args.length (); | |
291 | |
292 if (nargin == 1 && args(0).is_defined ()) | |
4233 | 293 retval = args(0).is_string (); |
807 | 294 else |
5823 | 295 print_usage (); |
807 | 296 |
297 return retval; | |
298 } | |
299 | |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
300 /* |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
301 |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
302 %!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
|
303 %!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
|
304 %!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
|
305 %!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
|
306 %!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
|
307 |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
308 */ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
309 |
5415 | 310 DEFUN (strcmp, args, , |
311 "-*- texinfo -*-\n\ | |
6678 | 312 @deftypefn {Built-in Function} {} strcmp (@var{s1}, @var{s2})\n\ |
5415 | 313 Return 1 if the character strings @var{s1} and @var{s2} are the same,\n\ |
314 and 0 otherwise.\n\ | |
5674 | 315 \n\ |
316 If either @var{s1} or @var{s2} is a cell array of strings, then an array\n\ | |
317 of the same size is returned, containing the values described above for\n\ | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
318 every member of the cell array. The other argument may also be a cell\n\ |
5674 | 319 array of strings (of the same size or with only one element), char matrix\n\ |
320 or character string.\n\ | |
321 \n\ | |
322 @strong{Caution:} For compatibility with @sc{Matlab}, Octave's strcmp\n\ | |
323 function returns 1 if the character strings are equal, and 0 otherwise.\n\ | |
324 This is just the opposite of the corresponding C library function.\n\ | |
325 @seealso{strcmpi, strncmp, strncmpi}\n\ | |
5415 | 326 @end deftypefn") |
327 { | |
5531 | 328 octave_value retval; |
5415 | 329 |
5416 | 330 if (args.length () == 2) |
331 { | |
332 bool s1_string = args(0).is_string (); | |
333 bool s1_cell = args(0).is_cell (); | |
334 bool s2_string = args(1).is_string (); | |
335 bool s2_cell = args(1).is_cell (); | |
5415 | 336 |
5416 | 337 if (s1_string && s2_string) |
338 { | |
339 // Must match exactly in all dimensions. | |
340 | |
341 const dim_vector dv1 = args(0).dims (); | |
342 const dim_vector dv2 = args(1).dims (); | |
5415 | 343 |
5416 | 344 if (dv1.length () == dv2.length ()) |
345 { | |
8809
732cb0236488
strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents:
8524
diff
changeset
|
346 for (octave_idx_type i = 0; i < dv1.length (); i++) |
5416 | 347 { |
348 if (dv1(i) != dv2(i)) | |
349 { | |
350 retval = false; | |
351 return retval; | |
352 } | |
353 } | |
5415 | 354 |
5416 | 355 if (dv1(0) == 0) |
356 retval = true; | |
357 else | |
358 { | |
8825
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
359 const charNDArray s1 = args(0).char_array_value (); |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
360 const charNDArray s2 = args(1).char_array_value (); |
5415 | 361 |
8809
732cb0236488
strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents:
8524
diff
changeset
|
362 for (octave_idx_type i = 0; i < dv1.numel (); i++) |
5416 | 363 { |
364 if (s1(i) != s2(i)) | |
365 { | |
366 retval = false; | |
367 return retval; | |
368 } | |
369 } | |
5415 | 370 |
5416 | 371 retval = true; |
372 } | |
5415 | 373 } |
374 } | |
5416 | 375 else if ((s1_string && s2_cell) || (s1_cell && s2_string)) |
5415 | 376 { |
8825
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
377 octave_value str_val, cell_val; |
5415 | 378 |
5416 | 379 if (s1_string) |
380 { | |
8825
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
381 str_val = args (0); |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
382 cell_val = args (1); |
5416 | 383 } |
384 else | |
385 { | |
8825
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
386 str_val = args (1); |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
387 cell_val = args (0); |
5416 | 388 } |
389 | |
8825
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
390 const Cell cell = cell_val.cell_value (); |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
391 const string_vector str = str_val.all_strings (); |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
392 octave_idx_type r = str.rows (); |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
393 |
5862 | 394 if (r == 0 || r == 1) |
5416 | 395 { |
396 // Broadcast the string. | |
397 | |
8825
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
398 boolNDArray output (cell_val.dims (), false); |
5415 | 399 |
5862 | 400 std::string s = r == 0 ? std::string () : str[0]; |
401 | |
8825
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
402 if (cell_val.is_cellstr ()) |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
403 { |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
404 const Array<std::string> cellstr = cell_val.cellstr_value (); |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
405 for (octave_idx_type i = 0; i < cellstr.length (); i++) |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
406 output(i) = cellstr(i) == s; |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
407 } |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
408 else |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
409 { |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
410 // FIXME: should we warn here? |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
411 for (octave_idx_type i = 0; i < cell.length (); i++) |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
412 { |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
413 if (cell(i).is_string ()) |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
414 output(i) = (cell(i).string_value () == s); |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
415 } |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
416 } |
5416 | 417 |
418 retval = output; | |
419 } | |
420 else if (r > 1) | |
5415 | 421 { |
5416 | 422 if (cell.length () == 1) |
5415 | 423 { |
5416 | 424 // Broadcast the cell. |
425 | |
426 const dim_vector dv (r, 1); | |
8472
5451f7460ea6
strfns.cc (Fstrcmp, Fstrncmp): initialize all elements of output with constructor arg
John W. Eaton <jwe@octave.org>
parents:
8464
diff
changeset
|
427 boolNDArray output (dv, false); |
5416 | 428 |
429 if (cell(0).is_string ()) | |
430 { | |
431 const std::string str2 = cell(0).string_value (); | |
5415 | 432 |
8809
732cb0236488
strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents:
8524
diff
changeset
|
433 for (octave_idx_type i = 0; i < r; i++) |
5416 | 434 output(i) = (str[i] == str2); |
435 } | |
436 | |
437 retval = output; | |
5415 | 438 } |
439 else | |
440 { | |
5416 | 441 // Must match in all dimensions. |
442 | |
8524
937921654627
clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents:
8472
diff
changeset
|
443 boolNDArray output (cell.dims (), false); |
5415 | 444 |
5416 | 445 if (cell.length () == r) |
446 { | |
8825
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
447 if (cell_val.is_cellstr ()) |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
448 { |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
449 const Array<std::string> cellstr = cell_val.cellstr_value (); |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
450 for (octave_idx_type i = 0; i < cellstr.length (); i++) |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
451 output(i) = str[i] == cellstr(i); |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
452 } |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
453 else |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
454 { |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
455 // FIXME: should we warn here? |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
456 for (octave_idx_type i = 0; i < r; i++) |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
457 { |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
458 if (cell(i).is_string ()) |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
459 output(i) = (str[i] == cell(i).string_value ()); |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
460 } |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
461 } |
5415 | 462 |
5416 | 463 retval = output; |
464 } | |
465 else | |
466 retval = false; | |
5415 | 467 } |
468 } | |
469 } | |
5416 | 470 else if (s1_cell && s2_cell) |
5415 | 471 { |
8825
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
472 octave_value cell1_val, cell2_val; |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
473 octave_idx_type r1 = args(0).numel (), r2; |
5416 | 474 |
475 if (r1 == 1) | |
476 { | |
477 // Make the singleton cell2. | |
5415 | 478 |
8825
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
479 cell1_val = args(1); |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
480 cell2_val = args(0); |
5416 | 481 } |
482 else | |
483 { | |
8825
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
484 cell1_val = args(0); |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
485 cell2_val = args(1); |
5416 | 486 } |
5415 | 487 |
8825
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
488 const Cell cell1 = cell1_val.cell_value (); |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
489 const Cell cell2 = cell2_val.cell_value (); |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
490 r1 = cell1.numel (); |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
491 r2 = cell2.numel (); |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
492 |
8524
937921654627
clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents:
8472
diff
changeset
|
493 const dim_vector size1 = cell1.dims (); |
937921654627
clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents:
8472
diff
changeset
|
494 const dim_vector size2 = cell2.dims (); |
5416 | 495 |
8472
5451f7460ea6
strfns.cc (Fstrcmp, Fstrncmp): initialize all elements of output with constructor arg
John W. Eaton <jwe@octave.org>
parents:
8464
diff
changeset
|
496 boolNDArray output (size1, false); |
5416 | 497 |
498 if (r2 == 1) | |
499 { | |
500 // Broadcast cell2. | |
501 | |
8472
5451f7460ea6
strfns.cc (Fstrcmp, Fstrncmp): initialize all elements of output with constructor arg
John W. Eaton <jwe@octave.org>
parents:
8464
diff
changeset
|
502 if (cell2(0).is_string ()) |
5416 | 503 { |
504 const std::string str2 = cell2(0).string_value (); | |
505 | |
8825
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
506 if (cell1_val.is_cellstr ()) |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
507 { |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
508 const Array<std::string> cellstr = cell1_val.cellstr_value (); |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
509 for (octave_idx_type i = 0; i < cellstr.length (); i++) |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
510 output(i) = cellstr(i) == str2; |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
511 } |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
512 else |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
513 { |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
514 // FIXME: should we warn here? |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
515 for (octave_idx_type i = 0; i < r1; i++) |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
516 { |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
517 if (cell1(i).is_string ()) |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
518 { |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
519 const std::string str1 = cell1(i).string_value (); |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
520 output(i) = (str1 == str2); |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
521 } |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
522 } |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
523 } |
5416 | 524 } |
5415 | 525 } |
5416 | 526 else |
527 { | |
528 if (size1 != size2) | |
529 { | |
530 error ("strcmp: nonconformant cell arrays"); | |
531 return retval; | |
532 } | |
533 | |
8825
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
534 if (cell1.is_cellstr () && cell2.is_cellstr ()) |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
535 { |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
536 const Array<std::string> cellstr1 = cell1_val.cellstr_value (); |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
537 const Array<std::string> cellstr2 = cell2_val.cellstr_value (); |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
538 for (octave_idx_type i = 0; i < r1; i++) |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
539 output (i) = cellstr1(i) == cellstr2(i); |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
540 } |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
541 else |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
542 { |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
543 // FIXME: should we warn here? |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
544 for (octave_idx_type i = 0; i < r1; i++) |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
545 { |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
546 if (cell1(i).is_string () && cell2(i).is_string ()) |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
547 { |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
548 const std::string str1 = cell1(i).string_value (); |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
549 const std::string str2 = cell2(i).string_value (); |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
550 output(i) = (str1 == str2); |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
551 } |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
552 } |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8809
diff
changeset
|
553 } |
5416 | 554 } |
555 | |
556 retval = output; | |
5415 | 557 } |
5531 | 558 else |
559 retval = false; | |
5415 | 560 } |
5416 | 561 else |
5823 | 562 print_usage (); |
5415 | 563 |
564 return retval; | |
565 } | |
566 | |
5862 | 567 /* |
568 %!shared x | |
569 %! x = char (zeros (0, 2)); | |
570 %!assert (strcmp ('', x) == false); | |
571 %!assert (strcmp (x, '') == false); | |
572 %!assert (strcmp (x, x) == true); | |
5911 | 573 ## %!assert (strcmp ({''}, x) == false); |
574 ## %!assert (strcmp ({x}, '') == false); | |
575 ## %!assert (strcmp ({x}, x) == true); | |
576 ## %!assert (strcmp ('', {x}) == false); | |
577 ## %!assert (strcmp (x, {''}) == false); | |
578 ## %!assert (strcmp (x, {x}) == true); | |
579 ## %!assert (all (strcmp ({x; x}, '') == [false; false])); | |
580 ## %!assert (all (strcmp ({x; x}, {''}) == [false; false])); | |
581 ## %!assert (all (strcmp ('', {x; x}) == [false; false])); | |
582 ## %!assert (all (strcmp ({''}, {x; x}) == [false; false])); | |
5862 | 583 %!assert (strcmp ({'foo'}, x) == false); |
584 %!assert (strcmp ({'foo'}, 'foo') == true); | |
585 %!assert (strcmp ({'foo'}, x) == false); | |
586 %!assert (strcmp (x, {'foo'}) == false); | |
587 %!assert (strcmp ('foo', {'foo'}) == true); | |
588 %!assert (strcmp (x, {'foo'}) == false); | |
589 %!shared y | |
590 %! y = char (zeros (2, 0)); | |
591 %!assert (strcmp ('', y) == false); | |
592 %!assert (strcmp (y, '') == false); | |
593 %!assert (strcmp (y, y) == true); | |
594 %!assert (all (strcmp ({''}, y) == [true; true])); | |
595 %!assert (strcmp ({y}, '') == true); | |
596 %!assert (all (strcmp ({y}, y) == [true; true])); | |
597 %!assert (all (strcmp ('', {y}) == [true; true])); | |
598 %!assert (all (strcmp (y, {''}) == [true; true])); | |
599 %!assert (all (strcmp (y, {y}) == [true; true])); | |
5911 | 600 ## %!assert (all (strcmp ({y; y}, '') == [false; false])); |
601 ## %!assert (all (strcmp ({y; y}, {''}) == [false; false])); | |
602 ## %!assert (all (strcmp ('', {y; y}) == [false; false])); | |
603 ## %!assert (all (strcmp ({''}, {y; y}) == [false; false])); | |
5862 | 604 %!assert (all (strcmp ({'foo'}, y) == [false; false])); |
605 %!assert (all (strcmp ({'foo'}, y) == [false; false])); | |
606 %!assert (all (strcmp (y, {'foo'}) == [false; false])); | |
607 %!assert (all (strcmp (y, {'foo'}) == [false; false])); | |
608 */ | |
609 | |
6250 | 610 DEFUN (strncmp, args, , |
611 "-*- texinfo -*-\n\ | |
6678 | 612 @deftypefn {Built-in Function} {} strncmp (@var{s1}, @var{s2}, @var{n})\n\ |
6250 | 613 Return 1 if the first @var{n} characters of strings @var{s1} and @var{s2} are the same,\n\ |
614 and 0 otherwise.\n\ | |
615 \n\ | |
616 @example\n\ | |
617 @group\n\ | |
618 strncmp (\"abce\", \"abcd\", 3)\n\ | |
619 @result{} 1\n\ | |
620 @end group\n\ | |
621 @end example\n\ | |
622 \n\ | |
623 If either @var{s1} or @var{s2} is a cell array of strings, then an array\n\ | |
624 of the same size is returned, containing the values described above for\n\ | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
625 every member of the cell array. The other argument may also be a cell\n\ |
6250 | 626 array of strings (of the same size or with only one element), char matrix\n\ |
627 or character string.\n\ | |
628 \n\ | |
629 @example\n\ | |
630 @group\n\ | |
6256 | 631 strncmp (\"abce\", @{\"abcd\", \"bca\", \"abc\"@}, 3)\n\ |
6250 | 632 @result{} [1, 0, 1]\n\ |
633 @end group\n\ | |
634 @end example\n\ | |
635 \n\ | |
636 @strong{Caution:} For compatibility with @sc{Matlab}, Octave's strncmp\n\ | |
637 function returns 1 if the character strings are equal, and 0 otherwise.\n\ | |
638 This is just the opposite of the corresponding C library function.\n\ | |
639 @seealso{strncmpi, strcmp, strcmpi}\n\ | |
640 @end deftypefn") | |
641 { | |
642 octave_value retval; | |
643 | |
644 if (args.length () == 3) | |
645 { | |
646 bool s1_string = args(0).is_string (); | |
647 bool s1_cell = args(0).is_cell (); | |
648 bool s2_string = args(1).is_string (); | |
649 bool s2_cell = args(1).is_cell (); | |
650 | |
651 // Match only first n strings. | |
8809
732cb0236488
strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents:
8524
diff
changeset
|
652 int n = args(2).int_value (); |
6250 | 653 |
654 if (n <= 0) | |
655 { | |
656 error ("strncmp: N must be greater than 0"); | |
657 return retval; | |
658 } | |
659 | |
660 if (s1_string && s2_string) | |
661 { | |
662 // The only restriction here is that each string has equal or | |
663 // greater than n characters | |
664 | |
665 const dim_vector dv1 = args(0).dims (); | |
666 const dim_vector dv2 = args(1).dims (); | |
667 | |
668 if (dv1.numel () >= n && dv2.numel () >= n) | |
669 { | |
670 // Follow Matlab in the sense that the first n characters of | |
671 // the two strings (in column major order) need to be the same. | |
672 charNDArray s1 = args(0).char_array_value (); | |
673 charNDArray s2 = args(1).char_array_value (); | |
674 | |
675 for (int i = 0; i < n; i++) | |
676 { | |
677 if (s1(i) != s2(i)) | |
678 { | |
679 retval = false; | |
680 return retval; | |
681 } | |
682 } | |
683 | |
684 retval = true; | |
685 } | |
686 else | |
687 retval = false; | |
688 } | |
689 else if ((s1_string && s2_cell) || (s1_cell && s2_string)) | |
690 { | |
691 string_vector str; | |
692 Cell cell; | |
8809
732cb0236488
strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents:
8524
diff
changeset
|
693 octave_idx_type r, c; |
6250 | 694 |
695 if (s1_string) | |
696 { | |
697 str = args(0).all_strings (); | |
698 r = args(0).rows (); | |
699 c = args(0).columns (); | |
700 cell = args(1).cell_value (); | |
701 } | |
702 else | |
703 { | |
704 str = args(1).all_strings (); | |
705 r = args(1).rows (); | |
706 c = args(1).columns (); | |
707 cell = args(0).cell_value (); | |
708 } | |
709 | |
710 if (r == 1) | |
711 { | |
712 // Broadcast the string. | |
713 | |
8524
937921654627
clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents:
8472
diff
changeset
|
714 boolNDArray output (cell.dims (), false); |
6250 | 715 |
716 if (c < n) | |
717 { | |
8809
732cb0236488
strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents:
8524
diff
changeset
|
718 for (octave_idx_type i = 0; i < cell.length (); i++) |
6250 | 719 output(i) = false; |
720 } | |
721 else | |
722 { | |
8809
732cb0236488
strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents:
8524
diff
changeset
|
723 for (octave_idx_type i = 0; i < cell.length (); i++) |
6250 | 724 { |
725 if (cell(i).is_string ()) | |
726 { | |
727 const std::string str2 = cell(i).string_value (); | |
728 | |
8809
732cb0236488
strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents:
8524
diff
changeset
|
729 if (str2.length () >= n |
8472
5451f7460ea6
strfns.cc (Fstrcmp, Fstrncmp): initialize all elements of output with constructor arg
John W. Eaton <jwe@octave.org>
parents:
8464
diff
changeset
|
730 && str2.compare (0, n, str[0], 0, n) == 0) |
6250 | 731 output(i) = true; |
732 } | |
733 } | |
734 } | |
735 | |
736 retval = output; | |
737 } | |
738 else if (r > 1) | |
739 { | |
740 if (cell.length () == 1) | |
741 { | |
742 // Broadcast the cell. | |
743 | |
744 const dim_vector dv (r, 1); | |
8472
5451f7460ea6
strfns.cc (Fstrcmp, Fstrncmp): initialize all elements of output with constructor arg
John W. Eaton <jwe@octave.org>
parents:
8464
diff
changeset
|
745 boolNDArray output (dv, false); |
6250 | 746 |
747 if (cell(0).is_string () && c >= n) | |
748 { | |
749 const std::string str2 = cell(0).string_value (); | |
750 | |
751 if (str2.length () >= n) | |
752 { | |
8809
732cb0236488
strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents:
8524
diff
changeset
|
753 for (octave_idx_type i = 0; i < r; i++) |
6250 | 754 { |
755 if (str[i].compare (0, n, str2, 0, n) == 0) | |
756 output(i) = true; | |
757 } | |
758 } | |
759 } | |
760 | |
761 retval = output; | |
762 } | |
763 else | |
764 { | |
765 // Must match in all dimensions. | |
766 | |
8524
937921654627
clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents:
8472
diff
changeset
|
767 boolNDArray output (cell.dims (), false); |
6250 | 768 |
769 if (cell.numel () == r) | |
770 { | |
8809
732cb0236488
strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents:
8524
diff
changeset
|
771 for (octave_idx_type i = 0; i < r; i++) |
6250 | 772 { |
773 if (cell(i).is_string () && c >= n) | |
774 { | |
775 std::string str2 = cell(i).string_value (); | |
776 | |
777 if (str2.length () >= n | |
778 && str2.compare (0, n, str[i], 0, n) == 0) | |
779 output(i) = true; | |
780 } | |
781 } | |
782 | |
783 retval = output; | |
784 } | |
785 else | |
786 { | |
787 error ("strncmp: the number of rows of the string matrix must match the number of elements in the cell"); | |
788 return retval; | |
789 } | |
790 } | |
791 } | |
792 } | |
793 else if (s1_cell && s2_cell) | |
794 { | |
795 Cell cell1; | |
796 Cell cell2; | |
797 | |
8809
732cb0236488
strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents:
8524
diff
changeset
|
798 octave_idx_type r1 = args(0).numel (); |
732cb0236488
strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents:
8524
diff
changeset
|
799 octave_idx_type r2; |
6250 | 800 |
801 if (r1 == 1) | |
802 { | |
803 // Make the singleton cell2. | |
804 | |
805 cell1 = args(1).cell_value (); | |
806 cell2 = args(0).cell_value (); | |
807 r1 = cell1.length (); | |
808 r2 = 1; | |
809 } | |
810 else | |
811 { | |
812 cell1 = args(0).cell_value (); | |
813 cell2 = args(1).cell_value (); | |
814 r2 = cell2.length (); | |
815 } | |
816 | |
8524
937921654627
clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents:
8472
diff
changeset
|
817 const dim_vector size1 = cell1.dims (); |
937921654627
clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents:
8472
diff
changeset
|
818 const dim_vector size2 = cell2.dims (); |
6250 | 819 |
8472
5451f7460ea6
strfns.cc (Fstrcmp, Fstrncmp): initialize all elements of output with constructor arg
John W. Eaton <jwe@octave.org>
parents:
8464
diff
changeset
|
820 boolNDArray output (size1, false); |
6250 | 821 |
822 if (r2 == 1) | |
823 { | |
824 // Broadcast cell2. | |
825 | |
8472
5451f7460ea6
strfns.cc (Fstrcmp, Fstrncmp): initialize all elements of output with constructor arg
John W. Eaton <jwe@octave.org>
parents:
8464
diff
changeset
|
826 if (cell2(0).is_string ()) |
6250 | 827 { |
828 const std::string str2 = cell2(0).string_value (); | |
829 | |
8809
732cb0236488
strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents:
8524
diff
changeset
|
830 for (octave_idx_type i = 0; i < r1; i++) |
6250 | 831 { |
832 if (cell1(i).is_string ()) | |
833 { | |
834 const std::string str1 = cell1(i).string_value (); | |
835 | |
836 if (str1.length () >= n && str2.length () >= n | |
837 && str1.compare (0, n, str2, 0, n) == 0) | |
838 output(i) = true; | |
839 } | |
840 } | |
841 } | |
842 } | |
843 else | |
844 { | |
845 if (size1 != size2) | |
846 { | |
847 error ("strncmp: nonconformant cell arrays"); | |
848 return retval; | |
849 } | |
850 | |
8809
732cb0236488
strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents:
8524
diff
changeset
|
851 for (octave_idx_type i = 0; i < r1; i++) |
6250 | 852 { |
853 if (cell1(i).is_string () && cell2(i).is_string ()) | |
854 { | |
855 const std::string str1 = cell1(i).string_value (); | |
856 const std::string str2 = cell2(i).string_value (); | |
857 | |
858 if (str1.length () >= n && str2.length () >= n | |
859 && str1.compare (0, n, str2, 0, n) == 0) | |
860 output(i) = true; | |
861 } | |
862 } | |
863 } | |
864 | |
865 retval = output; | |
866 } | |
867 else | |
868 retval = false; | |
869 } | |
870 else | |
871 print_usage (); | |
872 | |
873 return retval; | |
874 } | |
875 | |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
876 /* |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
877 %!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
|
878 %!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
|
879 %!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
|
880 %!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
|
881 %!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
|
882 %!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
|
883 %!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
|
884 %!assert (all (strncmp ({"abcd", "bca", "abc"},{"abcd", "bca", "abe"}, 3) == [1, 1, 0])) |
8472
5451f7460ea6
strfns.cc (Fstrcmp, Fstrncmp): initialize all elements of output with constructor arg
John W. Eaton <jwe@octave.org>
parents:
8464
diff
changeset
|
885 %!assert (all (strncmp("abc", {"abcd", 10}, 2) == [1, 0])) |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
886 */ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
887 |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
888 |
5690 | 889 DEFUN (list_in_columns, args, , |
890 "-*- texinfo -*-\n\ | |
891 @deftypefn {Built-in Function} {} list_in_columns (@var{arg}, @var{width})\n\ | |
892 Return a string containing the elements of @var{arg} listed in\n\ | |
893 columns with an overall maximum width of @var{width}. The argument\n\ | |
894 @var{arg} must be a cell array of character strings or a character array.\n\ | |
895 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
|
896 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
|
897 For example:\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
898 \n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
899 @example\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
900 @group\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
901 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
|
902 @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
|
903 def qrs\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
904 ghijkl tuv\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
905 \n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
906 whos ans\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
907 @result{}\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
908 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
|
909 \n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
910 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
|
911 ==== ==== ==== ===== =====\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
912 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
|
913 \n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
914 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
|
915 @end group\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
916 @end example\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
917 \n\ |
5690 | 918 @seealso{terminal_size}\n\ |
919 @end deftypefn") | |
920 { | |
921 octave_value retval; | |
922 | |
923 int nargin = args.length (); | |
924 | |
925 if (nargin == 1 || nargin == 2) | |
926 { | |
927 string_vector s = args(0).all_strings (); | |
928 | |
929 if (! error_state) | |
930 { | |
5765 | 931 std::ostringstream buf; |
5690 | 932 |
933 if (nargin == 1) | |
934 // Let list_in_columns query terminal width. | |
935 s.list_in_columns (buf); | |
936 else | |
937 { | |
938 int width = args(1).int_value (); | |
939 | |
940 if (! error_state) | |
941 s.list_in_columns (buf, width); | |
942 else | |
943 error ("list_in_columns: expecting width to be an integer"); | |
944 } | |
945 | |
5765 | 946 retval = buf.str (); |
5690 | 947 } |
948 else | |
949 error ("list_in_columns: expecting cellstr or char array"); | |
950 } | |
951 else | |
5823 | 952 print_usage (); |
5690 | 953 |
954 return retval; | |
955 } | |
956 | |
807 | 957 /* |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
958 %!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
|
959 %!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
|
960 %!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
|
961 %!test |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
962 %! 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
|
963 %! 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
|
964 %! 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
|
965 %!test |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
966 %! 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
|
967 %! 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
|
968 %! 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
|
969 */ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
970 |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
971 /* |
807 | 972 ;;; Local Variables: *** |
973 ;;; mode: C++ *** | |
974 ;;; End: *** | |
975 */ |