44 #define ORD(ch) static_cast<unsigned char>(ch)
45 #define TABSIZE (std::numeric_limits<unsigned char>::max () + 1)
53 const char *
x = needle.
data ();
59 table[
ORD(x[i])] = m - i;
69 const char *
x = needle.
data ();
71 const char *y = haystack.
data ();
76 std::deque<octave_idx_type> accum;
93 if (y[i] == x[0] && y[i+1] == x[1])
101 if (y[i] == x[0] && y[i+1] == x[1])
102 accum.push_back (i++);
115 if (std::equal (x, x + m, y + j))
117 j += table[
ORD(y[j + m])];
124 if (std::equal (x, x + m, y + j))
130 j += table[
ORD(y[j + m])];
134 if (j == n - m && std::equal (x, x + m, y + j))
142 for (std::deque<octave_idx_type>::const_iterator iter = accum.begin ();
143 iter != accum.end (); iter++)
145 result.
xelem (k++) = *iter;
151 DEFUN (strfind, args, ,
153 @deftypefn {Built-in Function} {@var{idx} =} strfind (@var{str}, @var{pattern})\n\
154 @deftypefnx {Built-in Function} {@var{idx} =} strfind (@var{cellstr}, @var{pattern})\n\
155 @deftypefnx {Built-in Function} {@var{idx} =} strfind (@dots{}, \"overlaps\", @var{val})\n\
156 Search for @var{pattern} in the string @var{str} and return the starting\n\
157 index of every such occurrence in the vector @var{idx}.\n\
159 If there is no such occurrence, or if @var{pattern} is longer than\n\
160 @var{str}, or if @var{pattern} itself is empty, then @var{idx} is the empty\n\
163 The optional argument @qcode{\"overlaps\"} determines whether the pattern\n\
164 can match at every position in @var{str} (true), or only for unique\n\
165 occurrences of the complete pattern (false). The default is true.\n\
167 If a cell array of strings @var{cellstr} is specified then @var{idx} is a\n\
168 cell array of vectors, as specified above.\n\
174 strfind (\"abababa\", \"aba\")\n\
175 @result{} [1, 3, 5]\n\
177 strfind (\"abababa\", \"aba\", \"overlaps\", false)\n\
180 strfind (@{\"abababa\", \"bebebe\", \"ab\"@}, \"aba\")\n\
192 @seealso{findstr, strmatch, regexp, regexpi, find}\n\
196 int nargin = args.
length ();
197 bool overlaps =
true;
201 std::string opt = args(2).string_value ();
202 if (opt ==
"overlaps")
204 overlaps = args(3).bool_value ();
209 error (
"strfind: unknown option: %s", opt.c_str ());
254 error (
"strfind: each element of CELLSTR must be a string");
262 error (
"strfind: first argument must be a string or cell array of strings");
267 error (
"strfind: PATTERN must be a string or cell array of strings");
296 bool overlaps =
true)
332 retsiz = siz + nidx * (rsiz - psiz);
339 const char *src = str.
data ();
340 const char *reps = rep.
data ();
348 dest = std::copy (src + k, src + j, dest);
349 dest = std::copy (reps, reps + rsiz, dest);
353 std::copy (src + k, src + siz, dest);
361 DEFUN (strrep, args, ,
363 @deftypefn {Built-in Function} {@var{newstr} =} strrep (@var{str}, @var{ptn}, @var{rep})\n\
364 @deftypefnx {Built-in Function} {@var{newstr} =} strrep (@var{cellstr}, @var{ptn}, @var{rep})\n\
365 @deftypefnx {Built-in Function} {@var{newstr} =} strrep (@dots{}, \"overlaps\", @var{val})\n\
366 Replace all occurrences of the pattern @var{ptn} in the string @var{str}\n\
367 with the string @var{rep} and return the result.\n\
369 The optional argument @qcode{\"overlaps\"} determines whether the pattern\n\
370 can match at every position in @var{str} (true), or only for unique\n\
371 occurrences of the complete pattern (false). The default is true.\n\
373 @var{s} may also be a cell array of strings, in which case the replacement is\n\
374 done for each element and a cell array is returned.\n\
380 strrep (\"This is a test string\", \"is\", \"&%$\")\n\
381 @result{} \"Th&%$ &%$ a test string\"\n\
385 @seealso{regexprep, strfind, findstr}\n\
389 int nargin = args.
length ();
390 bool overlaps =
true;
394 std::string opt = args(3).string_value ();
395 if (opt ==
"overlaps")
397 overlaps = args(4).bool_value ();
402 error (
"strrep: unknown option: %s", opt.c_str ());
437 error (
"strrep: each element of S must be a string");
445 error (
"strrep: S must be a string or cell array of strings");
450 error (
"strrep: PTN and REP arguments must be strings or cell arrays of strings");
charNDArray char_array_value(bool frc_str_conv=false) const
OCTINTERP_API void print_usage(void)
octave_idx_type numel(void) const
Number of elements in the array.
bool is_scalar_type(void) const
static Array< octave_idx_type > qs_search(const Array< char > &needle, const Array< char > &haystack, const octave_idx_type *table, bool overlaps=true)
#define DEFUN(name, args_name, nargout_name, doc)
void error(const char *fmt,...)
Cell cell_value(void) const
OCTAVE_EXPORT octave_value_list Fstrfind(const octave_value_list &args, int)
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
bool is_string(void) const
const T * data(void) const
octave_idx_type length(void) const
T & xelem(octave_idx_type n)
static Array< char > qs_replace(const Array< char > &str, const Array< char > &pat, const Array< char > &rep, const octave_idx_type *table, bool overlaps=true)
bool is_empty(void) const
OCTAVE_EXPORT octave_value_list Fstrrep(const octave_value_list &args, int)
octave_value_list do_simple_cellfun(octave_value_list(*fun)(const octave_value_list &, int), const char *fun_name, const octave_value_list &args, int nargout)
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
static void qs_preprocess(const Array< char > &needle, octave_idx_type *table)
const T * fortran_vec(void) const
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))
F77_RET_T const double * x
charNDArray min(char d, const charNDArray &m)