Mercurial > hg > octave-jordi
annotate src/DLD-FUNCTIONS/strfind.cc @ 10074:5e2b4b7967cc
allow array of strings in strrep
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 08 Jan 2010 08:27:45 +0100 |
parents | 830986c43dee |
children | 76df75b10c80 |
rev | line source |
---|---|
10022 | 1 /* |
2 | |
3 Copyright (C) 2009 Jaroslav Hajek | |
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 3 of the License, or (at your | |
10 option) any 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, see | |
19 <http://www.gnu.org/licenses/>. | |
20 | |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
24 #include <config.h> | |
25 #endif | |
26 | |
27 #include <string> | |
28 #include <climits> | |
29 #include <algorithm> | |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
30 #include <deque> |
10022 | 31 |
32 #include "Cell.h" | |
33 #include "ov.h" | |
34 #include "defun-dld.h" | |
35 #include "unwind-prot.h" | |
36 #include "gripes.h" | |
37 #include "utils.h" | |
38 | |
39 // This allows safe indexing with char. In C++, char may be (and often is) signed! | |
40 #define ORD(ch) static_cast<unsigned char>(ch) | |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
41 #define TABSIZE (UCHAR_MAX + 1) |
10022 | 42 |
43 // This is the quick search algorithm, as described at | |
44 // http://www-igm.univ-mlv.fr/~lecroq/string/node19.html | |
45 static void | |
46 qs_preprocess (const Array<char>& needle, | |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
47 octave_idx_type table[TABSIZE]) |
10022 | 48 { |
49 const char *x = needle.data (); | |
50 octave_idx_type m = needle.numel (); | |
51 | |
52 for (octave_idx_type i = 0; i < UCHAR_MAX; i++) | |
53 table[i] = m + 1; | |
54 for (octave_idx_type i = 0; i < m; i++) | |
55 table[ORD(x[i])] = m - i; | |
56 } | |
57 | |
58 | |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
59 static Array<octave_idx_type> |
10022 | 60 qs_search (const Array<char>& needle, |
61 const Array<char>& haystack, | |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
62 const octave_idx_type table[TABSIZE], |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
63 bool overlaps = true) |
10022 | 64 { |
65 const char *x = needle.data (); | |
66 octave_idx_type m = needle.numel (); | |
67 const char *y = haystack.data (); | |
68 octave_idx_type n = haystack.numel (); | |
69 | |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
70 // We'll use deque because it typically has the most favorable properties for |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
71 // the operation we need. |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
72 std::deque<octave_idx_type> accum; |
10022 | 73 if (n >= m) |
74 { | |
75 octave_idx_type j = 0; | |
76 | |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
77 if (overlaps) |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
78 { |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
79 while (j < n - m) |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
80 { |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
81 if (std::equal (x, x + m, y + j)) |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
82 accum.push_back (j); |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
83 j += table[ORD(y[j + m])]; |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
84 } |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
85 } |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
86 else |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
87 { |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
88 while (j < n - m) |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
89 { |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
90 if (std::equal (x, x + m, y + j)) |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
91 { |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
92 accum.push_back (j); |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
93 j += m; |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
94 } |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
95 else |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
96 j += table[ORD(y[j + m])]; |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
97 } |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
98 } |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
99 |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
100 if (j == n - m && std::equal (x, x + m, y + j)) |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
101 accum.push_back (j); |
10022 | 102 } |
103 | |
104 octave_idx_type nmatch = accum.size (); | |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
105 Array<octave_idx_type> result (dim_vector (std::min (1, nmatch), nmatch)); |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
106 octave_idx_type k = 0; |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
107 for (std::deque<octave_idx_type>::const_iterator iter = accum.begin (); |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
108 iter != accum.end (); iter++) |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
109 { |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
110 result.xelem (k++) = *iter; |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
111 } |
10022 | 112 |
113 return result; | |
114 } | |
115 | |
116 DEFUN_DLD (strfind, args, , | |
117 "-*- texinfo -*-\n\ | |
118 @deftypefn {Loadable Function} {@var{idx} =} strfind (@var{str}, @var{pattern})\n\ | |
119 @deftypefnx {Loadable Function} {@var{idx} =} strfind (@var{cellstr}, @var{pattern})\n\ | |
120 Search for @var{pattern} in the string @var{str} and return the\n\ | |
121 starting index of every such occurrence in the vector @var{idx}.\n\ | |
122 If there is no such occurrence, or if @var{pattern} is longer\n\ | |
123 than @var{str}, then @var{idx} is the empty array @code{[]}.\n\ | |
124 \n\ | |
125 If the cell array of strings @var{cellstr} is specified instead of the\n\ | |
126 string @var{str}, then @var{idx} is a cell array of vectors, as specified\n\ | |
127 above. Examples:\n\ | |
128 \n\ | |
129 @example\n\ | |
130 @group\n\ | |
131 strfind (\"abababa\", \"aba\")\n\ | |
132 @result{} [1, 3, 5]\n\ | |
133 \n\ | |
134 strfind (@{\"abababa\", \"bebebe\", \"ab\"@}, \"aba\")\n\ | |
135 @result{} ans =\n\ | |
136 @{\n\ | |
137 [1,1] =\n\ | |
138 \n\ | |
139 1 3 5\n\ | |
140 \n\ | |
141 [1,2] = [](1x0)\n\ | |
142 [1,3] = [](1x0)\n\ | |
143 @}\n\ | |
144 @end group\n\ | |
145 @end example\n\ | |
146 @seealso{findstr, strmatch, strcmp, strncmp, strcmpi, strncmpi, find}\n\ | |
147 @end deftypefn") | |
148 { | |
149 octave_value retval; | |
150 int nargin = args.length (); | |
151 | |
152 if (nargin == 2) | |
153 { | |
154 octave_value argstr = args(0), argp = args(1); | |
155 if (argp.is_string ()) | |
156 { | |
157 Array<char> needle = argp.char_array_value (); | |
158 OCTAVE_LOCAL_BUFFER (octave_idx_type, table, UCHAR_MAX); | |
159 qs_preprocess (needle, table); | |
160 | |
161 if (argstr.is_string ()) | |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
162 retval = octave_value (qs_search (needle, argstr.char_array_value (), table), |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
163 true, true); |
10022 | 164 else if (argstr.is_cell ()) |
165 { | |
166 const Cell argsc = argstr.cell_value (); | |
167 Cell retc (argsc.dims ()); | |
168 octave_idx_type ns = argsc.numel (); | |
169 | |
170 for (octave_idx_type i = 0; i < ns; i++) | |
171 { | |
172 octave_value argse = argsc(i); | |
173 if (argse.is_string ()) | |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
174 retc(i) = octave_value (qs_search (needle, argse.char_array_value (), table), |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
175 true, true); |
10022 | 176 else |
177 { | |
178 error ("strfind: each cell element must be a string"); | |
179 break; | |
180 } | |
181 } | |
182 | |
183 retval = retc; | |
184 } | |
185 else | |
186 error ("strfind: first argument must be a string or cell array of strings"); | |
187 } | |
188 else | |
189 error ("strfind: pattern must be a string"); | |
190 } | |
191 else | |
192 print_usage (); | |
193 | |
194 return retval; | |
195 } | |
196 | |
197 /* | |
198 | |
199 %!error strfind (); | |
200 %!error strfind ("foo", "bar", 1); | |
201 %!error strfind ("foo", 100); | |
202 %!error strfind (100, "foo"); | |
203 | |
204 %!assert (strfind ("abababa", "aba"), [1, 3, 5]); | |
205 %!assert (strfind ({"abababa", "bla", "bla"}, "a"), {[1, 3, 5, 7], 3, 3}); | |
206 %!assert (strfind ("Linux _is_ user-friendly. It just isn't ignorant-friendly or idiot-friendly.", "friendly"), [17, 50, 68]); | |
207 | |
208 */ | |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
209 |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
210 static Array<char> |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
211 qs_replace (const Array<char>& str, const Array<char>& pat, |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
212 const Array<char>& rep, |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
213 const octave_idx_type table[TABSIZE]) |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
214 { |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
215 Array<char> ret = str; |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
216 |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
217 octave_idx_type siz = str.numel (), psiz = pat.numel (), rsiz = rep.numel (); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
218 |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
219 if (psiz != 0) |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
220 { |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
221 // Look up matches, without overlaps. |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
222 const Array<octave_idx_type> idx = qs_search (pat, str, table, false); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
223 octave_idx_type nidx = idx.numel (); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
224 |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
225 if (nidx) |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
226 { |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
227 // Compute result size. |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
228 octave_idx_type retsiz = siz + nidx * (rsiz - psiz); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
229 ret.clear (dim_vector (1, retsiz)); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
230 const char *src = str.data (), *reps = rep.data (); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
231 char *dest = ret.fortran_vec (); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
232 |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
233 octave_idx_type k = 0; |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
234 for (octave_idx_type i = 0; i < nidx; i++) |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
235 { |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
236 octave_idx_type j = idx(i); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
237 dest = std::copy (src + k, src + j, dest); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
238 dest = std::copy (reps, reps + rsiz, dest); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
239 k = j + psiz; |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
240 } |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
241 |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
242 std::copy (src + k, src + siz, dest); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
243 } |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
244 } |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
245 |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
246 return ret; |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
247 } |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
248 |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
249 DEFUN_DLD (strrep, args, , |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
250 "-*- texinfo -*-\n\ |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
251 @deftypefn {Loadable Function} {} strrep (@var{s}, @var{x}, @var{y})\n\ |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
252 Replace all occurrences of the substring @var{x} of the string @var{s}\n\ |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
253 with the string @var{y} and return the result. For example,\n\ |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
254 \n\ |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
255 @example\n\ |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
256 @group\n\ |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
257 strrep (\"This is a test string\", \"is\", \"&%$\")\n\ |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
258 @result{} \"Th&%$ &%$ a test string\"\n\ |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
259 @end group\n\ |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
260 @end example\n\ |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
261 \n\ |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
262 @var{s} may also be a cell array of strings, in which case the replacement is\n\ |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
263 done for each element and a cell array is returned.\n\ |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
264 @seealso{regexprep, strfind, findstr}\n\ |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
265 @end deftypefn") |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
266 { |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
267 octave_value retval; |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
268 int nargin = args.length (); |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
269 |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
270 if (nargin == 3) |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
271 { |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
272 octave_value argstr = args(0), argp = args(1), argr = args(2); |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
273 if (argp.is_string () && argr.is_string ()) |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
274 { |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
275 const Array<char> pat = argp.char_array_value (); |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
276 const Array<char> rep = argr.char_array_value (); |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
277 |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
278 OCTAVE_LOCAL_BUFFER (octave_idx_type, table, UCHAR_MAX); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
279 qs_preprocess (pat, table); |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
280 |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
281 if (argstr.is_string ()) |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
282 retval = qs_replace (argstr.char_array_value (), pat, rep, table); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
283 else if (argstr.is_cell ()) |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
284 { |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
285 const Cell argsc = argstr.cell_value (); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
286 Cell retc (argsc.dims ()); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
287 octave_idx_type ns = argsc.numel (); |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
288 |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
289 for (octave_idx_type i = 0; i < ns; i++) |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
290 { |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
291 octave_value argse = argsc(i); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
292 if (argse.is_string ()) |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
293 retc(i) = qs_replace (argse.char_array_value (), pat, rep, table); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
294 else |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
295 { |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
296 error ("strrep: each cell element must be a string"); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
297 break; |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
298 } |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
299 } |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
300 |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
301 retval = retc; |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
302 } |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
303 else |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
304 error ("strrep: first argument must be a string or cell array of strings"); |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
305 } |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
306 else |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
307 error ("strrep: 2rd and 3rd arguments must be strings"); |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
308 } |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
309 else |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
310 print_usage (); |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
311 |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
312 return retval; |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
313 } |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
314 |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
315 /* |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
316 |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
317 %!assert(strcmp (strrep ("This is a test string", "is", "&%$"), |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
318 %! "Th&%$ &%$ a test string")); |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
319 |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
320 %!error strrep (); |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
321 |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
322 %!error strrep ("foo", "bar", 3, 4); |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
323 |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
324 */ |