Mercurial > hg > octave-nkf
annotate src/DLD-FUNCTIONS/strfind.cc @ 10358:72fab01e5d68
improve some size_t queries
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Thu, 25 Feb 2010 12:55:13 +0100 |
parents | e317791645c4 |
children | 4d1fc073fbb7 |
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; |
10133
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
73 if (m == 1) |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
74 { |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
75 // Looking for a single character. |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
76 for (octave_idx_type i = 0; i < n; i++) |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
77 { |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
78 if (y[i] == x[0]) |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
79 accum.push_back (i); |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
80 } |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
81 } |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
82 else if (m == 2) |
10022 | 83 { |
10133
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
84 // Two characters. |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
85 if (overlaps) |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
86 { |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
87 for (octave_idx_type i = 0; i < n-1; i++) |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
88 { |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
89 if (y[i] == x[0] && y[i+1] == x[1]) |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
90 accum.push_back (i); |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
91 } |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
92 } |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
93 else |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
94 { |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
95 for (octave_idx_type i = 0; i < n-1; i++) |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
96 { |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
97 if (y[i] == x[0] && y[i+1] == x[1]) |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
98 accum.push_back (i++); |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
99 } |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
100 } |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
101 } |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
102 else if (n >= m) |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
103 { |
2e4fc7fdba15
optimize strfind with 1 or 2 characters
Jaroslav Hajek <highegg@gmail.com>
parents:
10086
diff
changeset
|
104 // General case. |
10022 | 105 octave_idx_type j = 0; |
106 | |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
107 if (overlaps) |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
108 { |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
109 while (j < n - m) |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
110 { |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
111 if (std::equal (x, x + m, y + j)) |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
112 accum.push_back (j); |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
113 j += table[ORD(y[j + m])]; |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
114 } |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
115 } |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
116 else |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
117 { |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
118 while (j < n - m) |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
119 { |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
120 if (std::equal (x, x + m, y + j)) |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
121 { |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
122 accum.push_back (j); |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
123 j += m; |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
124 } |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
125 else |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
126 j += table[ORD(y[j + m])]; |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
127 } |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
128 } |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
129 |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
130 if (j == n - m && std::equal (x, x + m, y + j)) |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
131 accum.push_back (j); |
10022 | 132 } |
133 | |
134 octave_idx_type nmatch = accum.size (); | |
10258 | 135 octave_idx_type one = 1; |
136 Array<octave_idx_type> result (dim_vector (std::min (one, nmatch), nmatch)); | |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
137 octave_idx_type k = 0; |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
138 for (std::deque<octave_idx_type>::const_iterator iter = accum.begin (); |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
139 iter != accum.end (); iter++) |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
140 { |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
141 result.xelem (k++) = *iter; |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
142 } |
10022 | 143 |
144 return result; | |
145 } | |
146 | |
147 DEFUN_DLD (strfind, args, , | |
148 "-*- texinfo -*-\n\ | |
149 @deftypefn {Loadable Function} {@var{idx} =} strfind (@var{str}, @var{pattern})\n\ | |
150 @deftypefnx {Loadable Function} {@var{idx} =} strfind (@var{cellstr}, @var{pattern})\n\ | |
151 Search for @var{pattern} in the string @var{str} and return the\n\ | |
152 starting index of every such occurrence in the vector @var{idx}.\n\ | |
153 If there is no such occurrence, or if @var{pattern} is longer\n\ | |
154 than @var{str}, then @var{idx} is the empty array @code{[]}.\n\ | |
155 \n\ | |
156 If the cell array of strings @var{cellstr} is specified instead of the\n\ | |
157 string @var{str}, then @var{idx} is a cell array of vectors, as specified\n\ | |
158 above. Examples:\n\ | |
159 \n\ | |
160 @example\n\ | |
161 @group\n\ | |
162 strfind (\"abababa\", \"aba\")\n\ | |
163 @result{} [1, 3, 5]\n\ | |
164 \n\ | |
165 strfind (@{\"abababa\", \"bebebe\", \"ab\"@}, \"aba\")\n\ | |
166 @result{} ans =\n\ | |
167 @{\n\ | |
168 [1,1] =\n\ | |
169 \n\ | |
170 1 3 5\n\ | |
171 \n\ | |
172 [1,2] = [](1x0)\n\ | |
173 [1,3] = [](1x0)\n\ | |
174 @}\n\ | |
175 @end group\n\ | |
176 @end example\n\ | |
177 @seealso{findstr, strmatch, strcmp, strncmp, strcmpi, strncmpi, find}\n\ | |
178 @end deftypefn") | |
179 { | |
180 octave_value retval; | |
181 int nargin = args.length (); | |
182 | |
183 if (nargin == 2) | |
184 { | |
10086
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10074
diff
changeset
|
185 octave_value argstr = args(0), argpat = args(1); |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10074
diff
changeset
|
186 if (argpat.is_string ()) |
10022 | 187 { |
10086
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10074
diff
changeset
|
188 Array<char> needle = argpat.char_array_value (); |
10022 | 189 OCTAVE_LOCAL_BUFFER (octave_idx_type, table, UCHAR_MAX); |
190 qs_preprocess (needle, table); | |
191 | |
192 if (argstr.is_string ()) | |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
193 retval = octave_value (qs_search (needle, argstr.char_array_value (), table), |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
194 true, true); |
10022 | 195 else if (argstr.is_cell ()) |
196 { | |
197 const Cell argsc = argstr.cell_value (); | |
198 Cell retc (argsc.dims ()); | |
199 octave_idx_type ns = argsc.numel (); | |
200 | |
201 for (octave_idx_type i = 0; i < ns; i++) | |
202 { | |
203 octave_value argse = argsc(i); | |
204 if (argse.is_string ()) | |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
205 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
|
206 true, true); |
10022 | 207 else |
208 { | |
209 error ("strfind: each cell element must be a string"); | |
210 break; | |
211 } | |
212 } | |
213 | |
214 retval = retc; | |
215 } | |
216 else | |
217 error ("strfind: first argument must be a string or cell array of strings"); | |
218 } | |
10086
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10074
diff
changeset
|
219 else if (argpat.is_cell ()) |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10074
diff
changeset
|
220 retval = do_simple_cellfun (Fstrfind, "strfind", args); |
10022 | 221 else |
10086
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10074
diff
changeset
|
222 error ("strfind: pattern must be a string or cell array of strings"); |
10022 | 223 } |
224 else | |
225 print_usage (); | |
226 | |
227 return retval; | |
228 } | |
229 | |
230 /* | |
231 | |
232 %!error strfind (); | |
233 %!error strfind ("foo", "bar", 1); | |
234 %!error strfind ("foo", 100); | |
235 %!error strfind (100, "foo"); | |
236 | |
237 %!assert (strfind ("abababa", "aba"), [1, 3, 5]); | |
238 %!assert (strfind ({"abababa", "bla", "bla"}, "a"), {[1, 3, 5, 7], 3, 3}); | |
239 %!assert (strfind ("Linux _is_ user-friendly. It just isn't ignorant-friendly or idiot-friendly.", "friendly"), [17, 50, 68]); | |
240 | |
241 */ | |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
242 |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
243 static Array<char> |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
244 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
|
245 const Array<char>& rep, |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
246 const octave_idx_type table[TABSIZE]) |
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 Array<char> ret = str; |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
249 |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
250 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
|
251 |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
252 if (psiz != 0) |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
253 { |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
254 // Look up matches, without overlaps. |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
255 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
|
256 octave_idx_type nidx = idx.numel (); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
257 |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
258 if (nidx) |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
259 { |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
260 // Compute result size. |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
261 octave_idx_type retsiz = siz + nidx * (rsiz - psiz); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
262 ret.clear (dim_vector (1, retsiz)); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
263 const char *src = str.data (), *reps = rep.data (); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
264 char *dest = ret.fortran_vec (); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
265 |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
266 octave_idx_type k = 0; |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
267 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
|
268 { |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
269 octave_idx_type j = idx(i); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
270 dest = std::copy (src + k, src + j, dest); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
271 dest = std::copy (reps, reps + rsiz, dest); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
272 k = j + psiz; |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
273 } |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
274 |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
275 std::copy (src + k, src + siz, dest); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
276 } |
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 |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
279 return ret; |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
280 } |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
281 |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
282 DEFUN_DLD (strrep, args, , |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
283 "-*- texinfo -*-\n\ |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
284 @deftypefn {Loadable Function} {} strrep (@var{s}, @var{x}, @var{y})\n\ |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
285 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
|
286 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
|
287 \n\ |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
288 @example\n\ |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
289 @group\n\ |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
290 strrep (\"This is a test string\", \"is\", \"&%$\")\n\ |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
291 @result{} \"Th&%$ &%$ a test string\"\n\ |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
292 @end group\n\ |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
293 @end example\n\ |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
294 \n\ |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
295 @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
|
296 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
|
297 @seealso{regexprep, strfind, findstr}\n\ |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
298 @end deftypefn") |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
299 { |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
300 octave_value retval; |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
301 int nargin = args.length (); |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
302 |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
303 if (nargin == 3) |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
304 { |
10086
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10074
diff
changeset
|
305 octave_value argstr = args(0), argpat = args(1), argrep = args(2); |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10074
diff
changeset
|
306 if (argpat.is_string () && argrep.is_string ()) |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
307 { |
10086
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10074
diff
changeset
|
308 const Array<char> pat = argpat.char_array_value (); |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10074
diff
changeset
|
309 const Array<char> rep = argrep.char_array_value (); |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
310 |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
311 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
|
312 qs_preprocess (pat, table); |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
313 |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
314 if (argstr.is_string ()) |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
315 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
|
316 else if (argstr.is_cell ()) |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
317 { |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
318 const Cell argsc = argstr.cell_value (); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
319 Cell retc (argsc.dims ()); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
320 octave_idx_type ns = argsc.numel (); |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
321 |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
322 for (octave_idx_type i = 0; i < ns; i++) |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
323 { |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
324 octave_value argse = argsc(i); |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
325 if (argse.is_string ()) |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
326 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
|
327 else |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
328 { |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
329 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
|
330 break; |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
331 } |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
332 } |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
333 |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
334 retval = retc; |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
335 } |
10074
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
336 else |
5e2b4b7967cc
allow array of strings in strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10053
diff
changeset
|
337 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
|
338 } |
10086
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10074
diff
changeset
|
339 else if (argpat.is_cell () || argrep.is_cell ()) |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10074
diff
changeset
|
340 retval = do_simple_cellfun (Fstrrep, "strrep", args); |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
341 else |
10086
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
10074
diff
changeset
|
342 error ("strrep: x and y arguments must be strings or cell arrays of strings"); |
10053
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
343 } |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
344 else |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
345 print_usage (); |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
346 |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
347 return retval; |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
348 } |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
349 |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
350 /* |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
351 |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
352 %!assert(strcmp (strrep ("This is a test string", "is", "&%$"), |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
353 %! "Th&%$ &%$ a test string")); |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
354 |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
355 %!error strrep (); |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
356 |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
357 %!error strrep ("foo", "bar", 3, 4); |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
358 |
830986c43dee
implement compiled strrep
Jaroslav Hajek <highegg@gmail.com>
parents:
10022
diff
changeset
|
359 */ |