807
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
807
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
807
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
807
|
26 #endif |
|
27 |
1355
|
28 #include <cctype> |
807
|
29 |
|
30 #include "dMatrix.h" |
|
31 |
1355
|
32 #include "defun.h" |
807
|
33 #include "error.h" |
|
34 #include "gripes.h" |
2366
|
35 #include "ov.h" |
1355
|
36 #include "oct-obj.h" |
4457
|
37 #include "unwind-prot.h" |
807
|
38 #include "utils.h" |
|
39 |
4358
|
40 DEFUN (char, args, , |
|
41 "-*- texinfo -*-\n\ |
|
42 @deftypefn {Built-in Function} {} char (@var{x})\n\ |
|
43 @deftypefnx {Built-in Function} {} char (@var{cell_array})\n\ |
|
44 @deftypefnx {Built-in Function} {} char (@var{s1}, @var{s2}, @dots{})\n\ |
|
45 Create a string array from a numeric matrix, cell array, or list of\n\ |
|
46 \n\ |
|
47 If the argument is a numeric matrix, each element of the matrix is\n\ |
|
48 converted to the corresponding ASCII character. For example,\n\ |
|
49 \n\ |
|
50 @example\n\ |
|
51 @group\n\ |
4734
|
52 char ([97, 98, 99])\n\ |
4358
|
53 @result{} \"abc\"\n\ |
|
54 @end group\n\ |
|
55 @end example\n\ |
|
56 \n\ |
|
57 If the argument is a cell array of strings, the result is a string array\n\ |
|
58 with each element corresponding to one element of the cell array.\n\ |
|
59 \n\ |
|
60 For multiple string arguments, the result is a string array with each\n\ |
|
61 element corresponding to the arguments.\n\ |
|
62 \n\ |
|
63 The returned values are padded with blanks as needed to make each row\n\ |
|
64 of the string array have the same length.\n\ |
|
65 @end deftypefn") |
|
66 { |
|
67 octave_value retval; |
|
68 |
|
69 int nargin = args.length (); |
|
70 |
|
71 if (nargin == 1) |
5281
|
72 retval = args(0).convert_to_str (true, true, |
|
73 args(0).is_dq_string () ? '"' : '\''); |
4358
|
74 else if (nargin > 1) |
|
75 { |
|
76 int n_elts = 0; |
|
77 |
|
78 int max_len = 0; |
|
79 |
|
80 for (int i = 0; i < nargin; i++) |
|
81 { |
4457
|
82 string_vector s = args(i).all_strings (false, true); |
4358
|
83 |
|
84 if (error_state) |
|
85 { |
4457
|
86 error ("char: unable to convert some args to strings"); |
4358
|
87 return retval; |
|
88 } |
|
89 |
|
90 n_elts += s.length (); |
|
91 |
|
92 int s_max_len = s.max_length (); |
|
93 |
|
94 if (s_max_len > max_len) |
|
95 max_len = s_max_len; |
|
96 } |
|
97 |
|
98 string_vector result (n_elts); |
|
99 |
|
100 int k = 0; |
|
101 |
|
102 for (int i = 0; i < nargin; i++) |
|
103 { |
4457
|
104 string_vector s = args(i).all_strings (false, true); |
4358
|
105 |
|
106 int n = s.length (); |
|
107 |
|
108 for (int j = 0; j < n; j++) |
|
109 { |
|
110 std::string t = s[j]; |
|
111 int t_len = t.length (); |
|
112 |
|
113 if (max_len > t_len) |
|
114 t += std::string (max_len - t_len, ' '); |
|
115 |
|
116 result[k++] = t; |
|
117 } |
|
118 } |
|
119 |
5280
|
120 retval = octave_value (result, '\''); |
4358
|
121 } |
|
122 else |
|
123 print_usage ("char"); |
|
124 |
|
125 return retval; |
|
126 } |
|
127 |
4535
|
128 DEFUN (ischar, args, , |
3361
|
129 "-*- texinfo -*-\n\ |
4535
|
130 @deftypefn {Built-in Function} {} ischar (@var{a})\n\ |
3361
|
131 Return 1 if @var{a} is a string. Otherwise, return 0.\n\ |
|
132 @end deftypefn") |
807
|
133 { |
4233
|
134 octave_value retval; |
807
|
135 |
|
136 int nargin = args.length (); |
|
137 |
|
138 if (nargin == 1 && args(0).is_defined ()) |
4233
|
139 retval = args(0).is_string (); |
807
|
140 else |
4535
|
141 print_usage ("ischar"); |
807
|
142 |
|
143 return retval; |
|
144 } |
|
145 |
5415
|
146 DEFUN (strcmp, args, , |
|
147 "-*- texinfo -*-\n\ |
|
148 @deftypefn {Function File} {} strcmp (@var{s1}, @var{s2})\n\ |
|
149 Return 1 if the character strings @var{s1} and @var{s2} are the same,\n\ |
|
150 and 0 otherwise.\n\ |
|
151 @strong{Caution:} For compatibility with @sc{Matlab}, Octave's strcmp\n\ |
|
152 function returns 1 if the strings are equal, and 0 otherwise. This is\n\ |
|
153 just the opposite of the corresponding C library function.\n\ |
|
154 @end deftypefn") |
|
155 { |
|
156 if (args.length () != 2) |
|
157 { |
|
158 usage ("strcmp (S, T)"); |
|
159 return octave_value(); |
|
160 } |
|
161 |
|
162 octave_value retval = false; |
|
163 |
|
164 bool s1_string = args(0).is_string (); |
|
165 bool s1_cell = args(0).is_cell (); |
|
166 bool s2_string = args(1).is_string (); |
|
167 bool s2_cell = args(1).is_cell (); |
|
168 |
|
169 if (s1_string && s2_string) // must match exactly in all dimensions |
|
170 { |
|
171 const dim_vector dv1 = args(0).dims (); |
|
172 const dim_vector dv2 = args(1).dims (); |
|
173 |
|
174 if (dv1.length () == dv2.length ()) |
|
175 { |
|
176 for (int i = 0; i < dv1.length (); i++) |
|
177 if (dv1(i) != dv2(i)) |
|
178 return retval; |
|
179 |
|
180 if (dv1(0) == 0) |
|
181 retval = true; |
|
182 else |
|
183 { |
|
184 charNDArray s1 = args(0).char_array_value (); |
|
185 charNDArray s2 = args(1).char_array_value (); |
|
186 |
|
187 for (int i = 0; i < dv1.numel (); i++) |
|
188 if (s1(i) != s2(i)) |
|
189 return retval; |
|
190 |
|
191 retval = true; |
|
192 } |
|
193 } |
|
194 } |
|
195 else if ((s1_string && s2_cell) || (s1_cell && s2_string)) |
|
196 { |
|
197 string_vector str; |
|
198 Cell cell; |
|
199 int r; |
|
200 |
|
201 if (s1_string) |
|
202 { |
|
203 str = args(0).all_strings (); |
|
204 r = args(0).rows (); |
|
205 cell = args(1).cell_value (); |
|
206 } |
|
207 else |
|
208 { |
|
209 str = args(1).all_strings (); |
|
210 r = args(1).rows (); |
|
211 cell = args(0).cell_value (); |
|
212 } |
|
213 |
|
214 if (r == 1) // broadcast the string |
|
215 { |
|
216 boolNDArray output (cell.dimensions); |
|
217 |
|
218 for (int i = 0; i < cell.length (); i++) |
|
219 if (cell(i).is_string ()) |
|
220 output(i) = (cell(i).string_value () == str[0]); |
|
221 else |
|
222 output(i) = false; |
|
223 |
|
224 retval = octave_value (output); |
|
225 } |
|
226 else if (r > 1) |
|
227 { |
|
228 if (cell.length () == 1) // broadcast the cell |
|
229 { |
|
230 const dim_vector& dv = dim_vector (r, 1); |
|
231 boolNDArray output (dv); |
|
232 |
|
233 if (cell(0).is_string ()) |
|
234 { |
|
235 const string str2 = cell(0).string_value (); |
|
236 |
|
237 for (int i = 0; i < r; i++) |
|
238 output(i) = (str[i] == str2); |
|
239 } |
|
240 else |
|
241 { |
|
242 for (int i = 0; i < r; i++) |
|
243 output(i) = false; |
|
244 } |
|
245 |
|
246 retval = octave_value (output); |
|
247 } |
|
248 else // must match in all dimensions |
|
249 { |
|
250 boolNDArray output (cell.dimensions); |
|
251 |
|
252 if (cell.length () == r) |
|
253 { |
|
254 for (int i = 0; i < r; i++) |
|
255 if (cell(i).is_string ()) |
|
256 output(i) = (str[i] == cell(i).string_value ()); |
|
257 else |
|
258 output(i) = false; |
|
259 retval = octave_value (output); |
|
260 } |
|
261 } |
|
262 } |
|
263 } |
|
264 else if (s1_cell && s2_cell) |
|
265 { |
|
266 Cell cell1; |
|
267 Cell cell2; |
|
268 |
|
269 int r1 = args(0).numel (); |
|
270 int r2; |
|
271 |
|
272 if (r1 == 1) // make the singleton cell2 |
|
273 { |
|
274 cell1 = args(1).cell_value (); |
|
275 cell2 = args(0).cell_value (); |
|
276 r1 = cell1.length (); |
|
277 r2 = 1; |
|
278 } |
|
279 else |
|
280 { |
|
281 cell1 = args(0).cell_value (); |
|
282 cell2 = args(1).cell_value (); |
|
283 r2 = cell2.length (); |
|
284 } |
|
285 |
|
286 const dim_vector size1 = cell1.dimensions; |
|
287 const dim_vector size2 = cell2.dimensions; |
|
288 |
|
289 boolNDArray output (size1); |
|
290 |
|
291 if (r2 == 1) // broadcast cell2 |
|
292 if (! cell2(0).is_string ()) |
|
293 for (int i = 0; i < r1; i++) |
|
294 output(i) = false; |
|
295 else |
|
296 { |
|
297 const string str2 = cell2(0).string_value (); |
|
298 |
|
299 for (int i = 0; i < r1; i++) |
|
300 if (cell1(i).is_string ()) |
|
301 { |
|
302 const string str1 = cell1(i).string_value (); |
|
303 output(i) = (str1 == str2); |
|
304 } |
|
305 else |
|
306 output(i) = false; |
|
307 } |
|
308 else |
|
309 { |
|
310 if (size1 != size2) |
|
311 { |
|
312 error ("strcmp: nonconformant cell arrays"); |
|
313 return octave_value (); |
|
314 } |
|
315 |
|
316 for (int i = 0; i < r1; i++) |
|
317 if (cell1(i).is_string () && cell2(i).is_string ()) |
|
318 { |
|
319 const string str1 = cell1(i).string_value (); |
|
320 const string str2 = cell2(i).string_value (); |
|
321 output(i) = (str1 == str2); |
|
322 } |
|
323 else |
|
324 output(i) = false; |
|
325 } |
|
326 |
|
327 retval = octave_value (output); |
|
328 } |
|
329 |
|
330 return retval; |
|
331 } |
|
332 |
807
|
333 /* |
|
334 ;;; Local Variables: *** |
|
335 ;;; mode: C++ *** |
|
336 ;;; End: *** |
|
337 */ |