Mercurial > hg > octave-lyh
annotate src/strfns.cc @ 8462:ebdf1e058d85
Make strvcat an internal function
author | Thorsten Meyer <thorsten.meyier@gmx.de> |
---|---|
date | Mon, 12 Jan 2009 12:22:26 -0500 |
parents | 9d456730b7a8 |
children | a74871446af7 |
rev | line source |
---|---|
807 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1994, 1995, 1996, 1997, 1999, 2002, 2003, 2004, 2005, |
4 2006, 2007 John W. Eaton | |
807 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
807 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
807 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
1192 | 25 #include <config.h> |
807 | 26 #endif |
27 | |
1355 | 28 #include <cctype> |
7528
26d8a92644de
try to avoid ctype macro problems
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
29 |
5765 | 30 #include <sstream> |
807 | 31 |
32 #include "dMatrix.h" | |
33 | |
5416 | 34 #include "Cell.h" |
1355 | 35 #include "defun.h" |
807 | 36 #include "error.h" |
37 #include "gripes.h" | |
2366 | 38 #include "ov.h" |
1355 | 39 #include "oct-obj.h" |
4457 | 40 #include "unwind-prot.h" |
807 | 41 #include "utils.h" |
42 | |
4358 | 43 DEFUN (char, args, , |
44 "-*- texinfo -*-\n\ | |
45 @deftypefn {Built-in Function} {} char (@var{x})\n\ | |
46 @deftypefnx {Built-in Function} {} char (@var{cell_array})\n\ | |
47 @deftypefnx {Built-in Function} {} char (@var{s1}, @var{s2}, @dots{})\n\ | |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
48 Create a string array from one or more numeric matrices, character\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
49 matrices or cell arrays. For numerical input, each element is converted\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
50 to the corresponding ASCII character. The arguments (and elements of\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
51 cell array(s)) are concatenated vertically.\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
52 The returned values are padded with blanks as needed to make each row\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
53 of the string array have the same length. Empty strings are not removed.\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
54 For example,\n\ |
4358 | 55 \n\ |
56 @example\n\ | |
57 @group\n\ | |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
58 char ([97, 98, 99], \"\", @{\"98\", \"99\", 100@}, [\"num\", \"bers\"])\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
59 @result{} [\"abc \"\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
60 \" \"\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
61 \"98 \"\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
62 \"99 \"\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
63 \"d \"\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
64 \"numbers\"]\n\ |
4358 | 65 @end group\n\ |
66 @end example\n\ | |
67 \n\ | |
68 @end deftypefn") | |
69 { | |
70 octave_value retval; | |
71 | |
72 int nargin = args.length (); | |
73 | |
74 if (nargin == 1) | |
5281 | 75 retval = args(0).convert_to_str (true, true, |
76 args(0).is_dq_string () ? '"' : '\''); | |
4358 | 77 else if (nargin > 1) |
78 { | |
79 int n_elts = 0; | |
80 | |
81 int max_len = 0; | |
82 | |
83 for (int i = 0; i < nargin; i++) | |
84 { | |
5707 | 85 string_vector s = args(i).all_strings (); |
4358 | 86 |
87 if (error_state) | |
88 { | |
4457 | 89 error ("char: unable to convert some args to strings"); |
4358 | 90 return retval; |
91 } | |
92 | |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
93 if (s.length () > 0) |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
94 n_elts += s.length (); |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
95 else |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
96 n_elts += 1; |
4358 | 97 |
98 int s_max_len = s.max_length (); | |
99 | |
100 if (s_max_len > max_len) | |
101 max_len = s_max_len; | |
102 } | |
103 | |
104 string_vector result (n_elts); | |
105 | |
106 int k = 0; | |
107 | |
108 for (int i = 0; i < nargin; i++) | |
109 { | |
5707 | 110 string_vector s = args(i).all_strings (); |
4358 | 111 |
112 int n = s.length (); | |
113 | |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
114 if (n > 0) |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
115 { |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
116 for (int j = 0; j < n; j++) |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
117 { |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
118 std::string t = s[j]; |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
119 int t_len = t.length (); |
4358 | 120 |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
121 if (max_len > t_len) |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
122 t += std::string (max_len - t_len, ' '); |
4358 | 123 |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
124 result[k++] = t; |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
125 } |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
126 } |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
127 else |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
128 { |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
129 result[k++] = std::string (max_len, ' '); |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
130 } |
4358 | 131 } |
132 | |
5280 | 133 retval = octave_value (result, '\''); |
4358 | 134 } |
135 else | |
5823 | 136 print_usage (); |
4358 | 137 |
138 return retval; | |
139 } | |
140 | |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
141 /* |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
142 %!error <Invalid call to char> char() |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
143 %!assert (char (100) == "d"); |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
144 %!assert (all(char (100,100) == ["d";"d"])); |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
145 %!assert (all(char ({100,100}) == ["d";"d"])); |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
146 %!assert (all(char ([100,100]) == ["dd"])); |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
147 %!assert (all(char ({100,{100}}) == ["d";"d"])); |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
148 %!assert (all(char (100, [], 100) == ["d";" ";"d"])) |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
149 %!assert (all(char ({100, [], 100}) == ["d";" ";"d"])) |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
150 %!assert (all(char ({100,{100, {""}}}) == ["d";"d";" "])) |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
151 %!assert (all(char (["a";"be"], {"c", 100}) == ["a";"be";"c";"d"])) |
8372
8dff9cba15fe
move str2mat to deprecated and make it a simple wrapper around char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8353
diff
changeset
|
152 %!assert(strcmp (char ("a", "bb", "ccc"), ["a "; "bb "; "ccc"])); |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
153 */ |
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
154 |
8462
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
155 DEFUN (strvcat, args, , |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
156 "-*- texinfo -*-\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
157 @deftypefn {Built-in Function} {} strvcat (@var{x})\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
158 @deftypefnx {Built-in Function} {} strvcat (@var{cell_array})\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
159 @deftypefnx {Built-in Function} {} strvcat (@var{s1}, @var{s2}, @dots{})\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
160 Create a character array from one or more numeric matrices, character\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
161 matrices or cell arrays. For numerical input, each element is converted\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
162 to the corresponding ASCII character. The arguments (and elements of\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
163 cell array(s)) are concatenated vertically.\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
164 The returned values are padded with blanks as needed to make each row\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
165 of the string array have the same length. Unlike @code{char}, empty\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
166 strings are removed.\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
167 For example,\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
168 \n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
169 @example\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
170 @group\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
171 strvcat ([97, 98, 99], \"\", @{\"98\", \"99\", 100@}, [\"num\", \"bers\"])\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
172 @result{} [\"abc \"\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
173 \"98 \"\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
174 \"99 \"\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
175 \"d \"\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
176 \"numbers\"]\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
177 @end group\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
178 @end example\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
179 \n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
180 @seealso{char, strcat, cstrcat}\n\ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
181 @end deftypefn") |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
182 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
183 octave_value retval; |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
184 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
185 int nargin = args.length (); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
186 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
187 if (nargin > 0) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
188 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
189 int n_elts = 0; |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
190 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
191 int max_len = 0; |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
192 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
193 for (int i = 0; i < nargin; i++) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
194 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
195 string_vector s = args(i).all_strings (); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
196 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
197 if (error_state) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
198 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
199 error ("strvcat: unable to convert some args to strings"); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
200 return retval; |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
201 } |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
202 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
203 int n = s.length (); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
204 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
205 // do not count empty strings in calculation of number of elements |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
206 if (n > 0) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
207 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
208 for (int j = 0; j < n; j++) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
209 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
210 if (s[j].length () > 0) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
211 n_elts++; |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
212 } |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
213 } |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
214 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
215 int s_max_len = s.max_length (); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
216 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
217 if (s_max_len > max_len) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
218 max_len = s_max_len; |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
219 } |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
220 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
221 string_vector result (n_elts); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
222 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
223 int k = 0; |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
224 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
225 for (int i = 0; i < nargin; i++) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
226 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
227 string_vector s = args(i).all_strings (); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
228 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
229 int n = s.length (); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
230 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
231 if (n > 0) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
232 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
233 for (int j = 0; j < n; j++) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
234 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
235 std::string t = s[j]; |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
236 if (t.length () > 0) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
237 { |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
238 int t_len = t.length (); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
239 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
240 if (max_len > t_len) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
241 t += std::string (max_len - t_len, ' '); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
242 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
243 result[k++] = t; |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
244 } |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
245 } |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
246 } |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
247 } |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
248 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
249 retval = octave_value (result, '\''); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
250 } |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
251 else |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
252 print_usage (); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
253 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
254 return retval; |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
255 } |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
256 |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
257 /* |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
258 %!error <Invalid call to strvcat> strvcat() |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
259 %!assert (strvcat (""), ""); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
260 %!assert (strvcat (100) == "d"); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
261 %!assert (all(strvcat (100,100) == ["d";"d"])); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
262 %!assert (all(strvcat ({100,100}) == ["d";"d"])); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
263 %!assert (all(strvcat ([100,100]) == ["dd"])); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
264 %!assert (all(strvcat ({100,{100}}) == ["d";"d"])); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
265 %!assert (all(strvcat (100, [], 100) == ["d";"d"])) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
266 %!assert (all(strvcat ({100, [], 100}) == ["d";"d"])) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
267 %!assert (all(strvcat ({100,{100, {""}}}) == ["d";"d"])) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
268 %!assert (all(strvcat (["a";"be"], {"c", 100}) == ["a";"be";"c";"d"])) |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
269 %!assert(strcmp (strvcat ("a", "bb", "ccc"), ["a "; "bb "; "ccc"])); |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
270 */ |
ebdf1e058d85
Make strvcat an internal function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8461
diff
changeset
|
271 |
8353
349a555729a9
keep empty strings in char
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7528
diff
changeset
|
272 |
4535 | 273 DEFUN (ischar, args, , |
3361 | 274 "-*- texinfo -*-\n\ |
4535 | 275 @deftypefn {Built-in Function} {} ischar (@var{a})\n\ |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
276 Return 1 if @var{a} is a character array. Otherwise, return 0.\n\ |
3361 | 277 @end deftypefn") |
807 | 278 { |
4233 | 279 octave_value retval; |
807 | 280 |
281 int nargin = args.length (); | |
282 | |
283 if (nargin == 1 && args(0).is_defined ()) | |
4233 | 284 retval = args(0).is_string (); |
807 | 285 else |
5823 | 286 print_usage (); |
807 | 287 |
288 return retval; | |
289 } | |
290 | |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
291 /* |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
292 |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
293 %!assert (ischar ("a"), logical (1)); |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
294 %!assert (ischar (["ab";"cd"]), logical (1)); |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
295 %!assert (ischar ({"ab"}), logical (0)); |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
296 %!assert (ischar (1), logical (0)); |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
297 %!error <Invalid call to ischar.*> ischar (); |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
298 |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
299 */ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
300 |
5415 | 301 DEFUN (strcmp, args, , |
302 "-*- texinfo -*-\n\ | |
6678 | 303 @deftypefn {Built-in Function} {} strcmp (@var{s1}, @var{s2})\n\ |
5415 | 304 Return 1 if the character strings @var{s1} and @var{s2} are the same,\n\ |
305 and 0 otherwise.\n\ | |
5674 | 306 \n\ |
307 If either @var{s1} or @var{s2} is a cell array of strings, then an array\n\ | |
308 of the same size is returned, containing the values described above for\n\ | |
309 every member of the cell array. The other argument may also be a cell\n\ | |
310 array of strings (of the same size or with only one element), char matrix\n\ | |
311 or character string.\n\ | |
312 \n\ | |
313 @strong{Caution:} For compatibility with @sc{Matlab}, Octave's strcmp\n\ | |
314 function returns 1 if the character strings are equal, and 0 otherwise.\n\ | |
315 This is just the opposite of the corresponding C library function.\n\ | |
316 @seealso{strcmpi, strncmp, strncmpi}\n\ | |
5415 | 317 @end deftypefn") |
318 { | |
5531 | 319 octave_value retval; |
5415 | 320 |
5416 | 321 if (args.length () == 2) |
322 { | |
323 bool s1_string = args(0).is_string (); | |
324 bool s1_cell = args(0).is_cell (); | |
325 bool s2_string = args(1).is_string (); | |
326 bool s2_cell = args(1).is_cell (); | |
5415 | 327 |
5416 | 328 if (s1_string && s2_string) |
329 { | |
330 // Must match exactly in all dimensions. | |
331 | |
332 const dim_vector dv1 = args(0).dims (); | |
333 const dim_vector dv2 = args(1).dims (); | |
5415 | 334 |
5416 | 335 if (dv1.length () == dv2.length ()) |
336 { | |
337 for (int i = 0; i < dv1.length (); i++) | |
338 { | |
339 if (dv1(i) != dv2(i)) | |
340 { | |
341 retval = false; | |
342 return retval; | |
343 } | |
344 } | |
5415 | 345 |
5416 | 346 if (dv1(0) == 0) |
347 retval = true; | |
348 else | |
349 { | |
350 charNDArray s1 = args(0).char_array_value (); | |
351 charNDArray s2 = args(1).char_array_value (); | |
5415 | 352 |
5416 | 353 for (int i = 0; i < dv1.numel (); i++) |
354 { | |
355 if (s1(i) != s2(i)) | |
356 { | |
357 retval = false; | |
358 return retval; | |
359 } | |
360 } | |
5415 | 361 |
5416 | 362 retval = true; |
363 } | |
5415 | 364 } |
365 } | |
5416 | 366 else if ((s1_string && s2_cell) || (s1_cell && s2_string)) |
5415 | 367 { |
5416 | 368 string_vector str; |
369 Cell cell; | |
370 int r; | |
5415 | 371 |
5416 | 372 if (s1_string) |
373 { | |
374 str = args(0).all_strings (); | |
375 r = args(0).rows (); | |
376 cell = args(1).cell_value (); | |
377 } | |
378 else | |
379 { | |
380 str = args(1).all_strings (); | |
381 r = args(1).rows (); | |
382 cell = args(0).cell_value (); | |
383 } | |
384 | |
5862 | 385 if (r == 0 || r == 1) |
5416 | 386 { |
387 // Broadcast the string. | |
388 | |
389 boolNDArray output (cell.dimensions); | |
5415 | 390 |
5862 | 391 std::string s = r == 0 ? std::string () : str[0]; |
392 | |
5416 | 393 for (int i = 0; i < cell.length (); i++) |
6250 | 394 { |
395 if (cell(i).is_string ()) | |
396 output(i) = (cell(i).string_value () == s); | |
397 else | |
398 output(i) = false; | |
399 } | |
5416 | 400 |
401 retval = output; | |
402 } | |
403 else if (r > 1) | |
5415 | 404 { |
5416 | 405 if (cell.length () == 1) |
5415 | 406 { |
5416 | 407 // Broadcast the cell. |
408 | |
409 const dim_vector dv (r, 1); | |
410 boolNDArray output (dv); | |
411 | |
412 if (cell(0).is_string ()) | |
413 { | |
414 const std::string str2 = cell(0).string_value (); | |
5415 | 415 |
5416 | 416 for (int i = 0; i < r; i++) |
417 output(i) = (str[i] == str2); | |
418 } | |
419 else | |
420 { | |
421 for (int i = 0; i < r; i++) | |
422 output(i) = false; | |
423 } | |
424 | |
425 retval = output; | |
5415 | 426 } |
427 else | |
428 { | |
5416 | 429 // Must match in all dimensions. |
430 | |
431 boolNDArray output (cell.dimensions); | |
5415 | 432 |
5416 | 433 if (cell.length () == r) |
434 { | |
435 for (int i = 0; i < r; i++) | |
6250 | 436 { |
437 if (cell(i).is_string ()) | |
438 output(i) = (str[i] == cell(i).string_value ()); | |
439 else | |
440 output(i) = false; | |
441 } | |
5415 | 442 |
5416 | 443 retval = output; |
444 } | |
445 else | |
446 retval = false; | |
5415 | 447 } |
448 } | |
449 } | |
5416 | 450 else if (s1_cell && s2_cell) |
5415 | 451 { |
5416 | 452 Cell cell1; |
453 Cell cell2; | |
5415 | 454 |
5416 | 455 int r1 = args(0).numel (); |
456 int r2; | |
457 | |
458 if (r1 == 1) | |
459 { | |
460 // Make the singleton cell2. | |
5415 | 461 |
5416 | 462 cell1 = args(1).cell_value (); |
463 cell2 = args(0).cell_value (); | |
464 r1 = cell1.length (); | |
465 r2 = 1; | |
466 } | |
467 else | |
468 { | |
469 cell1 = args(0).cell_value (); | |
470 cell2 = args(1).cell_value (); | |
471 r2 = cell2.length (); | |
472 } | |
5415 | 473 |
5416 | 474 const dim_vector size1 = cell1.dimensions; |
475 const dim_vector size2 = cell2.dimensions; | |
476 | |
477 boolNDArray output (size1); | |
478 | |
479 if (r2 == 1) | |
480 { | |
481 // Broadcast cell2. | |
482 | |
483 if (! cell2(0).is_string ()) | |
5415 | 484 { |
5416 | 485 for (int i = 0; i < r1; i++) |
486 output(i) = false; | |
5415 | 487 } |
488 else | |
5416 | 489 { |
490 const std::string str2 = cell2(0).string_value (); | |
491 | |
492 for (int i = 0; i < r1; i++) | |
493 { | |
494 if (cell1(i).is_string ()) | |
495 { | |
496 const std::string str1 = cell1(i).string_value (); | |
497 output(i) = (str1 == str2); | |
498 } | |
499 else | |
500 output(i) = false; | |
501 } | |
502 } | |
5415 | 503 } |
5416 | 504 else |
505 { | |
506 if (size1 != size2) | |
507 { | |
508 error ("strcmp: nonconformant cell arrays"); | |
509 return retval; | |
510 } | |
511 | |
512 for (int i = 0; i < r1; i++) | |
513 { | |
514 if (cell1(i).is_string () && cell2(i).is_string ()) | |
515 { | |
516 const std::string str1 = cell1(i).string_value (); | |
517 const std::string str2 = cell2(i).string_value (); | |
518 output(i) = (str1 == str2); | |
519 } | |
520 else | |
521 output(i) = false; | |
522 } | |
523 } | |
524 | |
525 retval = output; | |
5415 | 526 } |
5531 | 527 else |
528 retval = false; | |
5415 | 529 } |
5416 | 530 else |
5823 | 531 print_usage (); |
5415 | 532 |
533 return retval; | |
534 } | |
535 | |
5862 | 536 /* |
537 %!shared x | |
538 %! x = char (zeros (0, 2)); | |
539 %!assert (strcmp ('', x) == false); | |
540 %!assert (strcmp (x, '') == false); | |
541 %!assert (strcmp (x, x) == true); | |
5911 | 542 ## %!assert (strcmp ({''}, x) == false); |
543 ## %!assert (strcmp ({x}, '') == false); | |
544 ## %!assert (strcmp ({x}, x) == true); | |
545 ## %!assert (strcmp ('', {x}) == false); | |
546 ## %!assert (strcmp (x, {''}) == false); | |
547 ## %!assert (strcmp (x, {x}) == true); | |
548 ## %!assert (all (strcmp ({x; x}, '') == [false; false])); | |
549 ## %!assert (all (strcmp ({x; x}, {''}) == [false; false])); | |
550 ## %!assert (all (strcmp ('', {x; x}) == [false; false])); | |
551 ## %!assert (all (strcmp ({''}, {x; x}) == [false; false])); | |
5862 | 552 %!assert (strcmp ({'foo'}, x) == false); |
553 %!assert (strcmp ({'foo'}, 'foo') == true); | |
554 %!assert (strcmp ({'foo'}, x) == false); | |
555 %!assert (strcmp (x, {'foo'}) == false); | |
556 %!assert (strcmp ('foo', {'foo'}) == true); | |
557 %!assert (strcmp (x, {'foo'}) == false); | |
558 %!shared y | |
559 %! y = char (zeros (2, 0)); | |
560 %!assert (strcmp ('', y) == false); | |
561 %!assert (strcmp (y, '') == false); | |
562 %!assert (strcmp (y, y) == true); | |
563 %!assert (all (strcmp ({''}, y) == [true; true])); | |
564 %!assert (strcmp ({y}, '') == true); | |
565 %!assert (all (strcmp ({y}, y) == [true; true])); | |
566 %!assert (all (strcmp ('', {y}) == [true; true])); | |
567 %!assert (all (strcmp (y, {''}) == [true; true])); | |
568 %!assert (all (strcmp (y, {y}) == [true; true])); | |
5911 | 569 ## %!assert (all (strcmp ({y; y}, '') == [false; false])); |
570 ## %!assert (all (strcmp ({y; y}, {''}) == [false; false])); | |
571 ## %!assert (all (strcmp ('', {y; y}) == [false; false])); | |
572 ## %!assert (all (strcmp ({''}, {y; y}) == [false; false])); | |
5862 | 573 %!assert (all (strcmp ({'foo'}, y) == [false; false])); |
574 %!assert (all (strcmp ({'foo'}, y) == [false; false])); | |
575 %!assert (all (strcmp (y, {'foo'}) == [false; false])); | |
576 %!assert (all (strcmp (y, {'foo'}) == [false; false])); | |
577 */ | |
578 | |
6250 | 579 DEFUN (strncmp, args, , |
580 "-*- texinfo -*-\n\ | |
6678 | 581 @deftypefn {Built-in Function} {} strncmp (@var{s1}, @var{s2}, @var{n})\n\ |
6250 | 582 Return 1 if the first @var{n} characters of strings @var{s1} and @var{s2} are the same,\n\ |
583 and 0 otherwise.\n\ | |
584 \n\ | |
585 @example\n\ | |
586 @group\n\ | |
587 strncmp (\"abce\", \"abcd\", 3)\n\ | |
588 @result{} 1\n\ | |
589 @end group\n\ | |
590 @end example\n\ | |
591 \n\ | |
592 If either @var{s1} or @var{s2} is a cell array of strings, then an array\n\ | |
593 of the same size is returned, containing the values described above for\n\ | |
594 every member of the cell array. The other argument may also be a cell\n\ | |
595 array of strings (of the same size or with only one element), char matrix\n\ | |
596 or character string.\n\ | |
597 \n\ | |
598 @example\n\ | |
599 @group\n\ | |
6256 | 600 strncmp (\"abce\", @{\"abcd\", \"bca\", \"abc\"@}, 3)\n\ |
6250 | 601 @result{} [1, 0, 1]\n\ |
602 @end group\n\ | |
603 @end example\n\ | |
604 \n\ | |
605 @strong{Caution:} For compatibility with @sc{Matlab}, Octave's strncmp\n\ | |
606 function returns 1 if the character strings are equal, and 0 otherwise.\n\ | |
607 This is just the opposite of the corresponding C library function.\n\ | |
608 @seealso{strncmpi, strcmp, strcmpi}\n\ | |
609 @end deftypefn") | |
610 { | |
611 octave_value retval; | |
612 | |
613 if (args.length () == 3) | |
614 { | |
615 bool s1_string = args(0).is_string (); | |
616 bool s1_cell = args(0).is_cell (); | |
617 bool s2_string = args(1).is_string (); | |
618 bool s2_cell = args(1).is_cell (); | |
619 | |
620 // Match only first n strings. | |
621 int n = args(2).int_value (); | |
622 | |
623 if (n <= 0) | |
624 { | |
625 error ("strncmp: N must be greater than 0"); | |
626 return retval; | |
627 } | |
628 | |
629 if (s1_string && s2_string) | |
630 { | |
631 // The only restriction here is that each string has equal or | |
632 // greater than n characters | |
633 | |
634 const dim_vector dv1 = args(0).dims (); | |
635 const dim_vector dv2 = args(1).dims (); | |
636 | |
637 if (dv1.numel () >= n && dv2.numel () >= n) | |
638 { | |
639 // Follow Matlab in the sense that the first n characters of | |
640 // the two strings (in column major order) need to be the same. | |
641 charNDArray s1 = args(0).char_array_value (); | |
642 charNDArray s2 = args(1).char_array_value (); | |
643 | |
644 for (int i = 0; i < n; i++) | |
645 { | |
646 if (s1(i) != s2(i)) | |
647 { | |
648 retval = false; | |
649 return retval; | |
650 } | |
651 } | |
652 | |
653 retval = true; | |
654 } | |
655 else | |
656 retval = false; | |
657 } | |
658 else if ((s1_string && s2_cell) || (s1_cell && s2_string)) | |
659 { | |
660 string_vector str; | |
661 Cell cell; | |
662 int r, c; | |
663 | |
664 if (s1_string) | |
665 { | |
666 str = args(0).all_strings (); | |
667 r = args(0).rows (); | |
668 c = args(0).columns (); | |
669 cell = args(1).cell_value (); | |
670 } | |
671 else | |
672 { | |
673 str = args(1).all_strings (); | |
674 r = args(1).rows (); | |
675 c = args(1).columns (); | |
676 cell = args(0).cell_value (); | |
677 } | |
678 | |
679 if (r == 1) | |
680 { | |
681 // Broadcast the string. | |
682 | |
683 boolNDArray output (cell.dimensions); | |
684 | |
685 if (c < n) | |
686 { | |
687 for (int i = 0; i < cell.length (); i++) | |
688 output(i) = false; | |
689 } | |
690 else | |
691 { | |
692 for (int i = 0; i < cell.length (); i++) | |
693 { | |
694 if (cell(i).is_string ()) | |
695 { | |
696 const std::string str2 = cell(i).string_value (); | |
697 | |
698 if (str2.length() >= n) | |
699 { | |
700 if (str2.compare (0, n, str[0], 0, n) == 0) | |
701 output(i) = true; | |
702 else | |
703 output(i) = false; | |
704 } | |
705 else | |
706 output(i) = false; | |
707 } | |
708 } | |
709 } | |
710 | |
711 retval = output; | |
712 } | |
713 else if (r > 1) | |
714 { | |
715 if (cell.length () == 1) | |
716 { | |
717 // Broadcast the cell. | |
718 | |
719 const dim_vector dv (r, 1); | |
720 boolNDArray output (dv); | |
721 | |
722 if (cell(0).is_string () && c >= n) | |
723 { | |
724 const std::string str2 = cell(0).string_value (); | |
725 | |
726 if (str2.length () >= n) | |
727 { | |
728 for (int i = 0; i < r; i++) | |
729 { | |
730 if (str[i].compare (0, n, str2, 0, n) == 0) | |
731 output(i) = true; | |
732 else | |
733 output(i) = false; | |
734 } | |
735 } | |
736 else | |
737 { | |
738 for (int i = 0; i < r; i++) | |
739 output(i) = false; | |
740 } | |
741 } | |
742 else | |
743 { | |
744 for (int i = 0; i < r; i++) | |
745 output(i) = false; | |
746 } | |
747 | |
748 retval = output; | |
749 } | |
750 else | |
751 { | |
752 // Must match in all dimensions. | |
753 | |
754 boolNDArray output (cell.dimensions); | |
755 | |
756 if (cell.numel () == r) | |
757 { | |
758 for (int i = 0; i < r; i++) | |
759 { | |
760 output(i) = false; | |
761 | |
762 if (cell(i).is_string () && c >= n) | |
763 { | |
764 std::string str2 = cell(i).string_value (); | |
765 | |
766 if (str2.length () >= n | |
767 && str2.compare (0, n, str[i], 0, n) == 0) | |
768 output(i) = true; | |
769 } | |
770 } | |
771 | |
772 retval = output; | |
773 } | |
774 else | |
775 { | |
776 error ("strncmp: the number of rows of the string matrix must match the number of elements in the cell"); | |
777 return retval; | |
778 } | |
779 } | |
780 } | |
781 } | |
782 else if (s1_cell && s2_cell) | |
783 { | |
784 Cell cell1; | |
785 Cell cell2; | |
786 | |
787 int r1 = args(0).numel (); | |
788 int r2; | |
789 | |
790 if (r1 == 1) | |
791 { | |
792 // Make the singleton cell2. | |
793 | |
794 cell1 = args(1).cell_value (); | |
795 cell2 = args(0).cell_value (); | |
796 r1 = cell1.length (); | |
797 r2 = 1; | |
798 } | |
799 else | |
800 { | |
801 cell1 = args(0).cell_value (); | |
802 cell2 = args(1).cell_value (); | |
803 r2 = cell2.length (); | |
804 } | |
805 | |
806 const dim_vector size1 = cell1.dimensions; | |
807 const dim_vector size2 = cell2.dimensions; | |
808 | |
809 boolNDArray output (size1); | |
810 | |
811 if (r2 == 1) | |
812 { | |
813 // Broadcast cell2. | |
814 | |
815 if (! cell2(0).is_string ()) | |
816 { | |
817 for (int i = 0; i < r1; i++) | |
818 output(i) = false; | |
819 } | |
820 else | |
821 { | |
822 const std::string str2 = cell2(0).string_value (); | |
823 | |
824 for (int i = 0; i < r1; i++) | |
825 { | |
826 if (cell1(i).is_string ()) | |
827 { | |
828 const std::string str1 = cell1(i).string_value (); | |
829 | |
830 if (str1.length () >= n && str2.length () >= n | |
831 && str1.compare (0, n, str2, 0, n) == 0) | |
832 output(i) = true; | |
833 else | |
834 output(i) = false; | |
835 } | |
836 else | |
837 output(i) = false; | |
838 } | |
839 } | |
840 } | |
841 else | |
842 { | |
843 if (size1 != size2) | |
844 { | |
845 error ("strncmp: nonconformant cell arrays"); | |
846 return retval; | |
847 } | |
848 | |
849 for (int i = 0; i < r1; i++) | |
850 { | |
851 if (cell1(i).is_string () && cell2(i).is_string ()) | |
852 { | |
853 const std::string str1 = cell1(i).string_value (); | |
854 const std::string str2 = cell2(i).string_value (); | |
855 | |
856 if (str1.length () >= n && str2.length () >= n | |
857 && str1.compare (0, n, str2, 0, n) == 0) | |
858 output(i) = true; | |
859 else | |
860 output(i) = false; | |
861 } | |
862 else | |
863 output(i) = false; | |
864 } | |
865 } | |
866 | |
867 retval = output; | |
868 } | |
869 else | |
870 retval = false; | |
871 } | |
872 else | |
873 print_usage (); | |
874 | |
875 return retval; | |
876 } | |
877 | |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
878 /* |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
879 %!error <Invalid call to strncmp.*> strncmp (); |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
880 %!error <Invalid call to strncmp.*> strncmp ("abc", "def"); |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
881 %!assert (strncmp ("abce", "abc", 3) == 1) |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
882 %!assert (strncmp (100, 100, 1) == 0) |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
883 %!assert (all (strncmp ("abce", {"abcd", "bca", "abc"}, 3) == [1, 0, 1])) |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
884 %!assert (all (strncmp ("abc", {"abcd", "bca", "abc"}, 4) == [0, 0, 0])) |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
885 %!assert (all (strncmp ({"abcd", "bca", "abc"},"abce", 3) == [1, 0, 1])) |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
886 %!assert (all (strncmp ({"abcd", "bca", "abc"},{"abcd", "bca", "abe"}, 3) == [1, 1, 0])) |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
887 */ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
888 |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
889 |
5690 | 890 DEFUN (list_in_columns, args, , |
891 "-*- texinfo -*-\n\ | |
892 @deftypefn {Built-in Function} {} list_in_columns (@var{arg}, @var{width})\n\ | |
893 Return a string containing the elements of @var{arg} listed in\n\ | |
894 columns with an overall maximum width of @var{width}. The argument\n\ | |
895 @var{arg} must be a cell array of character strings or a character array.\n\ | |
896 If @var{width} is not specified, the width of the terminal screen is used.\n\ | |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
897 Newline characters are used to break the lines in the output string.\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
898 For example:\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
899 \n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
900 @example\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
901 @group\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
902 list_in_columns (@{\"abc\", \"def\", \"ghijkl\", \"mnop\", \"qrs\", \"tuv\"@}, 20)\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
903 @result{} ans = abc mnop\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
904 def qrs\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
905 ghijkl tuv\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
906 \n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
907 whos ans\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
908 @result{}\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
909 Variables in the current scope:\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
910 \n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
911 Attr Name Size Bytes Class\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
912 ==== ==== ==== ===== =====\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
913 ans 1x37 37 char\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
914 \n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
915 Total is 37 elements using 37 bytes\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
916 @end group\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
917 @end example\n\ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
918 \n\ |
5690 | 919 @seealso{terminal_size}\n\ |
920 @end deftypefn") | |
921 { | |
922 octave_value retval; | |
923 | |
924 int nargin = args.length (); | |
925 | |
926 if (nargin == 1 || nargin == 2) | |
927 { | |
928 string_vector s = args(0).all_strings (); | |
929 | |
930 if (! error_state) | |
931 { | |
5765 | 932 std::ostringstream buf; |
5690 | 933 |
934 if (nargin == 1) | |
935 // Let list_in_columns query terminal width. | |
936 s.list_in_columns (buf); | |
937 else | |
938 { | |
939 int width = args(1).int_value (); | |
940 | |
941 if (! error_state) | |
942 s.list_in_columns (buf, width); | |
943 else | |
944 error ("list_in_columns: expecting width to be an integer"); | |
945 } | |
946 | |
5765 | 947 retval = buf.str (); |
5690 | 948 } |
949 else | |
950 error ("list_in_columns: expecting cellstr or char array"); | |
951 } | |
952 else | |
5823 | 953 print_usage (); |
5690 | 954 |
955 return retval; | |
956 } | |
957 | |
807 | 958 /* |
8461
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
959 %!error <Invalid call to list_in_columns.*> list_in_columns (); |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
960 %!error <Invalid call to list_in_columns.*> list_in_columns (["abc", "def"], 20, 2); |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
961 %!error <invalid conversion from string to real scalar.*> list_in_columns (["abc", "def"], "a"); |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
962 %!test |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
963 %! input = {"abc", "def", "ghijkl", "mnop", "qrs", "tuv"}; |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
964 %! result = "abc mnop\ndef qrs\nghijkl tuv\n"; |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
965 %! assert (list_in_columns (input, 20) == result); |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
966 %!test |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
967 %! input = ["abc"; "def"; "ghijkl"; "mnop"; "qrs"; "tuv"]; |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
968 %! result = "abc mnop \ndef qrs \nghijkl tuv \n"; |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
969 %! assert (list_in_columns (input, 20) == result); |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
970 */ |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
971 |
9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
972 /* |
807 | 973 ;;; Local Variables: *** |
974 ;;; mode: C++ *** | |
975 ;;; End: *** | |
976 */ |