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