annotate src/strfns.cc @ 8920:eb63fbe60fab

update copyright notices
author John W. Eaton <jwe@octave.org>
date Sat, 07 Mar 2009 10:41:27 -0500
parents c3445f1c8cb4
children 58604c45ca74
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
807
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
1 /*
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
2
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
3 Copyright (C) 1994, 1995, 1996, 1997, 1999, 2002, 2003, 2004, 2005,
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8825
diff changeset
4 2006, 2007, 2008, 2009 John W. Eaton
807
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
5
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
6 This file is part of Octave.
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
7
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
8 Octave is free software; you can redistribute it and/or modify it
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
9 under the terms of the GNU General Public License as published by the
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6678
diff changeset
10 Free Software Foundation; either version 3 of the License, or (at your
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6678
diff changeset
11 option) any later version.
807
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
12
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
13 Octave is distributed in the hope that it will be useful, but WITHOUT
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
16 for more details.
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
17
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6678
diff changeset
19 along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6678
diff changeset
20 <http://www.gnu.org/licenses/>.
807
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
21
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
22 */
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
23
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
24 #ifdef HAVE_CONFIG_H
1192
b6360f2d4fa6 [project @ 1995-03-30 21:38:35 by jwe]
jwe
parents: 1009
diff changeset
25 #include <config.h>
807
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
26 #endif
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
27
1355
94697d007075 [project @ 1995-09-05 20:04:15 by jwe]
jwe
parents: 1315
diff changeset
28 #include <cctype>
7528
26d8a92644de try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
29
8463
a74871446af7 Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents: 8462
diff changeset
30 #include <queue>
5765
7ba9ad1fec11 [project @ 2006-04-17 05:05:15 by jwe]
jwe
parents: 5707
diff changeset
31 #include <sstream>
807
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
32
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
33 #include "dMatrix.h"
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
34
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
35 #include "Cell.h"
1355
94697d007075 [project @ 1995-09-05 20:04:15 by jwe]
jwe
parents: 1315
diff changeset
36 #include "defun.h"
807
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
37 #include "error.h"
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
38 #include "gripes.h"
2366
c90105b99479 [project @ 1996-10-11 23:22:27 by jwe]
jwe
parents: 2267
diff changeset
39 #include "ov.h"
1355
94697d007075 [project @ 1995-09-05 20:04:15 by jwe]
jwe
parents: 1315
diff changeset
40 #include "oct-obj.h"
4457
d7d9ca19960a [project @ 2003-07-11 03:05:38 by jwe]
jwe
parents: 4358
diff changeset
41 #include "unwind-prot.h"
807
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
42 #include "utils.h"
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
43
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
44 DEFUN (char, args, ,
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
45 "-*- texinfo -*-\n\
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
46 @deftypefn {Built-in Function} {} char (@var{x})\n\
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
47 @deftypefnx {Built-in Function} {} char (@var{cell_array})\n\
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
48 @deftypefnx {Built-in Function} {} char (@var{s1}, @var{s2}, @dots{})\n\
8461
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
49 Create a string array from one or more numeric matrices, character\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
50 matrices or cell arrays. For numerical input, each element is converted\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
51 to the corresponding ASCII character. The arguments (and elements of\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
52 cell array(s)) are concatenated vertically.\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
53 The returned values are padded with blanks as needed to make each row\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
54 of the string array have the same length. Empty strings are not removed.\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
55 For example,\n\
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
56 \n\
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
57 @example\n\
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
58 @group\n\
8461
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
59 char ([97, 98, 99], \"\", @{\"98\", \"99\", 100@}, [\"num\", \"bers\"])\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
60 @result{} [\"abc \"\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
61 \" \"\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
62 \"98 \"\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
63 \"99 \"\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
64 \"d \"\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
65 \"numbers\"]\n\
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
66 @end group\n\
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
67 @end example\n\
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
68 \n\
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
69 @end deftypefn")
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
70 {
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
71 octave_value retval;
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
72
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
73 int nargin = args.length ();
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
74
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
75 if (nargin == 1)
5281
f3266e7dbb99 [project @ 2005-04-14 19:35:20 by jwe]
jwe
parents: 5280
diff changeset
76 retval = args(0).convert_to_str (true, true,
f3266e7dbb99 [project @ 2005-04-14 19:35:20 by jwe]
jwe
parents: 5280
diff changeset
77 args(0).is_dq_string () ? '"' : '\'');
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
78 else if (nargin > 1)
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
79 {
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
80 int n_elts = 0;
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
81
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
82 int max_len = 0;
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
83
8463
a74871446af7 Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents: 8462
diff changeset
84 std::queue<string_vector> args_as_strings;
a74871446af7 Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents: 8462
diff changeset
85
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
86 for (int i = 0; i < nargin; i++)
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
87 {
5707
6286c96121c2 [project @ 2006-03-22 05:07:28 by jwe]
jwe
parents: 5690
diff changeset
88 string_vector s = args(i).all_strings ();
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
89
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
90 if (error_state)
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
91 {
4457
d7d9ca19960a [project @ 2003-07-11 03:05:38 by jwe]
jwe
parents: 4358
diff changeset
92 error ("char: unable to convert some args to strings");
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
93 return retval;
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
94 }
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
95
8353
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
96 if (s.length () > 0)
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
97 n_elts += s.length ();
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
98 else
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
99 n_elts += 1;
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
100
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
101 int s_max_len = s.max_length ();
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
102
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
103 if (s_max_len > max_len)
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
104 max_len = s_max_len;
8463
a74871446af7 Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents: 8462
diff changeset
105
a74871446af7 Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents: 8462
diff changeset
106 args_as_strings.push (s);
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
107 }
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
108
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
109 string_vector result (n_elts);
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
110
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
111 int k = 0;
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
112
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
113 for (int i = 0; i < nargin; i++)
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
114 {
8464
cb48e594a441 Fchar, Fstrvcat: use queue correctly
John W. Eaton <jwe@octave.org>
parents: 8463
diff changeset
115 string_vector s = args_as_strings.front ();
cb48e594a441 Fchar, Fstrvcat: use queue correctly
John W. Eaton <jwe@octave.org>
parents: 8463
diff changeset
116 args_as_strings.pop ();
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
117
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
118 int n = s.length ();
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
119
8353
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
120 if (n > 0)
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
121 {
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
122 for (int j = 0; j < n; j++)
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
123 {
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
124 std::string t = s[j];
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
125 int t_len = t.length ();
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
126
8353
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
127 if (max_len > t_len)
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
128 t += std::string (max_len - t_len, ' ');
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
129
8353
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
130 result[k++] = t;
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
131 }
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
132 }
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
133 else
8463
a74871446af7 Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents: 8462
diff changeset
134 result[k++] = std::string (max_len, ' ');
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
135 }
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
136
5280
25c12c80ccc3 [project @ 2005-04-14 19:19:45 by jwe]
jwe
parents: 4734
diff changeset
137 retval = octave_value (result, '\'');
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
138 }
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
139 else
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5765
diff changeset
140 print_usage ();
4358
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
141
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
142 return retval;
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
143 }
83d4452bc522 [project @ 2003-02-23 02:16:53 by jwe]
jwe
parents: 4233
diff changeset
144
8353
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
145 /*
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
146 %!error <Invalid call to char> char()
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
147 %!assert (char (100) == "d");
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
148 %!assert (all(char (100,100) == ["d";"d"]));
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
149 %!assert (all(char ({100,100}) == ["d";"d"]));
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
150 %!assert (all(char ([100,100]) == ["dd"]));
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
151 %!assert (all(char ({100,{100}}) == ["d";"d"]));
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
152 %!assert (all(char (100, [], 100) == ["d";" ";"d"]))
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
153 %!assert (all(char ({100, [], 100}) == ["d";" ";"d"]))
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
154 %!assert (all(char ({100,{100, {""}}}) == ["d";"d";" "]))
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
155 %!assert (all(char (["a";"be"], {"c", 100}) == ["a";"be";"c";"d"]))
8372
8dff9cba15fe move str2mat to deprecated and make it a simple wrapper around char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8353
diff changeset
156 %!assert(strcmp (char ("a", "bb", "ccc"), ["a "; "bb "; "ccc"]));
8353
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
157 */
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
158
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
159 DEFUN (strvcat, args, ,
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
160 "-*- texinfo -*-\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
161 @deftypefn {Built-in Function} {} strvcat (@var{x})\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
162 @deftypefnx {Built-in Function} {} strvcat (@var{cell_array})\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
163 @deftypefnx {Built-in Function} {} strvcat (@var{s1}, @var{s2}, @dots{})\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
164 Create a character array from one or more numeric matrices, character\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
165 matrices or cell arrays. For numerical input, each element is converted\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
166 to the corresponding ASCII character. The arguments (and elements of\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
167 cell array(s)) are concatenated vertically.\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
168 The returned values are padded with blanks as needed to make each row\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
169 of the string array have the same length. Unlike @code{char}, empty\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
170 strings are removed.\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
171 For example,\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
172 \n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
173 @example\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
174 @group\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
175 strvcat ([97, 98, 99], \"\", @{\"98\", \"99\", 100@}, [\"num\", \"bers\"])\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
176 @result{} [\"abc \"\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
177 \"98 \"\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
178 \"99 \"\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
179 \"d \"\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
180 \"numbers\"]\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
181 @end group\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
182 @end example\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
183 \n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
184 @seealso{char, strcat, cstrcat}\n\
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
185 @end deftypefn")
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
186 {
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
187 octave_value retval;
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
188
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
189 int nargin = args.length ();
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
190
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
191 if (nargin > 0)
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
192 {
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
193 int n_elts = 0;
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
194
8809
732cb0236488 strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
195 size_t max_len = 0;
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
196
8463
a74871446af7 Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents: 8462
diff changeset
197 std::queue<string_vector> args_as_strings;
a74871446af7 Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents: 8462
diff changeset
198
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
199 for (int i = 0; i < nargin; i++)
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
200 {
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
201 string_vector s = args(i).all_strings ();
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
202
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
203 if (error_state)
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
204 {
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
205 error ("strvcat: unable to convert some args to strings");
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
206 return retval;
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
207 }
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
208
8809
732cb0236488 strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
209 size_t n = s.length ();
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
210
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
211 // do not count empty strings in calculation of number of elements
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
212 if (n > 0)
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
213 {
8809
732cb0236488 strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
214 for (size_t j = 0; j < n; j++)
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
215 {
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
216 if (s[j].length () > 0)
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
217 n_elts++;
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
218 }
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
219 }
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
220
8809
732cb0236488 strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
221 size_t s_max_len = s.max_length ();
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
222
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
223 if (s_max_len > max_len)
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
224 max_len = s_max_len;
8463
a74871446af7 Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents: 8462
diff changeset
225
a74871446af7 Fchar, Fstrvcat: use queue to avoid duplicate calls to all_strings
John W. Eaton <jwe@octave.org>
parents: 8462
diff changeset
226 args_as_strings.push (s);
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
227 }
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
228
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
229 string_vector result (n_elts);
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
230
8809
732cb0236488 strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
231 octave_idx_type k = 0;
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
232
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
233 for (int i = 0; i < nargin; i++)
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
234 {
8464
cb48e594a441 Fchar, Fstrvcat: use queue correctly
John W. Eaton <jwe@octave.org>
parents: 8463
diff changeset
235 string_vector s = args_as_strings.front ();
cb48e594a441 Fchar, Fstrvcat: use queue correctly
John W. Eaton <jwe@octave.org>
parents: 8463
diff changeset
236 args_as_strings.pop ();
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
237
8809
732cb0236488 strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
238 size_t n = s.length ();
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
239
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
240 if (n > 0)
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
241 {
8809
732cb0236488 strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
242 for (size_t j = 0; j < n; j++)
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
243 {
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
244 std::string t = s[j];
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
245 if (t.length () > 0)
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
246 {
8809
732cb0236488 strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
247 size_t t_len = t.length ();
8462
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
248
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
249 if (max_len > t_len)
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
250 t += std::string (max_len - t_len, ' ');
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
251
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
252 result[k++] = t;
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
253 }
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
254 }
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
255 }
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
256 }
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
257
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
258 retval = octave_value (result, '\'');
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
259 }
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
260 else
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
261 print_usage ();
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
262
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
263 return retval;
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
264 }
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
265
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
266 /*
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
267 %!error <Invalid call to strvcat> strvcat()
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
268 %!assert (strvcat (""), "");
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
269 %!assert (strvcat (100) == "d");
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
270 %!assert (all(strvcat (100,100) == ["d";"d"]));
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
271 %!assert (all(strvcat ({100,100}) == ["d";"d"]));
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
272 %!assert (all(strvcat ([100,100]) == ["dd"]));
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
273 %!assert (all(strvcat ({100,{100}}) == ["d";"d"]));
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
274 %!assert (all(strvcat (100, [], 100) == ["d";"d"]))
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
275 %!assert (all(strvcat ({100, [], 100}) == ["d";"d"]))
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
276 %!assert (all(strvcat ({100,{100, {""}}}) == ["d";"d"]))
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
277 %!assert (all(strvcat (["a";"be"], {"c", 100}) == ["a";"be";"c";"d"]))
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
278 %!assert(strcmp (strvcat ("a", "bb", "ccc"), ["a "; "bb "; "ccc"]));
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
279 */
ebdf1e058d85 Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8461
diff changeset
280
8353
349a555729a9 keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 7528
diff changeset
281
4535
a5f23212a3d8 [project @ 2003-10-16 02:39:15 by jwe]
jwe
parents: 4457
diff changeset
282 DEFUN (ischar, args, ,
3361
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 3014
diff changeset
283 "-*- texinfo -*-\n\
4535
a5f23212a3d8 [project @ 2003-10-16 02:39:15 by jwe]
jwe
parents: 4457
diff changeset
284 @deftypefn {Built-in Function} {} ischar (@var{a})\n\
8461
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
285 Return 1 if @var{a} is a character array. Otherwise, return 0.\n\
3361
4f40efa995c1 [project @ 1999-11-19 21:19:37 by jwe]
jwe
parents: 3014
diff changeset
286 @end deftypefn")
807
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
287 {
4233
ccfdb55c8156 [project @ 2002-12-20 22:43:54 by jwe]
jwe
parents: 3363
diff changeset
288 octave_value retval;
807
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
289
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
290 int nargin = args.length ();
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
291
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
292 if (nargin == 1 && args(0).is_defined ())
4233
ccfdb55c8156 [project @ 2002-12-20 22:43:54 by jwe]
jwe
parents: 3363
diff changeset
293 retval = args(0).is_string ();
807
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
294 else
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5765
diff changeset
295 print_usage ();
807
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
296
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
297 return retval;
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
298 }
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
299
8461
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
300 /*
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
301
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
302 %!assert (ischar ("a"), logical (1));
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
303 %!assert (ischar (["ab";"cd"]), logical (1));
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
304 %!assert (ischar ({"ab"}), logical (0));
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
305 %!assert (ischar (1), logical (0));
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
306 %!error <Invalid call to ischar.*> ischar ();
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
307
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
308 */
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
309
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
310 DEFUN (strcmp, args, ,
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
311 "-*- texinfo -*-\n\
6678
49724abe1236 [project @ 2007-05-31 19:44:45 by jwe]
jwe
parents: 6256
diff changeset
312 @deftypefn {Built-in Function} {} strcmp (@var{s1}, @var{s2})\n\
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
313 Return 1 if the character strings @var{s1} and @var{s2} are the same,\n\
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
314 and 0 otherwise.\n\
5674
86adc85cc471 [project @ 2006-03-16 03:46:45 by jwe]
jwe
parents: 5531
diff changeset
315 \n\
86adc85cc471 [project @ 2006-03-16 03:46:45 by jwe]
jwe
parents: 5531
diff changeset
316 If either @var{s1} or @var{s2} is a cell array of strings, then an array\n\
86adc85cc471 [project @ 2006-03-16 03:46:45 by jwe]
jwe
parents: 5531
diff changeset
317 of the same size is returned, containing the values described above for\n\
86adc85cc471 [project @ 2006-03-16 03:46:45 by jwe]
jwe
parents: 5531
diff changeset
318 every member of the cell array. The other argument may also be a cell\n\
86adc85cc471 [project @ 2006-03-16 03:46:45 by jwe]
jwe
parents: 5531
diff changeset
319 array of strings (of the same size or with only one element), char matrix\n\
86adc85cc471 [project @ 2006-03-16 03:46:45 by jwe]
jwe
parents: 5531
diff changeset
320 or character string.\n\
86adc85cc471 [project @ 2006-03-16 03:46:45 by jwe]
jwe
parents: 5531
diff changeset
321 \n\
86adc85cc471 [project @ 2006-03-16 03:46:45 by jwe]
jwe
parents: 5531
diff changeset
322 @strong{Caution:} For compatibility with @sc{Matlab}, Octave's strcmp\n\
86adc85cc471 [project @ 2006-03-16 03:46:45 by jwe]
jwe
parents: 5531
diff changeset
323 function returns 1 if the character strings are equal, and 0 otherwise.\n\
86adc85cc471 [project @ 2006-03-16 03:46:45 by jwe]
jwe
parents: 5531
diff changeset
324 This is just the opposite of the corresponding C library function.\n\
86adc85cc471 [project @ 2006-03-16 03:46:45 by jwe]
jwe
parents: 5531
diff changeset
325 @seealso{strcmpi, strncmp, strncmpi}\n\
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
326 @end deftypefn")
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
327 {
5531
ad2e70544c77 [project @ 2005-11-07 19:12:21 by jwe]
jwe
parents: 5530
diff changeset
328 octave_value retval;
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
329
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
330 if (args.length () == 2)
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
331 {
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
332 bool s1_string = args(0).is_string ();
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
333 bool s1_cell = args(0).is_cell ();
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
334 bool s2_string = args(1).is_string ();
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
335 bool s2_cell = args(1).is_cell ();
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
336
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
337 if (s1_string && s2_string)
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
338 {
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
339 // Must match exactly in all dimensions.
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
340
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
341 const dim_vector dv1 = args(0).dims ();
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
342 const dim_vector dv2 = args(1).dims ();
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
343
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
344 if (dv1.length () == dv2.length ())
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
345 {
8809
732cb0236488 strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
346 for (octave_idx_type i = 0; i < dv1.length (); i++)
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
347 {
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
348 if (dv1(i) != dv2(i))
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
349 {
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
350 retval = false;
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
351 return retval;
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
352 }
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
353 }
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
354
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
355 if (dv1(0) == 0)
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
356 retval = true;
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
357 else
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
358 {
8825
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
359 const charNDArray s1 = args(0).char_array_value ();
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
360 const charNDArray s2 = args(1).char_array_value ();
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
361
8809
732cb0236488 strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
362 for (octave_idx_type i = 0; i < dv1.numel (); i++)
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
363 {
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
364 if (s1(i) != s2(i))
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
365 {
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
366 retval = false;
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
367 return retval;
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
368 }
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
369 }
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
370
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
371 retval = true;
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
372 }
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
373 }
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
374 }
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
375 else if ((s1_string && s2_cell) || (s1_cell && s2_string))
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
376 {
8825
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
377 octave_value str_val, cell_val;
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
378
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
379 if (s1_string)
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
380 {
8825
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
381 str_val = args (0);
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
382 cell_val = args (1);
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
383 }
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
384 else
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
385 {
8825
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
386 str_val = args (1);
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
387 cell_val = args (0);
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
388 }
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
389
8825
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
390 const Cell cell = cell_val.cell_value ();
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
391 const string_vector str = str_val.all_strings ();
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
392 octave_idx_type r = str.rows ();
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
393
5862
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
394 if (r == 0 || r == 1)
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
395 {
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
396 // Broadcast the string.
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
397
8825
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
398 boolNDArray output (cell_val.dims (), false);
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
399
5862
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
400 std::string s = r == 0 ? std::string () : str[0];
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
401
8825
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
402 if (cell_val.is_cellstr ())
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
403 {
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
404 const Array<std::string> cellstr = cell_val.cellstr_value ();
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
405 for (octave_idx_type i = 0; i < cellstr.length (); i++)
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
406 output(i) = cellstr(i) == s;
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
407 }
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
408 else
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
409 {
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
410 // FIXME: should we warn here?
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
411 for (octave_idx_type i = 0; i < cell.length (); i++)
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
412 {
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
413 if (cell(i).is_string ())
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
414 output(i) = (cell(i).string_value () == s);
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
415 }
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
416 }
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
417
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
418 retval = output;
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
419 }
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
420 else if (r > 1)
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
421 {
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
422 if (cell.length () == 1)
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
423 {
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
424 // Broadcast the cell.
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
425
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
426 const dim_vector dv (r, 1);
8472
5451f7460ea6 strfns.cc (Fstrcmp, Fstrncmp): initialize all elements of output with constructor arg
John W. Eaton <jwe@octave.org>
parents: 8464
diff changeset
427 boolNDArray output (dv, false);
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
428
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
429 if (cell(0).is_string ())
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
430 {
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
431 const std::string str2 = cell(0).string_value ();
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
432
8809
732cb0236488 strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
433 for (octave_idx_type i = 0; i < r; i++)
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
434 output(i) = (str[i] == str2);
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
435 }
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
436
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
437 retval = output;
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
438 }
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
439 else
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
440 {
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
441 // Must match in all dimensions.
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
442
8524
937921654627 clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents: 8472
diff changeset
443 boolNDArray output (cell.dims (), false);
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
444
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
445 if (cell.length () == r)
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
446 {
8825
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
447 if (cell_val.is_cellstr ())
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
448 {
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
449 const Array<std::string> cellstr = cell_val.cellstr_value ();
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
450 for (octave_idx_type i = 0; i < cellstr.length (); i++)
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
451 output(i) = str[i] == cellstr(i);
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
452 }
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
453 else
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
454 {
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
455 // FIXME: should we warn here?
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
456 for (octave_idx_type i = 0; i < r; i++)
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
457 {
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
458 if (cell(i).is_string ())
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
459 output(i) = (str[i] == cell(i).string_value ());
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
460 }
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
461 }
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
462
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
463 retval = output;
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
464 }
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
465 else
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
466 retval = false;
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
467 }
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
468 }
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
469 }
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
470 else if (s1_cell && s2_cell)
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
471 {
8825
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
472 octave_value cell1_val, cell2_val;
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
473 octave_idx_type r1 = args(0).numel (), r2;
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
474
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
475 if (r1 == 1)
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
476 {
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
477 // Make the singleton cell2.
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
478
8825
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
479 cell1_val = args(1);
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
480 cell2_val = args(0);
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
481 }
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
482 else
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
483 {
8825
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
484 cell1_val = args(0);
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
485 cell2_val = args(1);
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
486 }
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
487
8825
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
488 const Cell cell1 = cell1_val.cell_value ();
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
489 const Cell cell2 = cell2_val.cell_value ();
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
490 r1 = cell1.numel ();
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
491 r2 = cell2.numel ();
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
492
8524
937921654627 clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents: 8472
diff changeset
493 const dim_vector size1 = cell1.dims ();
937921654627 clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents: 8472
diff changeset
494 const dim_vector size2 = cell2.dims ();
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
495
8472
5451f7460ea6 strfns.cc (Fstrcmp, Fstrncmp): initialize all elements of output with constructor arg
John W. Eaton <jwe@octave.org>
parents: 8464
diff changeset
496 boolNDArray output (size1, false);
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
497
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
498 if (r2 == 1)
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
499 {
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
500 // Broadcast cell2.
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
501
8472
5451f7460ea6 strfns.cc (Fstrcmp, Fstrncmp): initialize all elements of output with constructor arg
John W. Eaton <jwe@octave.org>
parents: 8464
diff changeset
502 if (cell2(0).is_string ())
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
503 {
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
504 const std::string str2 = cell2(0).string_value ();
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
505
8825
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
506 if (cell1_val.is_cellstr ())
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
507 {
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
508 const Array<std::string> cellstr = cell1_val.cellstr_value ();
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
509 for (octave_idx_type i = 0; i < cellstr.length (); i++)
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
510 output(i) = cellstr(i) == str2;
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
511 }
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
512 else
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
513 {
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
514 // FIXME: should we warn here?
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
515 for (octave_idx_type i = 0; i < r1; i++)
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
516 {
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
517 if (cell1(i).is_string ())
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
518 {
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
519 const std::string str1 = cell1(i).string_value ();
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
520 output(i) = (str1 == str2);
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
521 }
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
522 }
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
523 }
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
524 }
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
525 }
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
526 else
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
527 {
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
528 if (size1 != size2)
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
529 {
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
530 error ("strcmp: nonconformant cell arrays");
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
531 return retval;
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
532 }
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
533
8825
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
534 if (cell1.is_cellstr () && cell2.is_cellstr ())
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
535 {
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
536 const Array<std::string> cellstr1 = cell1_val.cellstr_value ();
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
537 const Array<std::string> cellstr2 = cell2_val.cellstr_value ();
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
538 for (octave_idx_type i = 0; i < r1; i++)
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
539 output (i) = cellstr1(i) == cellstr2(i);
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
540 }
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
541 else
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
542 {
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
543 // FIXME: should we warn here?
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
544 for (octave_idx_type i = 0; i < r1; i++)
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
545 {
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
546 if (cell1(i).is_string () && cell2(i).is_string ())
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
547 {
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
548 const std::string str1 = cell1(i).string_value ();
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
549 const std::string str2 = cell2(i).string_value ();
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
550 output(i) = (str1 == str2);
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
551 }
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
552 }
c3445f1c8cb4 reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents: 8809
diff changeset
553 }
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
554 }
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
555
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
556 retval = output;
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
557 }
5531
ad2e70544c77 [project @ 2005-11-07 19:12:21 by jwe]
jwe
parents: 5530
diff changeset
558 else
ad2e70544c77 [project @ 2005-11-07 19:12:21 by jwe]
jwe
parents: 5530
diff changeset
559 retval = false;
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
560 }
5416
9433f94acd29 [project @ 2005-07-18 18:36:29 by jwe]
jwe
parents: 5415
diff changeset
561 else
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5765
diff changeset
562 print_usage ();
5415
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
563
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
564 return retval;
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
565 }
1fbae9f046e3 [project @ 2005-07-18 18:16:12 by jwe]
jwe
parents: 5307
diff changeset
566
5862
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
567 /*
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
568 %!shared x
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
569 %! x = char (zeros (0, 2));
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
570 %!assert (strcmp ('', x) == false);
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
571 %!assert (strcmp (x, '') == false);
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
572 %!assert (strcmp (x, x) == true);
5911
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
573 ## %!assert (strcmp ({''}, x) == false);
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
574 ## %!assert (strcmp ({x}, '') == false);
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
575 ## %!assert (strcmp ({x}, x) == true);
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
576 ## %!assert (strcmp ('', {x}) == false);
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
577 ## %!assert (strcmp (x, {''}) == false);
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
578 ## %!assert (strcmp (x, {x}) == true);
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
579 ## %!assert (all (strcmp ({x; x}, '') == [false; false]));
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
580 ## %!assert (all (strcmp ({x; x}, {''}) == [false; false]));
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
581 ## %!assert (all (strcmp ('', {x; x}) == [false; false]));
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
582 ## %!assert (all (strcmp ({''}, {x; x}) == [false; false]));
5862
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
583 %!assert (strcmp ({'foo'}, x) == false);
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
584 %!assert (strcmp ({'foo'}, 'foo') == true);
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
585 %!assert (strcmp ({'foo'}, x) == false);
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
586 %!assert (strcmp (x, {'foo'}) == false);
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
587 %!assert (strcmp ('foo', {'foo'}) == true);
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
588 %!assert (strcmp (x, {'foo'}) == false);
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
589 %!shared y
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
590 %! y = char (zeros (2, 0));
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
591 %!assert (strcmp ('', y) == false);
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
592 %!assert (strcmp (y, '') == false);
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
593 %!assert (strcmp (y, y) == true);
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
594 %!assert (all (strcmp ({''}, y) == [true; true]));
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
595 %!assert (strcmp ({y}, '') == true);
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
596 %!assert (all (strcmp ({y}, y) == [true; true]));
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
597 %!assert (all (strcmp ('', {y}) == [true; true]));
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
598 %!assert (all (strcmp (y, {''}) == [true; true]));
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
599 %!assert (all (strcmp (y, {y}) == [true; true]));
5911
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
600 ## %!assert (all (strcmp ({y; y}, '') == [false; false]));
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
601 ## %!assert (all (strcmp ({y; y}, {''}) == [false; false]));
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
602 ## %!assert (all (strcmp ('', {y; y}) == [false; false]));
a65b51ed388c [project @ 2006-07-28 16:31:00 by jwe]
jwe
parents: 5862
diff changeset
603 ## %!assert (all (strcmp ({''}, {y; y}) == [false; false]));
5862
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
604 %!assert (all (strcmp ({'foo'}, y) == [false; false]));
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
605 %!assert (all (strcmp ({'foo'}, y) == [false; false]));
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
606 %!assert (all (strcmp (y, {'foo'}) == [false; false]));
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
607 %!assert (all (strcmp (y, {'foo'}) == [false; false]));
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
608 */
2b35a7fe9302 [project @ 2006-06-20 04:31:56 by jwe]
jwe
parents: 5823
diff changeset
609
6250
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
610 DEFUN (strncmp, args, ,
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
611 "-*- texinfo -*-\n\
6678
49724abe1236 [project @ 2007-05-31 19:44:45 by jwe]
jwe
parents: 6256
diff changeset
612 @deftypefn {Built-in Function} {} strncmp (@var{s1}, @var{s2}, @var{n})\n\
6250
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
613 Return 1 if the first @var{n} characters of strings @var{s1} and @var{s2} are the same,\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
614 and 0 otherwise.\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
615 \n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
616 @example\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
617 @group\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
618 strncmp (\"abce\", \"abcd\", 3)\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
619 @result{} 1\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
620 @end group\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
621 @end example\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
622 \n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
623 If either @var{s1} or @var{s2} is a cell array of strings, then an array\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
624 of the same size is returned, containing the values described above for\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
625 every member of the cell array. The other argument may also be a cell\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
626 array of strings (of the same size or with only one element), char matrix\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
627 or character string.\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
628 \n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
629 @example\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
630 @group\n\
6256
83949ae13b2c [project @ 2007-01-30 18:45:59 by jwe]
jwe
parents: 6250
diff changeset
631 strncmp (\"abce\", @{\"abcd\", \"bca\", \"abc\"@}, 3)\n\
6250
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
632 @result{} [1, 0, 1]\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
633 @end group\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
634 @end example\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
635 \n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
636 @strong{Caution:} For compatibility with @sc{Matlab}, Octave's strncmp\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
637 function returns 1 if the character strings are equal, and 0 otherwise.\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
638 This is just the opposite of the corresponding C library function.\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
639 @seealso{strncmpi, strcmp, strcmpi}\n\
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
640 @end deftypefn")
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
641 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
642 octave_value retval;
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
643
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
644 if (args.length () == 3)
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
645 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
646 bool s1_string = args(0).is_string ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
647 bool s1_cell = args(0).is_cell ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
648 bool s2_string = args(1).is_string ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
649 bool s2_cell = args(1).is_cell ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
650
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
651 // Match only first n strings.
8809
732cb0236488 strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
652 int n = args(2).int_value ();
6250
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
653
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
654 if (n <= 0)
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
655 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
656 error ("strncmp: N must be greater than 0");
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
657 return retval;
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
658 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
659
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
660 if (s1_string && s2_string)
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
661 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
662 // The only restriction here is that each string has equal or
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
663 // greater than n characters
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
664
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
665 const dim_vector dv1 = args(0).dims ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
666 const dim_vector dv2 = args(1).dims ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
667
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
668 if (dv1.numel () >= n && dv2.numel () >= n)
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
669 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
670 // Follow Matlab in the sense that the first n characters of
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
671 // the two strings (in column major order) need to be the same.
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
672 charNDArray s1 = args(0).char_array_value ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
673 charNDArray s2 = args(1).char_array_value ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
674
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
675 for (int i = 0; i < n; i++)
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
676 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
677 if (s1(i) != s2(i))
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
678 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
679 retval = false;
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
680 return retval;
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
681 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
682 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
683
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
684 retval = true;
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
685 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
686 else
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
687 retval = false;
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
688 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
689 else if ((s1_string && s2_cell) || (s1_cell && s2_string))
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
690 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
691 string_vector str;
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
692 Cell cell;
8809
732cb0236488 strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
693 octave_idx_type r, c;
6250
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
694
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
695 if (s1_string)
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
696 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
697 str = args(0).all_strings ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
698 r = args(0).rows ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
699 c = args(0).columns ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
700 cell = args(1).cell_value ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
701 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
702 else
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
703 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
704 str = args(1).all_strings ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
705 r = args(1).rows ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
706 c = args(1).columns ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
707 cell = args(0).cell_value ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
708 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
709
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
710 if (r == 1)
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
711 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
712 // Broadcast the string.
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
713
8524
937921654627 clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents: 8472
diff changeset
714 boolNDArray output (cell.dims (), false);
6250
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
715
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
716 if (c < n)
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
717 {
8809
732cb0236488 strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
718 for (octave_idx_type i = 0; i < cell.length (); i++)
6250
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
719 output(i) = false;
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
720 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
721 else
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
722 {
8809
732cb0236488 strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
723 for (octave_idx_type i = 0; i < cell.length (); i++)
6250
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
724 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
725 if (cell(i).is_string ())
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
726 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
727 const std::string str2 = cell(i).string_value ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
728
8809
732cb0236488 strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
729 if (str2.length () >= n
8472
5451f7460ea6 strfns.cc (Fstrcmp, Fstrncmp): initialize all elements of output with constructor arg
John W. Eaton <jwe@octave.org>
parents: 8464
diff changeset
730 && str2.compare (0, n, str[0], 0, n) == 0)
6250
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
731 output(i) = true;
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
732 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
733 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
734 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
735
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
736 retval = output;
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
737 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
738 else if (r > 1)
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
739 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
740 if (cell.length () == 1)
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
741 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
742 // Broadcast the cell.
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
743
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
744 const dim_vector dv (r, 1);
8472
5451f7460ea6 strfns.cc (Fstrcmp, Fstrncmp): initialize all elements of output with constructor arg
John W. Eaton <jwe@octave.org>
parents: 8464
diff changeset
745 boolNDArray output (dv, false);
6250
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
746
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
747 if (cell(0).is_string () && c >= n)
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
748 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
749 const std::string str2 = cell(0).string_value ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
750
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
751 if (str2.length () >= n)
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
752 {
8809
732cb0236488 strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
753 for (octave_idx_type i = 0; i < r; i++)
6250
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
754 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
755 if (str[i].compare (0, n, str2, 0, n) == 0)
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
756 output(i) = true;
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
757 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
758 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
759 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
760
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
761 retval = output;
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
762 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
763 else
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
764 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
765 // Must match in all dimensions.
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
766
8524
937921654627 clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents: 8472
diff changeset
767 boolNDArray output (cell.dims (), false);
6250
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
768
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
769 if (cell.numel () == r)
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
770 {
8809
732cb0236488 strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
771 for (octave_idx_type i = 0; i < r; i++)
6250
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
772 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
773 if (cell(i).is_string () && c >= n)
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
774 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
775 std::string str2 = cell(i).string_value ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
776
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
777 if (str2.length () >= n
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
778 && str2.compare (0, n, str[i], 0, n) == 0)
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
779 output(i) = true;
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
780 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
781 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
782
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
783 retval = output;
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
784 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
785 else
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
786 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
787 error ("strncmp: the number of rows of the string matrix must match the number of elements in the cell");
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
788 return retval;
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
789 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
790 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
791 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
792 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
793 else if (s1_cell && s2_cell)
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
794 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
795 Cell cell1;
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
796 Cell cell2;
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
797
8809
732cb0236488 strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
798 octave_idx_type r1 = args(0).numel ();
732cb0236488 strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
799 octave_idx_type r2;
6250
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
800
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
801 if (r1 == 1)
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
802 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
803 // Make the singleton cell2.
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
804
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
805 cell1 = args(1).cell_value ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
806 cell2 = args(0).cell_value ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
807 r1 = cell1.length ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
808 r2 = 1;
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
809 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
810 else
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
811 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
812 cell1 = args(0).cell_value ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
813 cell2 = args(1).cell_value ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
814 r2 = cell2.length ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
815 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
816
8524
937921654627 clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents: 8472
diff changeset
817 const dim_vector size1 = cell1.dims ();
937921654627 clean up Array and DiagArray2
Jaroslav Hajek <highegg@gmail.com>
parents: 8472
diff changeset
818 const dim_vector size2 = cell2.dims ();
6250
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
819
8472
5451f7460ea6 strfns.cc (Fstrcmp, Fstrncmp): initialize all elements of output with constructor arg
John W. Eaton <jwe@octave.org>
parents: 8464
diff changeset
820 boolNDArray output (size1, false);
6250
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
821
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
822 if (r2 == 1)
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
823 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
824 // Broadcast cell2.
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
825
8472
5451f7460ea6 strfns.cc (Fstrcmp, Fstrncmp): initialize all elements of output with constructor arg
John W. Eaton <jwe@octave.org>
parents: 8464
diff changeset
826 if (cell2(0).is_string ())
6250
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
827 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
828 const std::string str2 = cell2(0).string_value ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
829
8809
732cb0236488 strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
830 for (octave_idx_type i = 0; i < r1; i++)
6250
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
831 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
832 if (cell1(i).is_string ())
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
833 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
834 const std::string str1 = cell1(i).string_value ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
835
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
836 if (str1.length () >= n && str2.length () >= n
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
837 && str1.compare (0, n, str2, 0, n) == 0)
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
838 output(i) = true;
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
839 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
840 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
841 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
842 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
843 else
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
844 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
845 if (size1 != size2)
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
846 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
847 error ("strncmp: nonconformant cell arrays");
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
848 return retval;
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
849 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
850
8809
732cb0236488 strfns.cc: use size_t or octave_idx_type instead of int as needed
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
851 for (octave_idx_type i = 0; i < r1; i++)
6250
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
852 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
853 if (cell1(i).is_string () && cell2(i).is_string ())
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
854 {
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
855 const std::string str1 = cell1(i).string_value ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
856 const std::string str2 = cell2(i).string_value ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
857
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
858 if (str1.length () >= n && str2.length () >= n
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
859 && str1.compare (0, n, str2, 0, n) == 0)
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
860 output(i) = true;
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
861 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
862 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
863 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
864
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
865 retval = output;
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
866 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
867 else
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
868 retval = false;
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
869 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
870 else
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
871 print_usage ();
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
872
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
873 return retval;
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
874 }
ff5e6cf72bda [project @ 2007-01-23 18:37:52 by jwe]
jwe
parents: 5911
diff changeset
875
8461
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
876 /*
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
877 %!error <Invalid call to strncmp.*> strncmp ();
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
878 %!error <Invalid call to strncmp.*> strncmp ("abc", "def");
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
879 %!assert (strncmp ("abce", "abc", 3) == 1)
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
880 %!assert (strncmp (100, 100, 1) == 0)
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
881 %!assert (all (strncmp ("abce", {"abcd", "bca", "abc"}, 3) == [1, 0, 1]))
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
882 %!assert (all (strncmp ("abc", {"abcd", "bca", "abc"}, 4) == [0, 0, 0]))
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
883 %!assert (all (strncmp ({"abcd", "bca", "abc"},"abce", 3) == [1, 0, 1]))
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
884 %!assert (all (strncmp ({"abcd", "bca", "abc"},{"abcd", "bca", "abe"}, 3) == [1, 1, 0]))
8472
5451f7460ea6 strfns.cc (Fstrcmp, Fstrncmp): initialize all elements of output with constructor arg
John W. Eaton <jwe@octave.org>
parents: 8464
diff changeset
885 %!assert (all (strncmp("abc", {"abcd", 10}, 2) == [1, 0]))
8461
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
886 */
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
887
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
888
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
889 DEFUN (list_in_columns, args, ,
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
890 "-*- texinfo -*-\n\
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
891 @deftypefn {Built-in Function} {} list_in_columns (@var{arg}, @var{width})\n\
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
892 Return a string containing the elements of @var{arg} listed in\n\
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
893 columns with an overall maximum width of @var{width}. The argument\n\
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
894 @var{arg} must be a cell array of character strings or a character array.\n\
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
895 If @var{width} is not specified, the width of the terminal screen is used.\n\
8461
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
896 Newline characters are used to break the lines in the output string.\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
897 For example:\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
898 \n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
899 @example\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
900 @group\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
901 list_in_columns (@{\"abc\", \"def\", \"ghijkl\", \"mnop\", \"qrs\", \"tuv\"@}, 20)\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
902 @result{} ans = abc mnop\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
903 def qrs\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
904 ghijkl tuv\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
905 \n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
906 whos ans\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
907 @result{}\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
908 Variables in the current scope:\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
909 \n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
910 Attr Name Size Bytes Class\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
911 ==== ==== ==== ===== =====\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
912 ans 1x37 37 char\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
913 \n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
914 Total is 37 elements using 37 bytes\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
915 @end group\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
916 @end example\n\
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
917 \n\
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
918 @seealso{terminal_size}\n\
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
919 @end deftypefn")
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
920 {
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
921 octave_value retval;
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
922
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
923 int nargin = args.length ();
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
924
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
925 if (nargin == 1 || nargin == 2)
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
926 {
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
927 string_vector s = args(0).all_strings ();
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
928
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
929 if (! error_state)
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
930 {
5765
7ba9ad1fec11 [project @ 2006-04-17 05:05:15 by jwe]
jwe
parents: 5707
diff changeset
931 std::ostringstream buf;
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
932
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
933 if (nargin == 1)
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
934 // Let list_in_columns query terminal width.
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
935 s.list_in_columns (buf);
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
936 else
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
937 {
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
938 int width = args(1).int_value ();
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
939
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
940 if (! error_state)
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
941 s.list_in_columns (buf, width);
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
942 else
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
943 error ("list_in_columns: expecting width to be an integer");
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
944 }
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
945
5765
7ba9ad1fec11 [project @ 2006-04-17 05:05:15 by jwe]
jwe
parents: 5707
diff changeset
946 retval = buf.str ();
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
947 }
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
948 else
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
949 error ("list_in_columns: expecting cellstr or char array");
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
950 }
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
951 else
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5765
diff changeset
952 print_usage ();
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
953
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
954 return retval;
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
955 }
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5674
diff changeset
956
807
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
957 /*
8461
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
958 %!error <Invalid call to list_in_columns.*> list_in_columns ();
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
959 %!error <Invalid call to list_in_columns.*> list_in_columns (["abc", "def"], 20, 2);
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
960 %!error <invalid conversion from string to real scalar.*> list_in_columns (["abc", "def"], "a");
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
961 %!test
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
962 %! input = {"abc", "def", "ghijkl", "mnop", "qrs", "tuv"};
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
963 %! result = "abc mnop\ndef qrs\nghijkl tuv\n";
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
964 %! assert (list_in_columns (input, 20) == result);
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
965 %!test
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
966 %! input = ["abc"; "def"; "ghijkl"; "mnop"; "qrs"; "tuv"];
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
967 %! result = "abc mnop \ndef qrs \nghijkl tuv \n";
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
968 %! assert (list_in_columns (input, 20) == result);
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
969 */
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
970
9d456730b7a8 strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8372
diff changeset
971 /*
807
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
972 ;;; Local Variables: ***
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
973 ;;; mode: C++ ***
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
974 ;;; End: ***
c03b179482fc [project @ 1994-10-14 14:21:58 by jwe]
jwe
parents:
diff changeset
975 */