807
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
807
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
807
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
807
|
26 #endif |
|
27 |
1355
|
28 #include <cctype> |
5765
|
29 #include <sstream> |
807
|
30 |
|
31 #include "dMatrix.h" |
|
32 |
5416
|
33 #include "Cell.h" |
1355
|
34 #include "defun.h" |
807
|
35 #include "error.h" |
|
36 #include "gripes.h" |
2366
|
37 #include "ov.h" |
1355
|
38 #include "oct-obj.h" |
4457
|
39 #include "unwind-prot.h" |
807
|
40 #include "utils.h" |
|
41 |
4358
|
42 DEFUN (char, args, , |
|
43 "-*- texinfo -*-\n\ |
|
44 @deftypefn {Built-in Function} {} char (@var{x})\n\ |
|
45 @deftypefnx {Built-in Function} {} char (@var{cell_array})\n\ |
|
46 @deftypefnx {Built-in Function} {} char (@var{s1}, @var{s2}, @dots{})\n\ |
|
47 Create a string array from a numeric matrix, cell array, or list of\n\ |
|
48 \n\ |
|
49 If the argument is a numeric matrix, each element of the matrix is\n\ |
|
50 converted to the corresponding ASCII character. For example,\n\ |
|
51 \n\ |
|
52 @example\n\ |
|
53 @group\n\ |
4734
|
54 char ([97, 98, 99])\n\ |
4358
|
55 @result{} \"abc\"\n\ |
|
56 @end group\n\ |
|
57 @end example\n\ |
|
58 \n\ |
|
59 If the argument is a cell array of strings, the result is a string array\n\ |
|
60 with each element corresponding to one element of the cell array.\n\ |
|
61 \n\ |
|
62 For multiple string arguments, the result is a string array with each\n\ |
|
63 element corresponding to the arguments.\n\ |
|
64 \n\ |
|
65 The returned values are padded with blanks as needed to make each row\n\ |
|
66 of the string array have the same length.\n\ |
|
67 @end deftypefn") |
|
68 { |
|
69 octave_value retval; |
|
70 |
|
71 int nargin = args.length (); |
|
72 |
|
73 if (nargin == 1) |
5281
|
74 retval = args(0).convert_to_str (true, true, |
|
75 args(0).is_dq_string () ? '"' : '\''); |
4358
|
76 else if (nargin > 1) |
|
77 { |
|
78 int n_elts = 0; |
|
79 |
|
80 int max_len = 0; |
|
81 |
|
82 for (int i = 0; i < nargin; i++) |
|
83 { |
5707
|
84 string_vector s = args(i).all_strings (); |
4358
|
85 |
|
86 if (error_state) |
|
87 { |
4457
|
88 error ("char: unable to convert some args to strings"); |
4358
|
89 return retval; |
|
90 } |
|
91 |
|
92 n_elts += s.length (); |
|
93 |
|
94 int s_max_len = s.max_length (); |
|
95 |
|
96 if (s_max_len > max_len) |
|
97 max_len = s_max_len; |
|
98 } |
|
99 |
|
100 string_vector result (n_elts); |
|
101 |
|
102 int k = 0; |
|
103 |
|
104 for (int i = 0; i < nargin; i++) |
|
105 { |
5707
|
106 string_vector s = args(i).all_strings (); |
4358
|
107 |
|
108 int n = s.length (); |
|
109 |
|
110 for (int j = 0; j < n; j++) |
|
111 { |
|
112 std::string t = s[j]; |
|
113 int t_len = t.length (); |
|
114 |
|
115 if (max_len > t_len) |
|
116 t += std::string (max_len - t_len, ' '); |
|
117 |
|
118 result[k++] = t; |
|
119 } |
|
120 } |
|
121 |
5280
|
122 retval = octave_value (result, '\''); |
4358
|
123 } |
|
124 else |
5823
|
125 print_usage (); |
4358
|
126 |
|
127 return retval; |
|
128 } |
|
129 |
4535
|
130 DEFUN (ischar, args, , |
3361
|
131 "-*- texinfo -*-\n\ |
4535
|
132 @deftypefn {Built-in Function} {} ischar (@var{a})\n\ |
3361
|
133 Return 1 if @var{a} is a string. Otherwise, return 0.\n\ |
|
134 @end deftypefn") |
807
|
135 { |
4233
|
136 octave_value retval; |
807
|
137 |
|
138 int nargin = args.length (); |
|
139 |
|
140 if (nargin == 1 && args(0).is_defined ()) |
4233
|
141 retval = args(0).is_string (); |
807
|
142 else |
5823
|
143 print_usage (); |
807
|
144 |
|
145 return retval; |
|
146 } |
|
147 |
5415
|
148 DEFUN (strcmp, args, , |
|
149 "-*- texinfo -*-\n\ |
|
150 @deftypefn {Function File} {} strcmp (@var{s1}, @var{s2})\n\ |
|
151 Return 1 if the character strings @var{s1} and @var{s2} are the same,\n\ |
|
152 and 0 otherwise.\n\ |
5674
|
153 \n\ |
|
154 If either @var{s1} or @var{s2} is a cell array of strings, then an array\n\ |
|
155 of the same size is returned, containing the values described above for\n\ |
|
156 every member of the cell array. The other argument may also be a cell\n\ |
|
157 array of strings (of the same size or with only one element), char matrix\n\ |
|
158 or character string.\n\ |
|
159 \n\ |
|
160 @strong{Caution:} For compatibility with @sc{Matlab}, Octave's strcmp\n\ |
|
161 function returns 1 if the character strings are equal, and 0 otherwise.\n\ |
|
162 This is just the opposite of the corresponding C library function.\n\ |
|
163 @seealso{strcmpi, strncmp, strncmpi}\n\ |
5415
|
164 @end deftypefn") |
|
165 { |
5531
|
166 octave_value retval; |
5415
|
167 |
5416
|
168 if (args.length () == 2) |
|
169 { |
|
170 bool s1_string = args(0).is_string (); |
|
171 bool s1_cell = args(0).is_cell (); |
|
172 bool s2_string = args(1).is_string (); |
|
173 bool s2_cell = args(1).is_cell (); |
5415
|
174 |
5416
|
175 if (s1_string && s2_string) |
|
176 { |
|
177 // Must match exactly in all dimensions. |
|
178 |
|
179 const dim_vector dv1 = args(0).dims (); |
|
180 const dim_vector dv2 = args(1).dims (); |
5415
|
181 |
5416
|
182 if (dv1.length () == dv2.length ()) |
|
183 { |
|
184 for (int i = 0; i < dv1.length (); i++) |
|
185 { |
|
186 if (dv1(i) != dv2(i)) |
|
187 { |
|
188 retval = false; |
|
189 return retval; |
|
190 } |
|
191 } |
5415
|
192 |
5416
|
193 if (dv1(0) == 0) |
|
194 retval = true; |
|
195 else |
|
196 { |
|
197 charNDArray s1 = args(0).char_array_value (); |
|
198 charNDArray s2 = args(1).char_array_value (); |
5415
|
199 |
5416
|
200 for (int i = 0; i < dv1.numel (); i++) |
|
201 { |
|
202 if (s1(i) != s2(i)) |
|
203 { |
|
204 retval = false; |
|
205 return retval; |
|
206 } |
|
207 } |
5415
|
208 |
5416
|
209 retval = true; |
|
210 } |
5415
|
211 } |
|
212 } |
5416
|
213 else if ((s1_string && s2_cell) || (s1_cell && s2_string)) |
5415
|
214 { |
5416
|
215 string_vector str; |
|
216 Cell cell; |
|
217 int r; |
5415
|
218 |
5416
|
219 if (s1_string) |
|
220 { |
|
221 str = args(0).all_strings (); |
|
222 r = args(0).rows (); |
|
223 cell = args(1).cell_value (); |
|
224 } |
|
225 else |
|
226 { |
|
227 str = args(1).all_strings (); |
|
228 r = args(1).rows (); |
|
229 cell = args(0).cell_value (); |
|
230 } |
|
231 |
5862
|
232 if (r == 0 || r == 1) |
5416
|
233 { |
|
234 // Broadcast the string. |
|
235 |
|
236 boolNDArray output (cell.dimensions); |
5415
|
237 |
5862
|
238 std::string s = r == 0 ? std::string () : str[0]; |
|
239 |
5416
|
240 for (int i = 0; i < cell.length (); i++) |
|
241 if (cell(i).is_string ()) |
5862
|
242 output(i) = (cell(i).string_value () == s); |
5416
|
243 else |
|
244 output(i) = false; |
|
245 |
|
246 retval = output; |
|
247 } |
|
248 else if (r > 1) |
5415
|
249 { |
5416
|
250 if (cell.length () == 1) |
5415
|
251 { |
5416
|
252 // Broadcast the cell. |
|
253 |
|
254 const dim_vector dv (r, 1); |
|
255 boolNDArray output (dv); |
|
256 |
|
257 if (cell(0).is_string ()) |
|
258 { |
|
259 const std::string str2 = cell(0).string_value (); |
5415
|
260 |
5416
|
261 for (int i = 0; i < r; i++) |
|
262 output(i) = (str[i] == str2); |
|
263 } |
|
264 else |
|
265 { |
|
266 for (int i = 0; i < r; i++) |
|
267 output(i) = false; |
|
268 } |
|
269 |
|
270 retval = output; |
5415
|
271 } |
|
272 else |
|
273 { |
5416
|
274 // Must match in all dimensions. |
|
275 |
|
276 boolNDArray output (cell.dimensions); |
5415
|
277 |
5416
|
278 if (cell.length () == r) |
|
279 { |
|
280 for (int i = 0; i < r; i++) |
|
281 if (cell(i).is_string ()) |
|
282 output(i) = (str[i] == cell(i).string_value ()); |
|
283 else |
|
284 output(i) = false; |
5415
|
285 |
5416
|
286 retval = output; |
|
287 } |
|
288 else |
|
289 retval = false; |
5415
|
290 } |
|
291 } |
|
292 } |
5416
|
293 else if (s1_cell && s2_cell) |
5415
|
294 { |
5416
|
295 Cell cell1; |
|
296 Cell cell2; |
5415
|
297 |
5416
|
298 int r1 = args(0).numel (); |
|
299 int r2; |
|
300 |
|
301 if (r1 == 1) |
|
302 { |
|
303 // Make the singleton cell2. |
5415
|
304 |
5416
|
305 cell1 = args(1).cell_value (); |
|
306 cell2 = args(0).cell_value (); |
|
307 r1 = cell1.length (); |
|
308 r2 = 1; |
|
309 } |
|
310 else |
|
311 { |
|
312 cell1 = args(0).cell_value (); |
|
313 cell2 = args(1).cell_value (); |
|
314 r2 = cell2.length (); |
|
315 } |
5415
|
316 |
5416
|
317 const dim_vector size1 = cell1.dimensions; |
|
318 const dim_vector size2 = cell2.dimensions; |
|
319 |
|
320 boolNDArray output (size1); |
|
321 |
|
322 if (r2 == 1) |
|
323 { |
|
324 // Broadcast cell2. |
|
325 |
|
326 if (! cell2(0).is_string ()) |
5415
|
327 { |
5416
|
328 for (int i = 0; i < r1; i++) |
|
329 output(i) = false; |
5415
|
330 } |
|
331 else |
5416
|
332 { |
|
333 const std::string str2 = cell2(0).string_value (); |
|
334 |
|
335 for (int i = 0; i < r1; i++) |
|
336 { |
|
337 if (cell1(i).is_string ()) |
|
338 { |
|
339 const std::string str1 = cell1(i).string_value (); |
|
340 output(i) = (str1 == str2); |
|
341 } |
|
342 else |
|
343 output(i) = false; |
|
344 } |
|
345 } |
5415
|
346 } |
5416
|
347 else |
|
348 { |
|
349 if (size1 != size2) |
|
350 { |
|
351 error ("strcmp: nonconformant cell arrays"); |
|
352 return retval; |
|
353 } |
|
354 |
|
355 for (int i = 0; i < r1; i++) |
|
356 { |
|
357 if (cell1(i).is_string () && cell2(i).is_string ()) |
|
358 { |
|
359 const std::string str1 = cell1(i).string_value (); |
|
360 const std::string str2 = cell2(i).string_value (); |
|
361 output(i) = (str1 == str2); |
|
362 } |
|
363 else |
|
364 output(i) = false; |
|
365 } |
|
366 } |
|
367 |
|
368 retval = output; |
5415
|
369 } |
5531
|
370 else |
|
371 retval = false; |
5415
|
372 } |
5416
|
373 else |
5823
|
374 print_usage (); |
5415
|
375 |
|
376 return retval; |
|
377 } |
|
378 |
5862
|
379 /* |
|
380 %!shared x |
|
381 %! x = char (zeros (0, 2)); |
|
382 %!assert (strcmp ('', x) == false); |
|
383 %!assert (strcmp (x, '') == false); |
|
384 %!assert (strcmp (x, x) == true); |
5911
|
385 ## %!assert (strcmp ({''}, x) == false); |
|
386 ## %!assert (strcmp ({x}, '') == false); |
|
387 ## %!assert (strcmp ({x}, x) == true); |
|
388 ## %!assert (strcmp ('', {x}) == false); |
|
389 ## %!assert (strcmp (x, {''}) == false); |
|
390 ## %!assert (strcmp (x, {x}) == true); |
|
391 ## %!assert (all (strcmp ({x; x}, '') == [false; false])); |
|
392 ## %!assert (all (strcmp ({x; x}, {''}) == [false; false])); |
|
393 ## %!assert (all (strcmp ('', {x; x}) == [false; false])); |
|
394 ## %!assert (all (strcmp ({''}, {x; x}) == [false; false])); |
5862
|
395 %!assert (strcmp ({'foo'}, x) == false); |
|
396 %!assert (strcmp ({'foo'}, 'foo') == true); |
|
397 %!assert (strcmp ({'foo'}, x) == false); |
|
398 %!assert (strcmp (x, {'foo'}) == false); |
|
399 %!assert (strcmp ('foo', {'foo'}) == true); |
|
400 %!assert (strcmp (x, {'foo'}) == false); |
|
401 %!shared y |
|
402 %! y = char (zeros (2, 0)); |
|
403 %!assert (strcmp ('', y) == false); |
|
404 %!assert (strcmp (y, '') == false); |
|
405 %!assert (strcmp (y, y) == true); |
|
406 %!assert (all (strcmp ({''}, y) == [true; true])); |
|
407 %!assert (strcmp ({y}, '') == true); |
|
408 %!assert (all (strcmp ({y}, y) == [true; true])); |
|
409 %!assert (all (strcmp ('', {y}) == [true; true])); |
|
410 %!assert (all (strcmp (y, {''}) == [true; true])); |
|
411 %!assert (all (strcmp (y, {y}) == [true; true])); |
5911
|
412 ## %!assert (all (strcmp ({y; y}, '') == [false; false])); |
|
413 ## %!assert (all (strcmp ({y; y}, {''}) == [false; false])); |
|
414 ## %!assert (all (strcmp ('', {y; y}) == [false; false])); |
|
415 ## %!assert (all (strcmp ({''}, {y; y}) == [false; false])); |
5862
|
416 %!assert (all (strcmp ({'foo'}, y) == [false; false])); |
|
417 %!assert (all (strcmp ({'foo'}, y) == [false; false])); |
|
418 %!assert (all (strcmp (y, {'foo'}) == [false; false])); |
|
419 %!assert (all (strcmp (y, {'foo'}) == [false; false])); |
|
420 */ |
|
421 |
5690
|
422 DEFUN (list_in_columns, args, , |
|
423 "-*- texinfo -*-\n\ |
|
424 @deftypefn {Built-in Function} {} list_in_columns (@var{arg}, @var{width})\n\ |
|
425 Return a string containing the elements of @var{arg} listed in\n\ |
|
426 columns with an overall maximum width of @var{width}. The argument\n\ |
|
427 @var{arg} must be a cell array of character strings or a character array.\n\ |
|
428 If @var{width} is not specified, the width of the terminal screen is used.\n\ |
|
429 @seealso{terminal_size}\n\ |
|
430 @end deftypefn") |
|
431 { |
|
432 octave_value retval; |
|
433 |
|
434 int nargin = args.length (); |
|
435 |
|
436 if (nargin == 1 || nargin == 2) |
|
437 { |
|
438 string_vector s = args(0).all_strings (); |
|
439 |
|
440 if (! error_state) |
|
441 { |
5765
|
442 std::ostringstream buf; |
5690
|
443 |
|
444 if (nargin == 1) |
|
445 // Let list_in_columns query terminal width. |
|
446 s.list_in_columns (buf); |
|
447 else |
|
448 { |
|
449 int width = args(1).int_value (); |
|
450 |
|
451 if (! error_state) |
|
452 s.list_in_columns (buf, width); |
|
453 else |
|
454 error ("list_in_columns: expecting width to be an integer"); |
|
455 } |
|
456 |
5765
|
457 retval = buf.str (); |
5690
|
458 } |
|
459 else |
|
460 error ("list_in_columns: expecting cellstr or char array"); |
|
461 } |
|
462 else |
5823
|
463 print_usage (); |
5690
|
464 |
|
465 return retval; |
|
466 } |
|
467 |
807
|
468 /* |
|
469 ;;; Local Variables: *** |
|
470 ;;; mode: C++ *** |
|
471 ;;; End: *** |
|
472 */ |