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