42 #define ORD(ch) static_cast<unsigned char>(ch)
43 #define TABSIZE (std::numeric_limits<unsigned char>::max () + 1)
51 const char *
x = needle.
data ();
57 table[
ORD(x[i])] = m - i;
67 const char *
x = needle.
data ();
69 const char *y = haystack.
data ();
74 std::deque<octave_idx_type> accum;
91 if (y[i] == x[0] && y[i+1] == x[1])
99 if (y[i] == x[0] && y[i+1] == x[1])
100 accum.push_back (i++);
113 if (std::equal (x, x + m, y + j))
115 j += table[
ORD(y[j + m])];
122 if (std::equal (x, x + m, y + j))
128 j += table[
ORD(y[j + m])];
132 if (j == n - m && std::equal (x, x + m, y + j))
140 for (std::deque<octave_idx_type>::const_iterator iter = accum.begin ();
141 iter != accum.end (); iter++)
143 result.
xelem (k++) = *iter;
149 DEFUN (strfind, args, ,
151 @deftypefn {Built-in Function} {@var{idx} =} strfind (@var{str}, @var{pattern})\n\
152 @deftypefnx {Built-in Function} {@var{idx} =} strfind (@var{cellstr}, @var{pattern})\n\
153 @deftypefnx {Built-in Function} {@var{idx} =} strfind (@dots{}, \"overlaps\", @var{val})\n\
154 Search for @var{pattern} in the string @var{str} and return the\n\
155 starting index of every such occurrence in the vector @var{idx}.\n\
157 If there is no such occurrence, or if @var{pattern} is longer\n\
158 than @var{str}, then @var{idx} is the empty array @code{[]}.\n\
159 The optional argument @qcode{\"overlaps\"} determines whether the pattern\n\
160 can match at every position in @var{str} (true), or only for unique\n\
161 occurrences of the complete pattern (false). The default is true.\n\
163 If a cell array of strings @var{cellstr} is specified\n\
164 then @var{idx} is a cell array of vectors, as specified above.\n\
170 strfind (\"abababa\", \"aba\")\n\
171 @result{} [1, 3, 5]\n\
173 strfind (\"abababa\", \"aba\", \"overlaps\", false)\n\
176 strfind (@{\"abababa\", \"bebebe\", \"ab\"@}, \"aba\")\n\
188 @seealso{findstr, strmatch, regexp, regexpi, find}\n\
192 int nargin = args.
length ();
193 bool overlaps =
true;
197 std::string opt = args(2).string_value ();
198 if (opt ==
"overlaps")
200 overlaps = args(3).bool_value ();
205 error (
"strfind: unknown option: %s", opt.c_str ());
213 if (argpat.is_string ())
241 error (
"strfind: each element of CELLSTR must be a string");
249 error (
"strfind: first argument must be a string or cell array of strings");
251 else if (argpat.is_cell ())
254 error (
"strfind: PATTERN must be a string or cell array of strings");
278 bool overlaps =
true)
312 retsiz = siz + nidx * (rsiz - psiz);
315 const char *src = str.
data (), *reps = rep.
data ();
323 dest = std::copy (src + k, src + j, dest);
324 dest = std::copy (reps, reps + rsiz, dest);
328 std::copy (src + k, src + siz, dest);
335 DEFUN (strrep, args, ,
337 @deftypefn {Built-in Function} {@var{newstr} =} strrep (@var{str}, @var{ptn}, @var{rep})\n\
338 @deftypefnx {Built-in Function} {@var{newstr} =} strrep (@var{cellstr}, @var{ptn}, @var{rep})\n\
339 @deftypefnx {Built-in Function} {@var{newstr} =} strrep (@dots{}, \"overlaps\", @var{val})\n\
340 Replace all occurrences of the pattern @var{ptn} in the string @var{str}\n\
341 with the string @var{rep} and return the result.\n\
343 The optional argument @qcode{\"overlaps\"} determines whether the pattern\n\
344 can match at every position in @var{str} (true), or only for unique\n\
345 occurrences of the complete pattern (false). The default is true.\n\
347 @var{s} may also be a cell array of strings, in which case the replacement is\n\
348 done for each element and a cell array is returned.\n\
354 strrep (\"This is a test string\", \"is\", \"&%$\")\n\
355 @result{} \"Th&%$ &%$ a test string\"\n\
359 @seealso{regexprep, strfind, findstr}\n\
363 int nargin = args.
length ();
364 bool overlaps =
true;
368 std::string opt = args(3).string_value ();
369 if (opt ==
"overlaps")
371 overlaps = args(4).bool_value ();
376 error (
"strrep: unknown option: %s", opt.c_str ());
383 octave_value argstr = args(0), argpat = args(1), argrep = args(2);
384 if (argpat.is_string () && argrep.is_string ())
386 const Array<char> pat = argpat.char_array_value ();
409 error (
"strrep: each element of S must be a string");
417 error (
"strrep: S must be a string or cell array of strings");
419 else if (argpat.is_cell () || argrep.is_cell ())
422 error (
"strrep: PTN and REP arguments must be strings or cell arrays of strings");