Mercurial > hg > octave-lyh
annotate src/strfns.cc @ 8353:349a555729a9
keep empty strings in char
author | Thorsten Meyer <thorsten.meyier@gmx.de> |
---|---|
date | Sat, 15 Nov 2008 00:58:17 +0100 |
parents | 26d8a92644de |
children | 8dff9cba15fe |
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 |
5765 | 30 #include <sstream> |
807 | 31 |
32 #include "dMatrix.h" | |
33 | |
5416 | 34 #include "Cell.h" |
1355 | 35 #include "defun.h" |
807 | 36 #include "error.h" |
37 #include "gripes.h" | |
2366 | 38 #include "ov.h" |
1355 | 39 #include "oct-obj.h" |
4457 | 40 #include "unwind-prot.h" |
807 | 41 #include "utils.h" |
42 | |
4358 | 43 DEFUN (char, args, , |
44 "-*- texinfo -*-\n\ | |
45 @deftypefn {Built-in Function} {} char (@var{x})\n\ | |
46 @deftypefnx {Built-in Function} {} char (@var{cell_array})\n\ | |
47 @deftypefnx {Built-in Function} {} char (@var{s1}, @var{s2}, @dots{})\n\ | |
48 Create a string array from a numeric matrix, cell array, or list of\n\ | |
49 \n\ | |
50 If the argument is a numeric matrix, each element of the matrix is\n\ | |
51 converted to the corresponding ASCII character. For example,\n\ | |
52 \n\ | |
53 @example\n\ | |
54 @group\n\ | |
4734 | 55 char ([97, 98, 99])\n\ |
4358 | 56 @result{} \"abc\"\n\ |
57 @end group\n\ | |
58 @end example\n\ | |
59 \n\ | |
60 If the argument is a cell array of strings, the result is a string array\n\ | |
61 with each element corresponding to one element of the cell array.\n\ | |
62 \n\ | |
63 For multiple string arguments, the result is a string array with each\n\ | |
64 element corresponding to the arguments.\n\ | |
65 \n\ | |
66 The returned values are padded with blanks as needed to make each row\n\ | |
67 of the string array have the same length.\n\ | |
68 @end deftypefn") | |
69 { | |
70 octave_value retval; | |
71 | |
72 int nargin = args.length (); | |
73 | |
74 if (nargin == 1) | |
5281 | 75 retval = args(0).convert_to_str (true, true, |
76 args(0).is_dq_string () ? '"' : '\''); | |
4358 | 77 else if (nargin > 1) |
78 { | |
79 int n_elts = 0; | |
80 | |
81 int max_len = 0; | |
82 | |
83 for (int i = 0; i < nargin; i++) | |
84 { | |
5707 | 85 string_vector s = args(i).all_strings (); |
4358 | 86 |
87 if (error_state) | |
88 { | |
4457 | 89 error ("char: unable to convert some args to strings"); |
4358 | 90 return retval; |
91 } | |
92 | |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
93 if (s.length () > 0) |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
94 n_elts += s.length (); |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
95 else |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
96 n_elts += 1; |
4358 | 97 |
98 int s_max_len = s.max_length (); | |
99 | |
100 if (s_max_len > max_len) | |
101 max_len = s_max_len; | |
102 } | |
103 | |
104 string_vector result (n_elts); | |
105 | |
106 int k = 0; | |
107 | |
108 for (int i = 0; i < nargin; i++) | |
109 { | |
5707 | 110 string_vector s = args(i).all_strings (); |
4358 | 111 |
112 int n = s.length (); | |
113 | |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
114 if (n > 0) |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
115 { |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
116 for (int j = 0; j < n; j++) |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
117 { |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
118 std::string t = s[j]; |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
119 int t_len = t.length (); |
4358 | 120 |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
121 if (max_len > t_len) |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
122 t += std::string (max_len - t_len, ' '); |
4358 | 123 |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
124 result[k++] = t; |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
125 } |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
126 } |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
127 else |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
128 { |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
129 result[k++] = std::string (max_len, ' '); |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
130 } |
4358 | 131 } |
132 | |
5280 | 133 retval = octave_value (result, '\''); |
4358 | 134 } |
135 else | |
5823 | 136 print_usage (); |
4358 | 137 |
138 return retval; | |
139 } | |
140 | |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
141 /* |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
142 %!error <Invalid call to char> char() |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
143 %!assert (char (100) == "d"); |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
144 %!assert (all(char (100,100) == ["d";"d"])); |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
145 %!assert (all(char ({100,100}) == ["d";"d"])); |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
146 %!assert (all(char ([100,100]) == ["dd"])); |
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}) == ["d";" ";"d"])) |
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 (["a";"be"], {"c", 100}) == ["a";"be";"c";"d"])) |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
152 */ |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
153 |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
154 |
4535 | 155 DEFUN (ischar, args, , |
3361 | 156 "-*- texinfo -*-\n\ |
4535 | 157 @deftypefn {Built-in Function} {} ischar (@var{a})\n\ |
3361 | 158 Return 1 if @var{a} is a string. Otherwise, return 0.\n\ |
159 @end deftypefn") | |
807 | 160 { |
4233 | 161 octave_value retval; |
807 | 162 |
163 int nargin = args.length (); | |
164 | |
165 if (nargin == 1 && args(0).is_defined ()) | |
4233 | 166 retval = args(0).is_string (); |
807 | 167 else |
5823 | 168 print_usage (); |
807 | 169 |
170 return retval; | |
171 } | |
172 | |
5415 | 173 DEFUN (strcmp, args, , |
174 "-*- texinfo -*-\n\ | |
6678 | 175 @deftypefn {Built-in Function} {} strcmp (@var{s1}, @var{s2})\n\ |
5415 | 176 Return 1 if the character strings @var{s1} and @var{s2} are the same,\n\ |
177 and 0 otherwise.\n\ | |
5674 | 178 \n\ |
179 If either @var{s1} or @var{s2} is a cell array of strings, then an array\n\ | |
180 of the same size is returned, containing the values described above for\n\ | |
181 every member of the cell array. The other argument may also be a cell\n\ | |
182 array of strings (of the same size or with only one element), char matrix\n\ | |
183 or character string.\n\ | |
184 \n\ | |
185 @strong{Caution:} For compatibility with @sc{Matlab}, Octave's strcmp\n\ | |
186 function returns 1 if the character strings are equal, and 0 otherwise.\n\ | |
187 This is just the opposite of the corresponding C library function.\n\ | |
188 @seealso{strcmpi, strncmp, strncmpi}\n\ | |
5415 | 189 @end deftypefn") |
190 { | |
5531 | 191 octave_value retval; |
5415 | 192 |
5416 | 193 if (args.length () == 2) |
194 { | |
195 bool s1_string = args(0).is_string (); | |
196 bool s1_cell = args(0).is_cell (); | |
197 bool s2_string = args(1).is_string (); | |
198 bool s2_cell = args(1).is_cell (); | |
5415 | 199 |
5416 | 200 if (s1_string && s2_string) |
201 { | |
202 // Must match exactly in all dimensions. | |
203 | |
204 const dim_vector dv1 = args(0).dims (); | |
205 const dim_vector dv2 = args(1).dims (); | |
5415 | 206 |
5416 | 207 if (dv1.length () == dv2.length ()) |
208 { | |
209 for (int i = 0; i < dv1.length (); i++) | |
210 { | |
211 if (dv1(i) != dv2(i)) | |
212 { | |
213 retval = false; | |
214 return retval; | |
215 } | |
216 } | |
5415 | 217 |
5416 | 218 if (dv1(0) == 0) |
219 retval = true; | |
220 else | |
221 { | |
222 charNDArray s1 = args(0).char_array_value (); | |
223 charNDArray s2 = args(1).char_array_value (); | |
5415 | 224 |
5416 | 225 for (int i = 0; i < dv1.numel (); i++) |
226 { | |
227 if (s1(i) != s2(i)) | |
228 { | |
229 retval = false; | |
230 return retval; | |
231 } | |
232 } | |
5415 | 233 |
5416 | 234 retval = true; |
235 } | |
5415 | 236 } |
237 } | |
5416 | 238 else if ((s1_string && s2_cell) || (s1_cell && s2_string)) |
5415 | 239 { |
5416 | 240 string_vector str; |
241 Cell cell; | |
242 int r; | |
5415 | 243 |
5416 | 244 if (s1_string) |
245 { | |
246 str = args(0).all_strings (); | |
247 r = args(0).rows (); | |
248 cell = args(1).cell_value (); | |
249 } | |
250 else | |
251 { | |
252 str = args(1).all_strings (); | |
253 r = args(1).rows (); | |
254 cell = args(0).cell_value (); | |
255 } | |
256 | |
5862 | 257 if (r == 0 || r == 1) |
5416 | 258 { |
259 // Broadcast the string. | |
260 | |
261 boolNDArray output (cell.dimensions); | |
5415 | 262 |
5862 | 263 std::string s = r == 0 ? std::string () : str[0]; |
264 | |
5416 | 265 for (int i = 0; i < cell.length (); i++) |
6250 | 266 { |
267 if (cell(i).is_string ()) | |
268 output(i) = (cell(i).string_value () == s); | |
269 else | |
270 output(i) = false; | |
271 } | |
5416 | 272 |
273 retval = output; | |
274 } | |
275 else if (r > 1) | |
5415 | 276 { |
5416 | 277 if (cell.length () == 1) |
5415 | 278 { |
5416 | 279 // Broadcast the cell. |
280 | |
281 const dim_vector dv (r, 1); | |
282 boolNDArray output (dv); | |
283 | |
284 if (cell(0).is_string ()) | |
285 { | |
286 const std::string str2 = cell(0).string_value (); | |
5415 | 287 |
5416 | 288 for (int i = 0; i < r; i++) |
289 output(i) = (str[i] == str2); | |
290 } | |
291 else | |
292 { | |
293 for (int i = 0; i < r; i++) | |
294 output(i) = false; | |
295 } | |
296 | |
297 retval = output; | |
5415 | 298 } |
299 else | |
300 { | |
5416 | 301 // Must match in all dimensions. |
302 | |
303 boolNDArray output (cell.dimensions); | |
5415 | 304 |
5416 | 305 if (cell.length () == r) |
306 { | |
307 for (int i = 0; i < r; i++) | |
6250 | 308 { |
309 if (cell(i).is_string ()) | |
310 output(i) = (str[i] == cell(i).string_value ()); | |
311 else | |
312 output(i) = false; | |
313 } | |
5415 | 314 |
5416 | 315 retval = output; |
316 } | |
317 else | |
318 retval = false; | |
5415 | 319 } |
320 } | |
321 } | |
5416 | 322 else if (s1_cell && s2_cell) |
5415 | 323 { |
5416 | 324 Cell cell1; |
325 Cell cell2; | |
5415 | 326 |
5416 | 327 int r1 = args(0).numel (); |
328 int r2; | |
329 | |
330 if (r1 == 1) | |
331 { | |
332 // Make the singleton cell2. | |
5415 | 333 |
5416 | 334 cell1 = args(1).cell_value (); |
335 cell2 = args(0).cell_value (); | |
336 r1 = cell1.length (); | |
337 r2 = 1; | |
338 } | |
339 else | |
340 { | |
341 cell1 = args(0).cell_value (); | |
342 cell2 = args(1).cell_value (); | |
343 r2 = cell2.length (); | |
344 } | |
5415 | 345 |
5416 | 346 const dim_vector size1 = cell1.dimensions; |
347 const dim_vector size2 = cell2.dimensions; | |
348 | |
349 boolNDArray output (size1); | |
350 | |
351 if (r2 == 1) | |
352 { | |
353 // Broadcast cell2. | |
354 | |
355 if (! cell2(0).is_string ()) | |
5415 | 356 { |
5416 | 357 for (int i = 0; i < r1; i++) |
358 output(i) = false; | |
5415 | 359 } |
360 else | |
5416 | 361 { |
362 const std::string str2 = cell2(0).string_value (); | |
363 | |
364 for (int i = 0; i < r1; i++) | |
365 { | |
366 if (cell1(i).is_string ()) | |
367 { | |
368 const std::string str1 = cell1(i).string_value (); | |
369 output(i) = (str1 == str2); | |
370 } | |
371 else | |
372 output(i) = false; | |
373 } | |
374 } | |
5415 | 375 } |
5416 | 376 else |
377 { | |
378 if (size1 != size2) | |
379 { | |
380 error ("strcmp: nonconformant cell arrays"); | |
381 return retval; | |
382 } | |
383 | |
384 for (int i = 0; i < r1; i++) | |
385 { | |
386 if (cell1(i).is_string () && cell2(i).is_string ()) | |
387 { | |
388 const std::string str1 = cell1(i).string_value (); | |
389 const std::string str2 = cell2(i).string_value (); | |
390 output(i) = (str1 == str2); | |
391 } | |
392 else | |
393 output(i) = false; | |
394 } | |
395 } | |
396 | |
397 retval = output; | |
5415 | 398 } |
5531 | 399 else |
400 retval = false; | |
5415 | 401 } |
5416 | 402 else |
5823 | 403 print_usage (); |
5415 | 404 |
405 return retval; | |
406 } | |
407 | |
5862 | 408 /* |
409 %!shared x | |
410 %! x = char (zeros (0, 2)); | |
411 %!assert (strcmp ('', x) == false); | |
412 %!assert (strcmp (x, '') == false); | |
413 %!assert (strcmp (x, x) == true); | |
5911 | 414 ## %!assert (strcmp ({''}, x) == false); |
415 ## %!assert (strcmp ({x}, '') == false); | |
416 ## %!assert (strcmp ({x}, x) == true); | |
417 ## %!assert (strcmp ('', {x}) == false); | |
418 ## %!assert (strcmp (x, {''}) == false); | |
419 ## %!assert (strcmp (x, {x}) == true); | |
420 ## %!assert (all (strcmp ({x; x}, '') == [false; false])); | |
421 ## %!assert (all (strcmp ({x; x}, {''}) == [false; false])); | |
422 ## %!assert (all (strcmp ('', {x; x}) == [false; false])); | |
423 ## %!assert (all (strcmp ({''}, {x; x}) == [false; false])); | |
5862 | 424 %!assert (strcmp ({'foo'}, x) == false); |
425 %!assert (strcmp ({'foo'}, 'foo') == true); | |
426 %!assert (strcmp ({'foo'}, x) == false); | |
427 %!assert (strcmp (x, {'foo'}) == false); | |
428 %!assert (strcmp ('foo', {'foo'}) == true); | |
429 %!assert (strcmp (x, {'foo'}) == false); | |
430 %!shared y | |
431 %! y = char (zeros (2, 0)); | |
432 %!assert (strcmp ('', y) == false); | |
433 %!assert (strcmp (y, '') == false); | |
434 %!assert (strcmp (y, y) == true); | |
435 %!assert (all (strcmp ({''}, y) == [true; true])); | |
436 %!assert (strcmp ({y}, '') == true); | |
437 %!assert (all (strcmp ({y}, y) == [true; true])); | |
438 %!assert (all (strcmp ('', {y}) == [true; true])); | |
439 %!assert (all (strcmp (y, {''}) == [true; true])); | |
440 %!assert (all (strcmp (y, {y}) == [true; true])); | |
5911 | 441 ## %!assert (all (strcmp ({y; y}, '') == [false; false])); |
442 ## %!assert (all (strcmp ({y; y}, {''}) == [false; false])); | |
443 ## %!assert (all (strcmp ('', {y; y}) == [false; false])); | |
444 ## %!assert (all (strcmp ({''}, {y; y}) == [false; false])); | |
5862 | 445 %!assert (all (strcmp ({'foo'}, y) == [false; false])); |
446 %!assert (all (strcmp ({'foo'}, y) == [false; false])); | |
447 %!assert (all (strcmp (y, {'foo'}) == [false; false])); | |
448 %!assert (all (strcmp (y, {'foo'}) == [false; false])); | |
449 */ | |
450 | |
6250 | 451 DEFUN (strncmp, args, , |
452 "-*- texinfo -*-\n\ | |
6678 | 453 @deftypefn {Built-in Function} {} strncmp (@var{s1}, @var{s2}, @var{n})\n\ |
6250 | 454 Return 1 if the first @var{n} characters of strings @var{s1} and @var{s2} are the same,\n\ |
455 and 0 otherwise.\n\ | |
456 \n\ | |
457 @example\n\ | |
458 @group\n\ | |
459 strncmp (\"abce\", \"abcd\", 3)\n\ | |
460 @result{} 1\n\ | |
461 @end group\n\ | |
462 @end example\n\ | |
463 \n\ | |
464 If either @var{s1} or @var{s2} is a cell array of strings, then an array\n\ | |
465 of the same size is returned, containing the values described above for\n\ | |
466 every member of the cell array. The other argument may also be a cell\n\ | |
467 array of strings (of the same size or with only one element), char matrix\n\ | |
468 or character string.\n\ | |
469 \n\ | |
470 @example\n\ | |
471 @group\n\ | |
6256 | 472 strncmp (\"abce\", @{\"abcd\", \"bca\", \"abc\"@}, 3)\n\ |
6250 | 473 @result{} [1, 0, 1]\n\ |
474 @end group\n\ | |
475 @end example\n\ | |
476 \n\ | |
477 @strong{Caution:} For compatibility with @sc{Matlab}, Octave's strncmp\n\ | |
478 function returns 1 if the character strings are equal, and 0 otherwise.\n\ | |
479 This is just the opposite of the corresponding C library function.\n\ | |
480 @seealso{strncmpi, strcmp, strcmpi}\n\ | |
481 @end deftypefn") | |
482 { | |
483 octave_value retval; | |
484 | |
485 if (args.length () == 3) | |
486 { | |
487 bool s1_string = args(0).is_string (); | |
488 bool s1_cell = args(0).is_cell (); | |
489 bool s2_string = args(1).is_string (); | |
490 bool s2_cell = args(1).is_cell (); | |
491 | |
492 // Match only first n strings. | |
493 int n = args(2).int_value (); | |
494 | |
495 if (n <= 0) | |
496 { | |
497 error ("strncmp: N must be greater than 0"); | |
498 return retval; | |
499 } | |
500 | |
501 if (s1_string && s2_string) | |
502 { | |
503 // The only restriction here is that each string has equal or | |
504 // greater than n characters | |
505 | |
506 const dim_vector dv1 = args(0).dims (); | |
507 const dim_vector dv2 = args(1).dims (); | |
508 | |
509 if (dv1.numel () >= n && dv2.numel () >= n) | |
510 { | |
511 // Follow Matlab in the sense that the first n characters of | |
512 // the two strings (in column major order) need to be the same. | |
513 charNDArray s1 = args(0).char_array_value (); | |
514 charNDArray s2 = args(1).char_array_value (); | |
515 | |
516 for (int i = 0; i < n; i++) | |
517 { | |
518 if (s1(i) != s2(i)) | |
519 { | |
520 retval = false; | |
521 return retval; | |
522 } | |
523 } | |
524 | |
525 retval = true; | |
526 } | |
527 else | |
528 retval = false; | |
529 } | |
530 else if ((s1_string && s2_cell) || (s1_cell && s2_string)) | |
531 { | |
532 string_vector str; | |
533 Cell cell; | |
534 int r, c; | |
535 | |
536 if (s1_string) | |
537 { | |
538 str = args(0).all_strings (); | |
539 r = args(0).rows (); | |
540 c = args(0).columns (); | |
541 cell = args(1).cell_value (); | |
542 } | |
543 else | |
544 { | |
545 str = args(1).all_strings (); | |
546 r = args(1).rows (); | |
547 c = args(1).columns (); | |
548 cell = args(0).cell_value (); | |
549 } | |
550 | |
551 if (r == 1) | |
552 { | |
553 // Broadcast the string. | |
554 | |
555 boolNDArray output (cell.dimensions); | |
556 | |
557 if (c < n) | |
558 { | |
559 for (int i = 0; i < cell.length (); i++) | |
560 output(i) = false; | |
561 } | |
562 else | |
563 { | |
564 for (int i = 0; i < cell.length (); i++) | |
565 { | |
566 if (cell(i).is_string ()) | |
567 { | |
568 const std::string str2 = cell(i).string_value (); | |
569 | |
570 if (str2.length() >= n) | |
571 { | |
572 if (str2.compare (0, n, str[0], 0, n) == 0) | |
573 output(i) = true; | |
574 else | |
575 output(i) = false; | |
576 } | |
577 else | |
578 output(i) = false; | |
579 } | |
580 } | |
581 } | |
582 | |
583 retval = output; | |
584 } | |
585 else if (r > 1) | |
586 { | |
587 if (cell.length () == 1) | |
588 { | |
589 // Broadcast the cell. | |
590 | |
591 const dim_vector dv (r, 1); | |
592 boolNDArray output (dv); | |
593 | |
594 if (cell(0).is_string () && c >= n) | |
595 { | |
596 const std::string str2 = cell(0).string_value (); | |
597 | |
598 if (str2.length () >= n) | |
599 { | |
600 for (int i = 0; i < r; i++) | |
601 { | |
602 if (str[i].compare (0, n, str2, 0, n) == 0) | |
603 output(i) = true; | |
604 else | |
605 output(i) = false; | |
606 } | |
607 } | |
608 else | |
609 { | |
610 for (int i = 0; i < r; i++) | |
611 output(i) = false; | |
612 } | |
613 } | |
614 else | |
615 { | |
616 for (int i = 0; i < r; i++) | |
617 output(i) = false; | |
618 } | |
619 | |
620 retval = output; | |
621 } | |
622 else | |
623 { | |
624 // Must match in all dimensions. | |
625 | |
626 boolNDArray output (cell.dimensions); | |
627 | |
628 if (cell.numel () == r) | |
629 { | |
630 for (int i = 0; i < r; i++) | |
631 { | |
632 output(i) = false; | |
633 | |
634 if (cell(i).is_string () && c >= n) | |
635 { | |
636 std::string str2 = cell(i).string_value (); | |
637 | |
638 if (str2.length () >= n | |
639 && str2.compare (0, n, str[i], 0, n) == 0) | |
640 output(i) = true; | |
641 } | |
642 } | |
643 | |
644 retval = output; | |
645 } | |
646 else | |
647 { | |
648 error ("strncmp: the number of rows of the string matrix must match the number of elements in the cell"); | |
649 return retval; | |
650 } | |
651 } | |
652 } | |
653 } | |
654 else if (s1_cell && s2_cell) | |
655 { | |
656 Cell cell1; | |
657 Cell cell2; | |
658 | |
659 int r1 = args(0).numel (); | |
660 int r2; | |
661 | |
662 if (r1 == 1) | |
663 { | |
664 // Make the singleton cell2. | |
665 | |
666 cell1 = args(1).cell_value (); | |
667 cell2 = args(0).cell_value (); | |
668 r1 = cell1.length (); | |
669 r2 = 1; | |
670 } | |
671 else | |
672 { | |
673 cell1 = args(0).cell_value (); | |
674 cell2 = args(1).cell_value (); | |
675 r2 = cell2.length (); | |
676 } | |
677 | |
678 const dim_vector size1 = cell1.dimensions; | |
679 const dim_vector size2 = cell2.dimensions; | |
680 | |
681 boolNDArray output (size1); | |
682 | |
683 if (r2 == 1) | |
684 { | |
685 // Broadcast cell2. | |
686 | |
687 if (! cell2(0).is_string ()) | |
688 { | |
689 for (int i = 0; i < r1; i++) | |
690 output(i) = false; | |
691 } | |
692 else | |
693 { | |
694 const std::string str2 = cell2(0).string_value (); | |
695 | |
696 for (int i = 0; i < r1; i++) | |
697 { | |
698 if (cell1(i).is_string ()) | |
699 { | |
700 const std::string str1 = cell1(i).string_value (); | |
701 | |
702 if (str1.length () >= n && str2.length () >= n | |
703 && str1.compare (0, n, str2, 0, n) == 0) | |
704 output(i) = true; | |
705 else | |
706 output(i) = false; | |
707 } | |
708 else | |
709 output(i) = false; | |
710 } | |
711 } | |
712 } | |
713 else | |
714 { | |
715 if (size1 != size2) | |
716 { | |
717 error ("strncmp: nonconformant cell arrays"); | |
718 return retval; | |
719 } | |
720 | |
721 for (int i = 0; i < r1; i++) | |
722 { | |
723 if (cell1(i).is_string () && cell2(i).is_string ()) | |
724 { | |
725 const std::string str1 = cell1(i).string_value (); | |
726 const std::string str2 = cell2(i).string_value (); | |
727 | |
728 if (str1.length () >= n && str2.length () >= n | |
729 && str1.compare (0, n, str2, 0, n) == 0) | |
730 output(i) = true; | |
731 else | |
732 output(i) = false; | |
733 } | |
734 else | |
735 output(i) = false; | |
736 } | |
737 } | |
738 | |
739 retval = output; | |
740 } | |
741 else | |
742 retval = false; | |
743 } | |
744 else | |
745 print_usage (); | |
746 | |
747 return retval; | |
748 } | |
749 | |
5690 | 750 DEFUN (list_in_columns, args, , |
751 "-*- texinfo -*-\n\ | |
752 @deftypefn {Built-in Function} {} list_in_columns (@var{arg}, @var{width})\n\ | |
753 Return a string containing the elements of @var{arg} listed in\n\ | |
754 columns with an overall maximum width of @var{width}. The argument\n\ | |
755 @var{arg} must be a cell array of character strings or a character array.\n\ | |
756 If @var{width} is not specified, the width of the terminal screen is used.\n\ | |
757 @seealso{terminal_size}\n\ | |
758 @end deftypefn") | |
759 { | |
760 octave_value retval; | |
761 | |
762 int nargin = args.length (); | |
763 | |
764 if (nargin == 1 || nargin == 2) | |
765 { | |
766 string_vector s = args(0).all_strings (); | |
767 | |
768 if (! error_state) | |
769 { | |
5765 | 770 std::ostringstream buf; |
5690 | 771 |
772 if (nargin == 1) | |
773 // Let list_in_columns query terminal width. | |
774 s.list_in_columns (buf); | |
775 else | |
776 { | |
777 int width = args(1).int_value (); | |
778 | |
779 if (! error_state) | |
780 s.list_in_columns (buf, width); | |
781 else | |
782 error ("list_in_columns: expecting width to be an integer"); | |
783 } | |
784 | |
5765 | 785 retval = buf.str (); |
5690 | 786 } |
787 else | |
788 error ("list_in_columns: expecting cellstr or char array"); | |
789 } | |
790 else | |
5823 | 791 print_usage (); |
5690 | 792 |
793 return retval; | |
794 } | |
795 | |
807 | 796 /* |
797 ;;; Local Variables: *** | |
798 ;;; mode: C++ *** | |
799 ;;; End: *** | |
800 */ |