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 |
5416
|
32 #include "Cell.h" |
1355
|
33 #include "defun.h" |
807
|
34 #include "error.h" |
|
35 #include "gripes.h" |
2366
|
36 #include "ov.h" |
1355
|
37 #include "oct-obj.h" |
4457
|
38 #include "unwind-prot.h" |
807
|
39 #include "utils.h" |
|
40 |
4358
|
41 DEFUN (char, args, , |
|
42 "-*- texinfo -*-\n\ |
|
43 @deftypefn {Built-in Function} {} char (@var{x})\n\ |
|
44 @deftypefnx {Built-in Function} {} char (@var{cell_array})\n\ |
|
45 @deftypefnx {Built-in Function} {} char (@var{s1}, @var{s2}, @dots{})\n\ |
|
46 Create a string array from a numeric matrix, cell array, or list of\n\ |
|
47 \n\ |
|
48 If the argument is a numeric matrix, each element of the matrix is\n\ |
|
49 converted to the corresponding ASCII character. For example,\n\ |
|
50 \n\ |
|
51 @example\n\ |
|
52 @group\n\ |
4734
|
53 char ([97, 98, 99])\n\ |
4358
|
54 @result{} \"abc\"\n\ |
|
55 @end group\n\ |
|
56 @end example\n\ |
|
57 \n\ |
|
58 If the argument is a cell array of strings, the result is a string array\n\ |
|
59 with each element corresponding to one element of the cell array.\n\ |
|
60 \n\ |
|
61 For multiple string arguments, the result is a string array with each\n\ |
|
62 element corresponding to the arguments.\n\ |
|
63 \n\ |
|
64 The returned values are padded with blanks as needed to make each row\n\ |
|
65 of the string array have the same length.\n\ |
|
66 @end deftypefn") |
|
67 { |
|
68 octave_value retval; |
|
69 |
|
70 int nargin = args.length (); |
|
71 |
|
72 if (nargin == 1) |
5281
|
73 retval = args(0).convert_to_str (true, true, |
|
74 args(0).is_dq_string () ? '"' : '\''); |
4358
|
75 else if (nargin > 1) |
|
76 { |
|
77 int n_elts = 0; |
|
78 |
|
79 int max_len = 0; |
|
80 |
|
81 for (int i = 0; i < nargin; i++) |
|
82 { |
4457
|
83 string_vector s = args(i).all_strings (false, true); |
4358
|
84 |
|
85 if (error_state) |
|
86 { |
4457
|
87 error ("char: unable to convert some args to strings"); |
4358
|
88 return retval; |
|
89 } |
|
90 |
|
91 n_elts += s.length (); |
|
92 |
|
93 int s_max_len = s.max_length (); |
|
94 |
|
95 if (s_max_len > max_len) |
|
96 max_len = s_max_len; |
|
97 } |
|
98 |
|
99 string_vector result (n_elts); |
|
100 |
|
101 int k = 0; |
|
102 |
|
103 for (int i = 0; i < nargin; i++) |
|
104 { |
4457
|
105 string_vector s = args(i).all_strings (false, true); |
4358
|
106 |
|
107 int n = s.length (); |
|
108 |
|
109 for (int j = 0; j < n; j++) |
|
110 { |
|
111 std::string t = s[j]; |
|
112 int t_len = t.length (); |
|
113 |
|
114 if (max_len > t_len) |
|
115 t += std::string (max_len - t_len, ' '); |
|
116 |
|
117 result[k++] = t; |
|
118 } |
|
119 } |
|
120 |
5280
|
121 retval = octave_value (result, '\''); |
4358
|
122 } |
|
123 else |
|
124 print_usage ("char"); |
|
125 |
|
126 return retval; |
|
127 } |
|
128 |
4535
|
129 DEFUN (ischar, args, , |
3361
|
130 "-*- texinfo -*-\n\ |
4535
|
131 @deftypefn {Built-in Function} {} ischar (@var{a})\n\ |
3361
|
132 Return 1 if @var{a} is a string. Otherwise, return 0.\n\ |
|
133 @end deftypefn") |
807
|
134 { |
4233
|
135 octave_value retval; |
807
|
136 |
|
137 int nargin = args.length (); |
|
138 |
|
139 if (nargin == 1 && args(0).is_defined ()) |
4233
|
140 retval = args(0).is_string (); |
807
|
141 else |
4535
|
142 print_usage ("ischar"); |
807
|
143 |
|
144 return retval; |
|
145 } |
|
146 |
5415
|
147 DEFUN (strcmp, args, , |
|
148 "-*- texinfo -*-\n\ |
|
149 @deftypefn {Function File} {} strcmp (@var{s1}, @var{s2})\n\ |
|
150 Return 1 if the character strings @var{s1} and @var{s2} are the same,\n\ |
|
151 and 0 otherwise.\n\ |
5674
|
152 \n\ |
|
153 If either @var{s1} or @var{s2} is a cell array of strings, then an array\n\ |
|
154 of the same size is returned, containing the values described above for\n\ |
|
155 every member of the cell array. The other argument may also be a cell\n\ |
|
156 array of strings (of the same size or with only one element), char matrix\n\ |
|
157 or character string.\n\ |
|
158 \n\ |
|
159 @strong{Caution:} For compatibility with @sc{Matlab}, Octave's strcmp\n\ |
|
160 function returns 1 if the character strings are equal, and 0 otherwise.\n\ |
|
161 This is just the opposite of the corresponding C library function.\n\ |
|
162 @seealso{strcmpi, strncmp, strncmpi}\n\ |
5415
|
163 @end deftypefn") |
|
164 { |
5531
|
165 octave_value retval; |
5415
|
166 |
5416
|
167 if (args.length () == 2) |
|
168 { |
|
169 bool s1_string = args(0).is_string (); |
|
170 bool s1_cell = args(0).is_cell (); |
|
171 bool s2_string = args(1).is_string (); |
|
172 bool s2_cell = args(1).is_cell (); |
5415
|
173 |
5416
|
174 if (s1_string && s2_string) |
|
175 { |
|
176 // Must match exactly in all dimensions. |
|
177 |
|
178 const dim_vector dv1 = args(0).dims (); |
|
179 const dim_vector dv2 = args(1).dims (); |
5415
|
180 |
5416
|
181 if (dv1.length () == dv2.length ()) |
|
182 { |
|
183 for (int i = 0; i < dv1.length (); i++) |
|
184 { |
|
185 if (dv1(i) != dv2(i)) |
|
186 { |
|
187 retval = false; |
|
188 return retval; |
|
189 } |
|
190 } |
5415
|
191 |
5416
|
192 if (dv1(0) == 0) |
|
193 retval = true; |
|
194 else |
|
195 { |
|
196 charNDArray s1 = args(0).char_array_value (); |
|
197 charNDArray s2 = args(1).char_array_value (); |
5415
|
198 |
5416
|
199 for (int i = 0; i < dv1.numel (); i++) |
|
200 { |
|
201 if (s1(i) != s2(i)) |
|
202 { |
|
203 retval = false; |
|
204 return retval; |
|
205 } |
|
206 } |
5415
|
207 |
5416
|
208 retval = true; |
|
209 } |
5415
|
210 } |
|
211 } |
5416
|
212 else if ((s1_string && s2_cell) || (s1_cell && s2_string)) |
5415
|
213 { |
5416
|
214 string_vector str; |
|
215 Cell cell; |
|
216 int r; |
5415
|
217 |
5416
|
218 if (s1_string) |
|
219 { |
|
220 str = args(0).all_strings (); |
|
221 r = args(0).rows (); |
|
222 cell = args(1).cell_value (); |
|
223 } |
|
224 else |
|
225 { |
|
226 str = args(1).all_strings (); |
|
227 r = args(1).rows (); |
|
228 cell = args(0).cell_value (); |
|
229 } |
|
230 |
|
231 if (r == 1) |
|
232 { |
|
233 // Broadcast the string. |
|
234 |
|
235 boolNDArray output (cell.dimensions); |
5415
|
236 |
5416
|
237 for (int i = 0; i < cell.length (); i++) |
|
238 if (cell(i).is_string ()) |
|
239 output(i) = (cell(i).string_value () == str[0]); |
|
240 else |
|
241 output(i) = false; |
|
242 |
|
243 retval = output; |
|
244 } |
|
245 else if (r > 1) |
5415
|
246 { |
5416
|
247 if (cell.length () == 1) |
5415
|
248 { |
5416
|
249 // Broadcast the cell. |
|
250 |
|
251 const dim_vector dv (r, 1); |
|
252 boolNDArray output (dv); |
|
253 |
|
254 if (cell(0).is_string ()) |
|
255 { |
|
256 const std::string str2 = cell(0).string_value (); |
5415
|
257 |
5416
|
258 for (int i = 0; i < r; i++) |
|
259 output(i) = (str[i] == str2); |
|
260 } |
|
261 else |
|
262 { |
|
263 for (int i = 0; i < r; i++) |
|
264 output(i) = false; |
|
265 } |
|
266 |
|
267 retval = output; |
5415
|
268 } |
|
269 else |
|
270 { |
5416
|
271 // Must match in all dimensions. |
|
272 |
|
273 boolNDArray output (cell.dimensions); |
5415
|
274 |
5416
|
275 if (cell.length () == r) |
|
276 { |
|
277 for (int i = 0; i < r; i++) |
|
278 if (cell(i).is_string ()) |
|
279 output(i) = (str[i] == cell(i).string_value ()); |
|
280 else |
|
281 output(i) = false; |
5415
|
282 |
5416
|
283 retval = output; |
|
284 } |
|
285 else |
|
286 retval = false; |
5415
|
287 } |
|
288 } |
|
289 } |
5416
|
290 else if (s1_cell && s2_cell) |
5415
|
291 { |
5416
|
292 Cell cell1; |
|
293 Cell cell2; |
5415
|
294 |
5416
|
295 int r1 = args(0).numel (); |
|
296 int r2; |
|
297 |
|
298 if (r1 == 1) |
|
299 { |
|
300 // Make the singleton cell2. |
5415
|
301 |
5416
|
302 cell1 = args(1).cell_value (); |
|
303 cell2 = args(0).cell_value (); |
|
304 r1 = cell1.length (); |
|
305 r2 = 1; |
|
306 } |
|
307 else |
|
308 { |
|
309 cell1 = args(0).cell_value (); |
|
310 cell2 = args(1).cell_value (); |
|
311 r2 = cell2.length (); |
|
312 } |
5415
|
313 |
5416
|
314 const dim_vector size1 = cell1.dimensions; |
|
315 const dim_vector size2 = cell2.dimensions; |
|
316 |
|
317 boolNDArray output (size1); |
|
318 |
|
319 if (r2 == 1) |
|
320 { |
|
321 // Broadcast cell2. |
|
322 |
|
323 if (! cell2(0).is_string ()) |
5415
|
324 { |
5416
|
325 for (int i = 0; i < r1; i++) |
|
326 output(i) = false; |
5415
|
327 } |
|
328 else |
5416
|
329 { |
|
330 const std::string str2 = cell2(0).string_value (); |
|
331 |
|
332 for (int i = 0; i < r1; i++) |
|
333 { |
|
334 if (cell1(i).is_string ()) |
|
335 { |
|
336 const std::string str1 = cell1(i).string_value (); |
|
337 output(i) = (str1 == str2); |
|
338 } |
|
339 else |
|
340 output(i) = false; |
|
341 } |
|
342 } |
5415
|
343 } |
5416
|
344 else |
|
345 { |
|
346 if (size1 != size2) |
|
347 { |
|
348 error ("strcmp: nonconformant cell arrays"); |
|
349 return retval; |
|
350 } |
|
351 |
|
352 for (int i = 0; i < r1; i++) |
|
353 { |
|
354 if (cell1(i).is_string () && cell2(i).is_string ()) |
|
355 { |
|
356 const std::string str1 = cell1(i).string_value (); |
|
357 const std::string str2 = cell2(i).string_value (); |
|
358 output(i) = (str1 == str2); |
|
359 } |
|
360 else |
|
361 output(i) = false; |
|
362 } |
|
363 } |
|
364 |
|
365 retval = output; |
5415
|
366 } |
5531
|
367 else |
|
368 retval = false; |
5415
|
369 } |
5416
|
370 else |
|
371 print_usage ("strcmp"); |
5415
|
372 |
|
373 return retval; |
|
374 } |
|
375 |
5690
|
376 DEFUN (list_in_columns, args, , |
|
377 "-*- texinfo -*-\n\ |
|
378 @deftypefn {Built-in Function} {} list_in_columns (@var{arg}, @var{width})\n\ |
|
379 Return a string containing the elements of @var{arg} listed in\n\ |
|
380 columns with an overall maximum width of @var{width}. The argument\n\ |
|
381 @var{arg} must be a cell array of character strings or a character array.\n\ |
|
382 If @var{width} is not specified, the width of the terminal screen is used.\n\ |
|
383 @seealso{terminal_size}\n\ |
|
384 @end deftypefn") |
|
385 { |
|
386 octave_value retval; |
|
387 |
|
388 int nargin = args.length (); |
|
389 |
|
390 if (nargin == 1 || nargin == 2) |
|
391 { |
|
392 string_vector s = args(0).all_strings (); |
|
393 |
|
394 if (! error_state) |
|
395 { |
|
396 OSSTREAM buf; |
|
397 |
|
398 if (nargin == 1) |
|
399 // Let list_in_columns query terminal width. |
|
400 s.list_in_columns (buf); |
|
401 else |
|
402 { |
|
403 int width = args(1).int_value (); |
|
404 |
|
405 if (! error_state) |
|
406 s.list_in_columns (buf, width); |
|
407 else |
|
408 error ("list_in_columns: expecting width to be an integer"); |
|
409 } |
|
410 |
|
411 buf << OSSTREAM_ENDS; |
|
412 |
|
413 retval = OSSTREAM_STR (buf); |
|
414 |
|
415 OSSTREAM_FREEZE (buf); |
|
416 } |
|
417 else |
|
418 error ("list_in_columns: expecting cellstr or char array"); |
|
419 } |
|
420 else |
|
421 print_usage ("list_in_columns"); |
|
422 |
|
423 return retval; |
|
424 } |
|
425 |
807
|
426 /* |
|
427 ;;; Local Variables: *** |
|
428 ;;; mode: C++ *** |
|
429 ;;; End: *** |
|
430 */ |