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