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\ |
|
152 @strong{Caution:} For compatibility with @sc{Matlab}, Octave's strcmp\n\ |
|
153 function returns 1 if the strings are equal, and 0 otherwise. This is\n\ |
|
154 just the opposite of the corresponding C library function.\n\ |
|
155 @end deftypefn") |
|
156 { |
5416
|
157 octave_value retval; |
5415
|
158 |
5416
|
159 if (args.length () == 2) |
|
160 { |
|
161 bool s1_string = args(0).is_string (); |
|
162 bool s1_cell = args(0).is_cell (); |
|
163 bool s2_string = args(1).is_string (); |
|
164 bool s2_cell = args(1).is_cell (); |
5415
|
165 |
5416
|
166 if (s1_string && s2_string) |
|
167 { |
|
168 // Must match exactly in all dimensions. |
|
169 |
|
170 const dim_vector dv1 = args(0).dims (); |
|
171 const dim_vector dv2 = args(1).dims (); |
5415
|
172 |
5416
|
173 if (dv1.length () == dv2.length ()) |
|
174 { |
|
175 for (int i = 0; i < dv1.length (); i++) |
|
176 { |
|
177 if (dv1(i) != dv2(i)) |
|
178 { |
|
179 retval = false; |
|
180 return retval; |
|
181 } |
|
182 } |
5415
|
183 |
5416
|
184 if (dv1(0) == 0) |
|
185 retval = true; |
|
186 else |
|
187 { |
|
188 charNDArray s1 = args(0).char_array_value (); |
|
189 charNDArray s2 = args(1).char_array_value (); |
5415
|
190 |
5416
|
191 for (int i = 0; i < dv1.numel (); i++) |
|
192 { |
|
193 if (s1(i) != s2(i)) |
|
194 { |
|
195 retval = false; |
|
196 return retval; |
|
197 } |
|
198 } |
5415
|
199 |
5416
|
200 retval = true; |
|
201 } |
5415
|
202 } |
|
203 } |
5416
|
204 else if ((s1_string && s2_cell) || (s1_cell && s2_string)) |
5415
|
205 { |
5416
|
206 string_vector str; |
|
207 Cell cell; |
|
208 int r; |
5415
|
209 |
5416
|
210 if (s1_string) |
|
211 { |
|
212 str = args(0).all_strings (); |
|
213 r = args(0).rows (); |
|
214 cell = args(1).cell_value (); |
|
215 } |
|
216 else |
|
217 { |
|
218 str = args(1).all_strings (); |
|
219 r = args(1).rows (); |
|
220 cell = args(0).cell_value (); |
|
221 } |
|
222 |
|
223 if (r == 1) |
|
224 { |
|
225 // Broadcast the string. |
|
226 |
|
227 boolNDArray output (cell.dimensions); |
5415
|
228 |
5416
|
229 for (int i = 0; i < cell.length (); i++) |
|
230 if (cell(i).is_string ()) |
|
231 output(i) = (cell(i).string_value () == str[0]); |
|
232 else |
|
233 output(i) = false; |
|
234 |
|
235 retval = output; |
|
236 } |
|
237 else if (r > 1) |
5415
|
238 { |
5416
|
239 if (cell.length () == 1) |
5415
|
240 { |
5416
|
241 // Broadcast the cell. |
|
242 |
|
243 const dim_vector dv (r, 1); |
|
244 boolNDArray output (dv); |
|
245 |
|
246 if (cell(0).is_string ()) |
|
247 { |
|
248 const std::string str2 = cell(0).string_value (); |
5415
|
249 |
5416
|
250 for (int i = 0; i < r; i++) |
|
251 output(i) = (str[i] == str2); |
|
252 } |
|
253 else |
|
254 { |
|
255 for (int i = 0; i < r; i++) |
|
256 output(i) = false; |
|
257 } |
|
258 |
|
259 retval = output; |
5415
|
260 } |
|
261 else |
|
262 { |
5416
|
263 // Must match in all dimensions. |
|
264 |
|
265 boolNDArray output (cell.dimensions); |
5415
|
266 |
5416
|
267 if (cell.length () == r) |
|
268 { |
|
269 for (int i = 0; i < r; i++) |
|
270 if (cell(i).is_string ()) |
|
271 output(i) = (str[i] == cell(i).string_value ()); |
|
272 else |
|
273 output(i) = false; |
5415
|
274 |
5416
|
275 retval = output; |
|
276 } |
|
277 else |
|
278 retval = false; |
5415
|
279 } |
|
280 } |
|
281 } |
5416
|
282 else if (s1_cell && s2_cell) |
5415
|
283 { |
5416
|
284 Cell cell1; |
|
285 Cell cell2; |
5415
|
286 |
5416
|
287 int r1 = args(0).numel (); |
|
288 int r2; |
|
289 |
|
290 if (r1 == 1) |
|
291 { |
|
292 // Make the singleton cell2. |
5415
|
293 |
5416
|
294 cell1 = args(1).cell_value (); |
|
295 cell2 = args(0).cell_value (); |
|
296 r1 = cell1.length (); |
|
297 r2 = 1; |
|
298 } |
|
299 else |
|
300 { |
|
301 cell1 = args(0).cell_value (); |
|
302 cell2 = args(1).cell_value (); |
|
303 r2 = cell2.length (); |
|
304 } |
5415
|
305 |
5416
|
306 const dim_vector size1 = cell1.dimensions; |
|
307 const dim_vector size2 = cell2.dimensions; |
|
308 |
|
309 boolNDArray output (size1); |
|
310 |
|
311 if (r2 == 1) |
|
312 { |
|
313 // Broadcast cell2. |
|
314 |
|
315 if (! cell2(0).is_string ()) |
5415
|
316 { |
5416
|
317 for (int i = 0; i < r1; i++) |
|
318 output(i) = false; |
5415
|
319 } |
|
320 else |
5416
|
321 { |
|
322 const std::string str2 = cell2(0).string_value (); |
|
323 |
|
324 for (int i = 0; i < r1; i++) |
|
325 { |
|
326 if (cell1(i).is_string ()) |
|
327 { |
|
328 const std::string str1 = cell1(i).string_value (); |
|
329 output(i) = (str1 == str2); |
|
330 } |
|
331 else |
|
332 output(i) = false; |
|
333 } |
|
334 } |
5415
|
335 } |
5416
|
336 else |
|
337 { |
|
338 if (size1 != size2) |
|
339 { |
|
340 error ("strcmp: nonconformant cell arrays"); |
|
341 return retval; |
|
342 } |
|
343 |
|
344 for (int i = 0; i < r1; i++) |
|
345 { |
|
346 if (cell1(i).is_string () && cell2(i).is_string ()) |
|
347 { |
|
348 const std::string str1 = cell1(i).string_value (); |
|
349 const std::string str2 = cell2(i).string_value (); |
|
350 output(i) = (str1 == str2); |
|
351 } |
|
352 else |
|
353 output(i) = false; |
|
354 } |
|
355 } |
|
356 |
|
357 retval = output; |
5415
|
358 } |
|
359 } |
5416
|
360 else |
|
361 print_usage ("strcmp"); |
5415
|
362 |
|
363 return retval; |
|
364 } |
|
365 |
807
|
366 /* |
|
367 ;;; Local Variables: *** |
|
368 ;;; mode: C++ *** |
|
369 ;;; End: *** |
|
370 */ |