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