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