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 |
|
232 if (r == 1) |
|
233 { |
|
234 // Broadcast the string. |
|
235 |
|
236 boolNDArray output (cell.dimensions); |
5415
|
237 |
5416
|
238 for (int i = 0; i < cell.length (); i++) |
|
239 if (cell(i).is_string ()) |
|
240 output(i) = (cell(i).string_value () == str[0]); |
|
241 else |
|
242 output(i) = false; |
|
243 |
|
244 retval = output; |
|
245 } |
|
246 else if (r > 1) |
5415
|
247 { |
5416
|
248 if (cell.length () == 1) |
5415
|
249 { |
5416
|
250 // Broadcast the cell. |
|
251 |
|
252 const dim_vector dv (r, 1); |
|
253 boolNDArray output (dv); |
|
254 |
|
255 if (cell(0).is_string ()) |
|
256 { |
|
257 const std::string str2 = cell(0).string_value (); |
5415
|
258 |
5416
|
259 for (int i = 0; i < r; i++) |
|
260 output(i) = (str[i] == str2); |
|
261 } |
|
262 else |
|
263 { |
|
264 for (int i = 0; i < r; i++) |
|
265 output(i) = false; |
|
266 } |
|
267 |
|
268 retval = output; |
5415
|
269 } |
|
270 else |
|
271 { |
5416
|
272 // Must match in all dimensions. |
|
273 |
|
274 boolNDArray output (cell.dimensions); |
5415
|
275 |
5416
|
276 if (cell.length () == r) |
|
277 { |
|
278 for (int i = 0; i < r; i++) |
|
279 if (cell(i).is_string ()) |
|
280 output(i) = (str[i] == cell(i).string_value ()); |
|
281 else |
|
282 output(i) = false; |
5415
|
283 |
5416
|
284 retval = output; |
|
285 } |
|
286 else |
|
287 retval = false; |
5415
|
288 } |
|
289 } |
|
290 } |
5416
|
291 else if (s1_cell && s2_cell) |
5415
|
292 { |
5416
|
293 Cell cell1; |
|
294 Cell cell2; |
5415
|
295 |
5416
|
296 int r1 = args(0).numel (); |
|
297 int r2; |
|
298 |
|
299 if (r1 == 1) |
|
300 { |
|
301 // Make the singleton cell2. |
5415
|
302 |
5416
|
303 cell1 = args(1).cell_value (); |
|
304 cell2 = args(0).cell_value (); |
|
305 r1 = cell1.length (); |
|
306 r2 = 1; |
|
307 } |
|
308 else |
|
309 { |
|
310 cell1 = args(0).cell_value (); |
|
311 cell2 = args(1).cell_value (); |
|
312 r2 = cell2.length (); |
|
313 } |
5415
|
314 |
5416
|
315 const dim_vector size1 = cell1.dimensions; |
|
316 const dim_vector size2 = cell2.dimensions; |
|
317 |
|
318 boolNDArray output (size1); |
|
319 |
|
320 if (r2 == 1) |
|
321 { |
|
322 // Broadcast cell2. |
|
323 |
|
324 if (! cell2(0).is_string ()) |
5415
|
325 { |
5416
|
326 for (int i = 0; i < r1; i++) |
|
327 output(i) = false; |
5415
|
328 } |
|
329 else |
5416
|
330 { |
|
331 const std::string str2 = cell2(0).string_value (); |
|
332 |
|
333 for (int i = 0; i < r1; i++) |
|
334 { |
|
335 if (cell1(i).is_string ()) |
|
336 { |
|
337 const std::string str1 = cell1(i).string_value (); |
|
338 output(i) = (str1 == str2); |
|
339 } |
|
340 else |
|
341 output(i) = false; |
|
342 } |
|
343 } |
5415
|
344 } |
5416
|
345 else |
|
346 { |
|
347 if (size1 != size2) |
|
348 { |
|
349 error ("strcmp: nonconformant cell arrays"); |
|
350 return retval; |
|
351 } |
|
352 |
|
353 for (int i = 0; i < r1; i++) |
|
354 { |
|
355 if (cell1(i).is_string () && cell2(i).is_string ()) |
|
356 { |
|
357 const std::string str1 = cell1(i).string_value (); |
|
358 const std::string str2 = cell2(i).string_value (); |
|
359 output(i) = (str1 == str2); |
|
360 } |
|
361 else |
|
362 output(i) = false; |
|
363 } |
|
364 } |
|
365 |
|
366 retval = output; |
5415
|
367 } |
5531
|
368 else |
|
369 retval = false; |
5415
|
370 } |
5416
|
371 else |
5823
|
372 print_usage (); |
5415
|
373 |
|
374 return retval; |
|
375 } |
|
376 |
5690
|
377 DEFUN (list_in_columns, args, , |
|
378 "-*- texinfo -*-\n\ |
|
379 @deftypefn {Built-in Function} {} list_in_columns (@var{arg}, @var{width})\n\ |
|
380 Return a string containing the elements of @var{arg} listed in\n\ |
|
381 columns with an overall maximum width of @var{width}. The argument\n\ |
|
382 @var{arg} must be a cell array of character strings or a character array.\n\ |
|
383 If @var{width} is not specified, the width of the terminal screen is used.\n\ |
|
384 @seealso{terminal_size}\n\ |
|
385 @end deftypefn") |
|
386 { |
|
387 octave_value retval; |
|
388 |
|
389 int nargin = args.length (); |
|
390 |
|
391 if (nargin == 1 || nargin == 2) |
|
392 { |
|
393 string_vector s = args(0).all_strings (); |
|
394 |
|
395 if (! error_state) |
|
396 { |
5765
|
397 std::ostringstream buf; |
5690
|
398 |
|
399 if (nargin == 1) |
|
400 // Let list_in_columns query terminal width. |
|
401 s.list_in_columns (buf); |
|
402 else |
|
403 { |
|
404 int width = args(1).int_value (); |
|
405 |
|
406 if (! error_state) |
|
407 s.list_in_columns (buf, width); |
|
408 else |
|
409 error ("list_in_columns: expecting width to be an integer"); |
|
410 } |
|
411 |
5765
|
412 retval = buf.str (); |
5690
|
413 } |
|
414 else |
|
415 error ("list_in_columns: expecting cellstr or char array"); |
|
416 } |
|
417 else |
5823
|
418 print_usage (); |
5690
|
419 |
|
420 return retval; |
|
421 } |
|
422 |
807
|
423 /* |
|
424 ;;; Local Variables: *** |
|
425 ;;; mode: C++ *** |
|
426 ;;; End: *** |
|
427 */ |