GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
strfns.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1994-2017 John W. Eaton
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 3 of the License, or (at your
10 option) any 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, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <cctype>
28 
29 #include <queue>
30 #include <sstream>
31 
32 #include "dMatrix.h"
33 
34 #include "Cell.h"
35 #include "defun.h"
36 #include "error.h"
37 #include "errwarn.h"
38 #include "ov.h"
39 #include "ovl.h"
40 #include "unwind-prot.h"
41 #include "utils.h"
42 
43 #include "oct-string.h"
44 
45 DEFUN (char, args, ,
46  doc: /* -*- texinfo -*-
47 @deftypefn {} {} char (@var{x})
48 @deftypefnx {} {} char (@var{x}, @dots{})
49 @deftypefnx {} {} char (@var{s1}, @var{s2}, @dots{})
50 @deftypefnx {} {} char (@var{cell_array})
51 Create a string array from one or more numeric matrices, character
52 matrices, or cell arrays.
53 
54 Arguments are concatenated vertically. The returned values are padded with
55 blanks as needed to make each row of the string array have the same length.
56 Empty input strings are significant and will concatenated in the output.
57 
58 For numerical input, each element is converted to the corresponding ASCII
59 character. A range error results if an input is outside the ASCII range
60 (0-255).
61 
62 For cell arrays, each element is concatenated separately. Cell arrays
63 converted through @code{char} can mostly be converted back with
64 @code{cellstr}. For example:
65 
66 @example
67 @group
68 char ([97, 98, 99], "", @{"98", "99", 100@}, "str1", ["ha", "lf"])
69  @result{} ["abc "
70  " "
71  "98 "
72  "99 "
73  "d "
74  "str1 "
75  "half "]
76 @end group
77 @end example
78 @seealso{strvcat, cellstr}
79 @end deftypefn */)
80 {
82 
83  int nargin = args.length ();
84 
85  if (nargin == 0)
86  retval = "";
87  else if (nargin == 1)
88  retval = args(0).convert_to_str (true, true,
89  args(0).is_dq_string () ? '"' : '\'');
90  else
91  {
92  int n_elts = 0;
93 
94  int max_len = 0;
95 
96  std::queue<string_vector> args_as_strings;
97 
98  for (int i = 0; i < nargin; i++)
99  {
100  string_vector s = args(i).xstring_vector_value ("char: unable to convert some args to strings");
101 
102  if (s.numel () > 0)
103  n_elts += s.numel ();
104  else
105  n_elts += 1;
106 
107  int s_max_len = s.max_length ();
108 
109  if (s_max_len > max_len)
110  max_len = s_max_len;
111 
112  args_as_strings.push (s);
113  }
114 
115  string_vector result (n_elts);
116 
117  int k = 0;
118 
119  for (int i = 0; i < nargin; i++)
120  {
121  string_vector s = args_as_strings.front ();
122  args_as_strings.pop ();
123 
124  int n = s.numel ();
125 
126  if (n > 0)
127  {
128  for (int j = 0; j < n; j++)
129  {
130  std::string t = s[j];
131  int t_len = t.length ();
132 
133  if (max_len > t_len)
134  t += std::string (max_len - t_len, ' ');
135 
136  result[k++] = t;
137  }
138  }
139  else
140  result[k++] = std::string (max_len, ' ');
141  }
142 
143  retval = octave_value (result, '\'');
144  }
145 
146  return retval;
147 }
148 
149 /*
150 %!assert (char (), '')
151 %!assert (char (100), "d")
152 %!assert (char (100,100), ["d";"d"])
153 %!assert (char ({100,100}), ["d";"d"])
154 %!assert (char ([100,100]), ["dd"])
155 %!assert (char ({100,{100}}), ["d";"d"])
156 %!assert (char (100, [], 100), ["d";" ";"d"])
157 %!assert (char ({100, [], 100}), ["d";" ";"d"])
158 %!assert (char ({100,{100, {""}}}), ["d";"d";" "])
159 %!assert (char (["a";"be"], {"c", 100}), ["a";"be";"c";"d"])
160 %!assert (char ("a", "bb", "ccc"), ["a "; "bb "; "ccc"])
161 %!assert (char ([65, 83, 67, 73, 73]), "ASCII")
162 
163 %!test
164 %! x = char ("foo", "bar", "foobar");
165 %! assert (x(1,:), "foo ");
166 %! assert (x(2,:), "bar ");
167 %! assert (x(3,:), "foobar");
168 */
169 
170 DEFUN (strvcat, args, ,
171  doc: /* -*- texinfo -*-
172 @deftypefn {} {} strvcat (@var{x})
173 @deftypefnx {} {} strvcat (@var{x}, @dots{})
174 @deftypefnx {} {} strvcat (@var{s1}, @var{s2}, @dots{})
175 @deftypefnx {} {} strvcat (@var{cell_array})
176 Create a character array from one or more numeric matrices, character
177 matrices, or cell arrays.
178 
179 Arguments are concatenated vertically. The returned values are padded with
180 blanks as needed to make each row of the string array have the same length.
181 Unlike @code{char}, empty strings are removed and will not appear in the
182 output.
183 
184 For numerical input, each element is converted to the corresponding ASCII
185 character. A range error results if an input is outside the ASCII range
186 (0-255).
187 
188 For cell arrays, each element is concatenated separately. Cell arrays
189 converted through @code{strvcat} can mostly be converted back with
190 @code{cellstr}. For example:
191 
192 @example
193 @group
194 strvcat ([97, 98, 99], "", @{"98", "99", 100@}, "str1", ["ha", "lf"])
195  @result{} ["abc "
196  "98 "
197  "99 "
198  "d "
199  "str1 "
200  "half "]
201 @end group
202 @end example
203 @seealso{char, strcat, cstrcat}
204 @end deftypefn */)
205 {
206  int nargin = args.length ();
207  int n_elts = 0;
208  size_t max_len = 0;
209  std::queue<string_vector> args_as_strings;
210 
211  for (int i = 0; i < nargin; i++)
212  {
213  string_vector s = args(i).xstring_vector_value ("strvcat: unable to convert some args to strings");
214 
215  size_t n = s.numel ();
216 
217  // do not count empty strings in calculation of number of elements
218  if (n > 0)
219  {
220  for (size_t j = 0; j < n; j++)
221  {
222  if (s[j].length () > 0)
223  n_elts++;
224  }
225  }
226 
227  size_t s_max_len = s.max_length ();
228 
229  if (s_max_len > max_len)
230  max_len = s_max_len;
231 
232  args_as_strings.push (s);
233  }
234 
235  string_vector result (n_elts);
236 
237  octave_idx_type k = 0;
238 
239  for (int i = 0; i < nargin; i++)
240  {
241  string_vector s = args_as_strings.front ();
242  args_as_strings.pop ();
243 
244  size_t n = s.numel ();
245 
246  if (n > 0)
247  {
248  for (size_t j = 0; j < n; j++)
249  {
250  std::string t = s[j];
251  if (t.length () > 0)
252  {
253  size_t t_len = t.length ();
254 
255  if (max_len > t_len)
256  t += std::string (max_len - t_len, ' ');
257 
258  result[k++] = t;
259  }
260  }
261  }
262  }
263 
264  // Cannot use ovl. Relies on overloaded octave_value call.
265  return octave_value (result, '\'');
266 }
267 
268 /*
269 %!assert (strvcat (""), "")
270 %!assert (strvcat (100) == "d")
271 %!assert (strvcat (100,100), ["d";"d"])
272 %!assert (strvcat ({100,100}), ["d";"d"])
273 %!assert (strvcat ([100,100]), ["dd"])
274 %!assert (strvcat ({100,{100}}), ["d";"d"])
275 %!assert (strvcat (100, [], 100), ["d";"d"])
276 %!assert (strvcat ({100, [], 100}), ["d";"d"])
277 %!assert (strvcat ({100,{100, {""}}}), ["d";"d"])
278 %!assert (strvcat (["a";"be"], {"c", 100}), ["a";"be";"c";"d"])
279 %!assert (strvcat ("a", "bb", "ccc"), ["a "; "bb "; "ccc"])
280 %!assert (strvcat (), "")
281 */
282 
283 DEFUN (ischar, args, ,
284  doc: /* -*- texinfo -*-
285 @deftypefn {} {} ischar (@var{x})
286 Return true if @var{x} is a character array.
287 @seealso{isfloat, isinteger, islogical, isnumeric, iscellstr, isa}
288 @end deftypefn */)
289 {
290  if (args.length () != 1)
291  print_usage ();
292 
293  return ovl (args(0).is_string ());
294 }
295 
296 /*
297 %!assert (ischar ("a"), true)
298 %!assert (ischar (["ab";"cd"]), true)
299 %!assert (ischar ({"ab"}), false)
300 %!assert (ischar (1), false)
301 %!assert (ischar ([1, 2]), false)
302 %!assert (ischar ([]), false)
303 %!assert (ischar ([1, 2; 3, 4]), false)
304 %!assert (ischar (""), true)
305 %!assert (ischar ("test"), true)
306 %!assert (ischar (["test"; "ing"]), true)
307 %!assert (ischar (struct ("foo", "bar")), false)
308 
309 %!error ischar ()
310 %!error ischar ("test", 1)
311 */
312 
313 static octave_value
314 do_strcmp_fun (const octave_value& arg0, const octave_value& arg1,
315  octave_idx_type n, const char *fcn_name,
316  bool (*array_op) (const Array<char>&, const Array<char>&,
318  bool (*str_op) (const std::string&, const std::string&,
319  std::string::size_type))
320 
321 {
323 
324  bool s1_string = arg0.is_string ();
325  bool s1_cell = arg0.is_cell ();
326  bool s2_string = arg1.is_string ();
327  bool s2_cell = arg1.is_cell ();
328 
329  if (s1_string && s2_string)
330  retval = array_op (arg0.char_array_value (), arg1.char_array_value (), n);
331  else if ((s1_string && s2_cell) || (s1_cell && s2_string))
332  {
333  octave_value str_val, cell_val;
334 
335  if (s1_string)
336  {
337  str_val = arg0;
338  cell_val = arg1;
339  }
340  else
341  {
342  str_val = arg1;
343  cell_val = arg0;
344  }
345 
346  const Cell cell = cell_val.cell_value ();
347  const string_vector str = str_val.string_vector_value ();
348  octave_idx_type r = str.numel ();
349 
350  if (r == 0 || r == 1)
351  {
352  // Broadcast the string.
353 
354  boolNDArray output (cell_val.dims (), false);
355 
356  std::string s = r == 0 ? "" : str[0];
357 
358  if (cell_val.is_cellstr ())
359  {
360  const Array<std::string> cellstr = cell_val.cellstr_value ();
361  for (octave_idx_type i = 0; i < cellstr.numel (); i++)
362  output(i) = str_op (cellstr(i), s, n);
363  }
364  else
365  {
366  // FIXME: should we warn here?
367  for (octave_idx_type i = 0; i < cell.numel (); i++)
368  {
369  if (cell(i).is_string ())
370  output(i) = str_op (cell(i).string_value (), s, n);
371  }
372  }
373 
374  retval = output;
375  }
376  else if (r > 1)
377  {
378  if (cell.numel () == 1)
379  {
380  // Broadcast the cell.
381 
382  const dim_vector dv (r, 1);
383  boolNDArray output (dv, false);
384 
385  if (cell(0).is_string ())
386  {
387  const std::string str2 = cell(0).string_value ();
388 
389  for (octave_idx_type i = 0; i < r; i++)
390  output(i) = str_op (str[i], str2, n);
391  }
392 
393  retval = output;
394  }
395  else
396  {
397  // Must match in all dimensions.
398 
399  boolNDArray output (cell.dims (), false);
400 
401  if (cell.numel () == r)
402  {
403  if (cell_val.is_cellstr ())
404  {
405  const Array<std::string> cellstr
406  = cell_val.cellstr_value ();
407  for (octave_idx_type i = 0; i < cellstr.numel (); i++)
408  output(i) = str_op (str[i], cellstr(i), n);
409  }
410  else
411  {
412  // FIXME: should we warn here?
413  for (octave_idx_type i = 0; i < r; i++)
414  {
415  if (cell(i).is_string ())
416  output(i) = str_op (str[i],
417  cell(i).string_value (), n);
418  }
419  }
420 
421  retval = output;
422  }
423  else
424  retval = false;
425  }
426  }
427  }
428  else if (s1_cell && s2_cell)
429  {
430  octave_value cell1_val, cell2_val;
431  octave_idx_type r1 = arg0.numel (), r2;
432 
433  if (r1 == 1)
434  {
435  // Make the singleton cell2.
436 
437  cell1_val = arg1;
438  cell2_val = arg0;
439  }
440  else
441  {
442  cell1_val = arg0;
443  cell2_val = arg1;
444  }
445 
446  const Cell cell1 = cell1_val.cell_value ();
447  const Cell cell2 = cell2_val.cell_value ();
448  r1 = cell1.numel ();
449  r2 = cell2.numel ();
450 
451  const dim_vector size1 = cell1.dims ();
452  const dim_vector size2 = cell2.dims ();
453 
454  boolNDArray output (size1, false);
455 
456  if (r2 == 1)
457  {
458  // Broadcast cell2.
459 
460  if (cell2(0).is_string ())
461  {
462  const std::string str2 = cell2(0).string_value ();
463 
464  if (cell1_val.is_cellstr ())
465  {
466  const Array<std::string> cellstr = cell1_val.cellstr_value ();
467  for (octave_idx_type i = 0; i < cellstr.numel (); i++)
468  output(i) = str_op (cellstr(i), str2, n);
469  }
470  else
471  {
472  // FIXME: should we warn here?
473  for (octave_idx_type i = 0; i < r1; i++)
474  {
475  if (cell1(i).is_string ())
476  {
477  const std::string str1 = cell1(i).string_value ();
478  output(i) = str_op (str1, str2, n);
479  }
480  }
481  }
482  }
483  }
484  else
485  {
486  if (size1 != size2)
487  error ("%s: nonconformant cell arrays", fcn_name);
488 
489  if (cell1.is_cellstr () && cell2.is_cellstr ())
490  {
491  const Array<std::string> cellstr1 = cell1_val.cellstr_value ();
492  const Array<std::string> cellstr2 = cell2_val.cellstr_value ();
493  for (octave_idx_type i = 0; i < r1; i++)
494  output (i) = str_op (cellstr1(i), cellstr2(i), n);
495  }
496  else
497  {
498  // FIXME: should we warn here?
499  for (octave_idx_type i = 0; i < r1; i++)
500  {
501  if (cell1(i).is_string () && cell2(i).is_string ())
502  {
503  const std::string str1 = cell1(i).string_value ();
504  const std::string str2 = cell2(i).string_value ();
505  output(i) = str_op (str1, str2, n);
506  }
507  }
508  }
509  }
510 
511  retval = output;
512  }
513  else
514  retval = false;
515 
516  return retval;
517 }
518 
519 
520 // These are required so that they match the same signature as strncmp
521 // and strncmpi and can therefore be used in do_strcmp_fun.
522 
523 template <typename T, typename T_size_type>
524 static bool
525 strcmp_ignore_n (const T& s1, const T& s2, T_size_type)
526 { return octave::string::strcmp (s1, s2); }
527 
528 template <typename T, typename T_size_type>
529 static bool
530 strcmpi_ignore_n (const T& s1, const T& s2, T_size_type)
531 { return octave::string::strcmpi (s1, s2); }
532 
533 
534 DEFUN (strcmp, args, ,
535  doc: /* -*- texinfo -*-
536 @deftypefn {} {} strcmp (@var{s1}, @var{s2})
537 Return 1 if the character strings @var{s1} and @var{s2} are the same,
538 and 0 otherwise.
539 
540 If either @var{s1} or @var{s2} is a cell array of strings, then an array
541 of the same size is returned, containing the values described above for
542 every member of the cell array. The other argument may also be a cell
543 array of strings (of the same size or with only one element), char matrix
544 or character string.
545 
546 @strong{Caution:} For compatibility with @sc{matlab}, Octave's strcmp
547 function returns 1 if the character strings are equal, and 0 otherwise.
548 This is just the opposite of the corresponding C library function.
549 @seealso{strcmpi, strncmp, strncmpi}
550 @end deftypefn */)
551 {
552  if (args.length () != 2)
553  print_usage ();
554 
555  return ovl (do_strcmp_fun (args(0), args(1), 0, "strcmp",
556  strcmp_ignore_n, strcmp_ignore_n));
557 }
558 
559 /*
560 %!shared x
561 %! x = char (zeros (0, 2));
562 %!assert (strcmp ("", x), false)
563 %!assert (strcmp (x, ""), false)
564 %!assert (strcmp (x, x), true)
565 ## %!assert (strcmp ({""}, x), true)
566 ## %!assert (strcmp ({x}, ""), false)
567 ## %!assert (strcmp ({x}, x), true)
568 ## %!assert (strcmp ("", {x}), false)
569 ## %!assert (strcmp (x, {""}), false)
570 ## %!assert (strcmp (x, {x}), true)
571 ## %!assert (strcmp ({x; x}, ""), [false; false])
572 ## %!assert (strcmp ({x; x}, {""}), [false; false])
573 ## %!assert (strcmp ("", {x; x}), [false; false])
574 ## %!assert (strcmp ({""}, {x; x}), [false; false])
575 %!assert (strcmp ({"foo"}, x), false)
576 %!assert (strcmp ({"foo"}, "foo"), true)
577 %!assert (strcmp ({"foo"}, x), false)
578 %!assert (strcmp (x, {"foo"}), false)
579 %!assert (strcmp ("foo", {"foo"}), true)
580 %!assert (strcmp (x, {"foo"}), false)
581 %!shared y
582 %! y = char (zeros (2, 0));
583 %!assert (strcmp ("", y), false)
584 %!assert (strcmp (y, ""), false)
585 %!assert (strcmp (y, y), true)
586 %!assert (strcmp ({""}, y), [true; true])
587 %!assert (strcmp ({y}, ""), true)
588 %!assert (strcmp ({y}, y), [true; true])
589 %!assert (strcmp ("", {y}), true)
590 %!assert (strcmp (y, {""}), [true; true])
591 %!assert (strcmp (y, {y}), [true; true])
592 %!assert (strcmp ({y; y}, ""), [true; true])
593 %!assert (strcmp ({y; y}, {""}), [true; true])
594 %!assert (strcmp ("", {y; y}), [true; true])
595 %!assert (strcmp ({""}, {y; y}), [true; true])
596 %!assert (strcmp ({"foo"}, y), [false; false])
597 %!assert (strcmp ({"foo"}, y), [false; false])
598 %!assert (strcmp (y, {"foo"}), [false; false])
599 %!assert (strcmp (y, {"foo"}), [false; false])
600 %!assert (strcmp ("foobar", "foobar"), true)
601 %!assert (strcmp ("fooba", "foobar"), false)
602 
603 %!error strcmp ()
604 %!error strcmp ("foo", "bar", 3)
605 */
606 
607 DEFUN (strncmp, args, ,
608  doc: /* -*- texinfo -*-
609 @deftypefn {} {} strncmp (@var{s1}, @var{s2}, @var{n})
610 Return 1 if the first @var{n} characters of strings @var{s1} and @var{s2}
611 are the same, and 0 otherwise.
612 
613 @example
614 @group
615 strncmp ("abce", "abcd", 3)
616  @result{} 1
617 @end group
618 @end example
619 
620 If either @var{s1} or @var{s2} is a cell array of strings, then an array
621 of the same size is returned, containing the values described above for
622 every member of the cell array. The other argument may also be a cell
623 array of strings (of the same size or with only one element), char matrix
624 or character string.
625 
626 @example
627 @group
628 strncmp ("abce", @{"abcd", "bca", "abc"@}, 3)
629  @result{} [1, 0, 1]
630 @end group
631 @end example
632 
633 @strong{Caution:} For compatibility with @sc{matlab}, Octave's strncmp
634 function returns 1 if the character strings are equal, and 0 otherwise.
635 This is just the opposite of the corresponding C library function.
636 @seealso{strncmpi, strcmp, strcmpi}
637 @end deftypefn */)
638 {
639  if (args.length () != 3)
640  print_usage ();
641 
642  octave_idx_type n = args(2).idx_type_value ();
643 
644  if (n > 0)
645  return ovl (do_strcmp_fun (args(0), args(1), n, "strncmp",
648  else
649  error ("strncmp: N must be greater than 0");
650 }
651 
652 /*
653 %!assert (strncmp ("abce", "abc", 3), true)
654 %!assert (strncmp (100, 100, 1), false)
655 %!assert (strncmp ("abce", {"abcd", "bca", "abc"}, 3), logical ([1, 0, 1]))
656 %!assert (strncmp ("abc", {"abcd", "bca", "abc"}, 4), logical ([0, 0, 0]))
657 %!assert (strncmp ({"abcd", "bca", "abc"},"abce", 3), logical ([1, 0, 1]))
658 %!assert (strncmp ({"abcd", "bca", "abc"},{"abcd", "bca", "abe"}, 3), logical ([1, 1, 0]))
659 %!assert (strncmp ("abc", {"abcd", 10}, 2), logical ([1, 0]))
660 
661 %!error strncmp ()
662 %!error strncmp ("abc", "def")
663 */
664 
665 DEFUNX ("strcmpi", Fstrcmpi, args, ,
666  doc: /* -*- texinfo -*-
667 @deftypefn {} {} strcmpi (@var{s1}, @var{s2})
668 Return 1 if the character strings @var{s1} and @var{s2} are the same,
669 disregarding case of alphabetic characters, and 0 otherwise.
670 
671 If either @var{s1} or @var{s2} is a cell array of strings, then an array
672 of the same size is returned, containing the values described above for
673 every member of the cell array. The other argument may also be a cell
674 array of strings (of the same size or with only one element), char matrix
675 or character string.
676 
677 @strong{Caution:} For compatibility with @sc{matlab}, Octave's strcmp
678 function returns 1 if the character strings are equal, and 0 otherwise.
679 This is just the opposite of the corresponding C library function.
680 
681 @strong{Caution:} National alphabets are not supported.
682 @seealso{strcmp, strncmp, strncmpi}
683 @end deftypefn */)
684 {
685  if (args.length () != 2)
686  print_usage ();
687 
688  return ovl (do_strcmp_fun (args(0), args(1), 0, "strcmpi",
689  strcmpi_ignore_n, strcmpi_ignore_n));
690 }
691 
692 /*
693 %!assert (strcmpi ("abc123", "ABC123"), true)
694 */
695 
696 DEFUNX ("strncmpi", Fstrncmpi, args, ,
697  doc: /* -*- texinfo -*-
698 @deftypefn {} {} strncmpi (@var{s1}, @var{s2}, @var{n})
699 Return 1 if the first @var{n} character of @var{s1} and @var{s2} are the
700 same, disregarding case of alphabetic characters, and 0 otherwise.
701 
702 If either @var{s1} or @var{s2} is a cell array of strings, then an array
703 of the same size is returned, containing the values described above for
704 every member of the cell array. The other argument may also be a cell
705 array of strings (of the same size or with only one element), char matrix
706 or character string.
707 
708 @strong{Caution:} For compatibility with @sc{matlab}, Octave's strncmpi
709 function returns 1 if the character strings are equal, and 0 otherwise.
710 This is just the opposite of the corresponding C library function.
711 
712 @strong{Caution:} National alphabets are not supported.
713 @seealso{strncmp, strcmp, strcmpi}
714 @end deftypefn */)
715 {
716  if (args.length () != 3)
717  print_usage ();
718 
719  octave_idx_type n = args(2).idx_type_value ();
720 
721  if (n > 0)
722  return ovl (do_strcmp_fun (args(0), args(1), n, "strncmpi",
725  else
726  error ("strncmpi: N must be greater than 0");
727 }
728 
729 /*
730 %!assert (strncmpi ("abc123", "ABC456", 3), true)
731 */
732 
733 DEFUN (list_in_columns, args, ,
734  doc: /* -*- texinfo -*-
735 @deftypefn {} {} list_in_columns (@var{arg}, @var{width}, @var{prefix})
736 Return a string containing the elements of @var{arg} listed in columns with
737 an overall maximum width of @var{width} and optional prefix @var{prefix}.
738 
739 The argument @var{arg} must be a cell array of character strings or a
740 character array.
741 
742 If @var{width} is not specified or is an empty matrix, or less than or equal
743 to zero, the width of the terminal screen is used. Newline characters are
744 used to break the lines in the output string. For example:
745 @c Set example in small font to prevent overfull line
746 
747 @smallexample
748 @group
749 list_in_columns (@{"abc", "def", "ghijkl", "mnop", "qrs", "tuv"@}, 20)
750  @result{} abc mnop
751  def qrs
752  ghijkl tuv
753 
754 whos ans
755  @result{}
756  Variables in the current scope:
757 
758  Attr Name Size Bytes Class
759  ==== ==== ==== ===== =====
760  ans 1x37 37 char
761 
762  Total is 37 elements using 37 bytes
763 @end group
764 @end smallexample
765 
766 @seealso{terminal_size}
767 @end deftypefn */)
768 {
769  int nargin = args.length ();
770 
771  if (nargin < 1 || nargin > 3)
772  print_usage ();
773 
774  string_vector s = args(0).xstring_vector_value ("list_in_columns: ARG must be a cellstr or char array");
775 
776  int width = -1;
777 
778  if (nargin > 1 && ! args(1).is_empty ())
779  width = args(1).xint_value ("list_in_columns: WIDTH must be an integer");
780 
781  std::string prefix;
782 
783  if (nargin > 2)
784  prefix = args(2).xstring_value ("list_in_columns: PREFIX must be a string");
785 
786  std::ostringstream buf;
787 
788  s.list_in_columns (buf, width, prefix);
789 
790  return ovl (buf.str ());
791 }
792 
793 /*
794 %!test
795 %! input = {"abc", "def", "ghijkl", "mnop", "qrs", "tuv"};
796 %! result = "abc mnop\ndef qrs\nghijkl tuv\n";
797 %! assert (list_in_columns (input, 20), result);
798 %!test
799 %! input = ["abc"; "def"; "ghijkl"; "mnop"; "qrs"; "tuv"];
800 %! result = "abc mnop \ndef qrs \nghijkl tuv \n";
801 %! assert (list_in_columns (input, 20), result);
802 %!test
803 %! input = ["abc"; "def"; "ghijkl"; "mnop"; "qrs"; "tuv"];
804 %! result = " abc mnop \n def qrs \n ghijkl tuv \n";
805 %! assert (list_in_columns (input, 20, " "), result);
806 
807 %!error list_in_columns ()
808 %!error list_in_columns (["abc", "def"], 20, 2)
809 %!error list_in_columns (["abc", "def"], 20, " ", 3)
810 %!error <list_in_columns: WIDTH must be an integer> list_in_columns (["abc", "def"], "a")
811 */
OCTAVE_EXPORT octave_value_list ischar
Definition: data.cc:3327
Definition: Cell.h:37
charNDArray char_array_value(bool frc_str_conv=false) const
Definition: ov.h:831
OCTINTERP_API octave_value_list Fstrncmpi(const octave_value_list &=octave_value_list(), int=0)
octave_idx_type max_length(void) const
Definition: str-vec.h:81
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).is_integer_type())
OCTINTERP_API void print_usage(void)
Definition: defun.cc:52
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:363
s2
Definition: sub2ind.cc:107
for large enough k
Definition: lu.cc:606
#define DEFUN(name, args_name, nargout_name, doc)
Definition: defun.h:46
void error(const char *fmt,...)
Definition: error.cc:570
bool is_cell(void) const
Definition: ov.h:545
OCTAVE_EXPORT octave_value_list return the number of command line arguments passed to Octave If called with the optional argument the function t
Definition: ov-usr-fcn.cc:935
s
Definition: file-io.cc:2682
Cell cell_value(void) const
Definition: ov.cc:1687
JNIEnv void * args
Definition: ov-java.cc:67
Array< std::string > cellstr_value(void) const
Definition: ov.h:920
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
Definition: Array.h:439
octave_idx_type numel(const octave_value_list &idx)
Definition: ov.h:411
std::string string_value(bool force=false) const
Definition: ov.h:908
int nargin
Definition: graphics.cc:10115
bool is_string(void) const
Definition: ov.h:578
bool strcmp(const T &str_a, const T &str_b)
True if strings are the same.
Definition: oct-string.cc:112
std::string str
Definition: hash.cc:118
OCTINTERP_API octave_value_list Fstrcmpi(const octave_value_list &=octave_value_list(), int=0)
#define DEFUNX(name, fname, args_name, nargout_name, doc)
Definition: defun.h:54
octave_value retval
Definition: data.cc:6294
bool is_cellstr(void) const
Definition: ov.h:548
bool is_cellstr(void) const
Definition: Cell.cc:123
std::ostream & list_in_columns(std::ostream &, int width=0, const std::string &prefix="") const
Definition: str-vec.cc:195
dim_vector dims(void) const
Definition: ov.h:486
string_vector string_vector_value(bool pad=false) const
Definition: ov.h:911
With real return the complex result
Definition: data.cc:3375
octave_idx_type length(void) const
Number of elements in the array.
Definition: Array.h:354
bool is_empty(void) const
Definition: ov.h:542
bool strcmpi(const T &str_a, const T &str_b)
True if strings are the same, ignoring case.
Definition: oct-string.cc:129
bool is_dq_string(void) const
Definition: ov.h:584
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
bool strncmp(const T &str_a, const T &str_b, const typename T::size_type n)
True if the first N characters are the same.
Definition: oct-string.cc:146
bool strncmpi(const T &str_a, const T &str_b, const typename T::size_type n)
True if the first N characters are the same, ignoring case.
Definition: oct-string.cc:165
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:854
dim_vector dv
Definition: sub2ind.cc:263
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))