26 #if defined (HAVE_CONFIG_H)
61 std::size_t
len = s.length ();
67 if (s[j] ==
'\\' && j+1 <
len)
84 retval.insert (i,
"(?<=\\W|^)");
89 retval.insert (i,
"(?=\\W|$)");
95 bool bad_esc_seq = (j+1 >=
len);
98 if (! bad_esc_seq && s[++j] ==
'{')
108 int digit = s[k] -
'0';
109 if (digit < 0 || digit > 7)
114 if (bad_esc_seq || (brace && s[k++] !=
'}'))
117 warning (R
"(malformed octal escape sequence '\o' -- converting to '\0')");
151 std::size_t
len = s.length ();
157 if (s[j] ==
'\\' && j+1 <
len)
199 int tmpi = s[j] -
'0';
202 int digit = s[k] -
'0';
203 if (digit < 0 || digit > 7)
215 bool bad_esc_seq = (j+1 >=
len);
218 if (! bad_esc_seq && s[++j] ==
'{')
228 int digit = s[k] -
'0';
229 if (digit < 0 || digit > 7)
234 if (bad_esc_seq || (brace && s[k++] !=
'}'))
236 warning (R
"(malformed octal escape sequence '\o' -- converting to '\0')");
246 bool bad_esc_seq = (j+1 >=
len);
249 if (! bad_esc_seq && s[++j] ==
'{')
259 if (! isxdigit (s[k]))
265 tmpi += digit -
'a' + 10;
266 else if (digit >=
'A')
267 tmpi += digit -
'A' + 10;
271 if (bad_esc_seq || (brace && s[k++] !=
'}'))
273 warning (R
"(malformed hex escape sequence '\x' -- converting to '\0')");
286 retval[i] =
'\\'; retval[++i] =
'$';
290 retval[i] =
'\\'; retval[++i] =
'\\';
314 const std::string& who,
int skip,
bool& extra_args)
318 for (
int i = skip; i < args.
length (); i++)
322 str = args(i).xstring_value (
"%s: optional arguments must be strings", who.c_str ());
326 if (str.find (
"once", 0) == 0)
328 else if (str.find (
"matchcase", 0) == 0)
330 else if (str.find (
"ignorecase", 0) == 0)
332 else if (str.find (
"dotall", 0) == 0)
334 else if (str.find (
"stringanchors", 0) == 0)
336 else if (str.find (
"literalspacing", 0) == 0)
338 else if (str.find (
"noemptymatch", 0) == 0)
340 else if (str.find (
"dotexceptnewline", 0) == 0)
342 else if (str.find (
"lineanchors", 0) == 0)
344 else if (str.find (
"freespacing", 0) == 0)
346 else if (str.find (
"emptymatch", 0) == 0)
348 else if (str.find (
"start", 0) == 0
349 || str.find (
"end", 0) == 0
350 || str.find (
"tokenextents", 0) == 0
351 || str.find (
"match", 0) == 0
352 || str.find (
"tokens", 0) == 0
353 || str.find (
"names", 0) == 0
354 || str.find (
"split", 0) == 0)
357 error (
"%s: unrecognized option", who.c_str ());
363 const std::string& who,
bool case_insensitive =
false)
367 int nargin = args.
length ();
370 const std::string buffer = args(0).string_value ();
372 std::string pattern = args(1).string_value ();
379 bool extra_options =
false;
387 std::size_t sz = rx_lst.
size ();
397 for (
int j = 0; j < named_pats.
numel (); j++)
402 for (
const auto& match_data : rx_lst)
406 ctmp(i++) = named_tokens(j);
409 nmap.
assign (named_pats(j), ctmp);
416 auto p = rx_lst.
begin ();
418 retval(4) = (sz ? p->tokens () :
Cell ());
419 retval(3) = (sz ? p->match_string () :
"");
420 retval(2) = (sz ? p->token_extents () :
Matrix ());
424 double start = p->start ();
425 double end = p->end ();
428 split(0) = buffer.substr (0, start-1);
429 split(1) = buffer.substr (end);
450 std::size_t sp_start = 0;
453 for (
const auto& match_data : rx_lst)
455 double s = match_data.start ();
456 double e = match_data.end ();
460 match_string(i) = match_data.match_string ();
461 token_extents(i) = match_data.token_extents ();
464 split(i) = buffer.substr (sp_start, s-sp_start-1);
469 split(i) = buffer.substr (sp_start);
473 retval(3) = match_string;
474 retval(2) = token_extents;
485 new_retval.
resize (nargout);
489 for (
int j = 2; j < nargin; j++)
492 std::string str = args(j).string_value ();
495 if (str.find (
"once", 0) == 0
496 || str.find (
"stringanchors", 0) == 0
497 || str.find (
"lineanchors", 0) == 0
498 || str.find (
"matchcase", 0) == 0
499 || str.find (
"ignorecase", 0) == 0
500 || str.find (
"dotall", 0) == 0
501 || str.find (
"dotexceptnewline", 0) == 0
502 || str.find (
"literalspacing", 0) == 0
503 || str.find (
"freespacing", 0) == 0
504 || str.find (
"noemptymatch", 0) == 0
505 || str.find (
"emptymatch", 0) == 0)
507 else if (str.find (
"start", 0) == 0)
509 else if (str.find (
"end", 0) == 0)
511 else if (str.find (
"tokenextents", 0) == 0)
513 else if (str.find (
"match", 0) == 0)
515 else if (str.find (
"tokens", 0) == 0)
517 else if (str.find (
"names", 0) == 0)
519 else if (str.find (
"split", 0) == 0)
522 new_retval(
n++) = retval(k);
532 for (
int j = 0; j < 7; j++)
535 new_retval(
n++) = retval(j);
547 const std::string& who,
bool case_insensitive =
false)
551 if (args(0).iscell ())
556 if (args(1).iscell ())
560 if (cellpat.
numel () == 1)
562 for (
int j = 0; j < nargout; j++)
563 newretval[j].resize (cellstr.
dims ());
565 new_args(1) = cellpat(0);
569 new_args(0) = cellstr(i);
573 for (
int j = 0; j < nargout; j++)
574 newretval[j](i) = tmp(j);
577 else if (cellstr.
numel () == 1)
579 for (
int j = 0; j < nargout; j++)
580 newretval[j].resize (cellpat.
dims ());
582 new_args(0) = cellstr(0);
586 new_args(1) = cellpat(i);
590 for (
int j = 0; j < nargout; j++)
591 newretval[j](i) = tmp(j);
594 else if (cellstr.
numel () == cellpat.
numel ())
596 if (cellstr.
dims () != cellpat.
dims ())
597 error (
"%s: inconsistent cell array dimensions", who.c_str ());
599 for (
int j = 0; j < nargout; j++)
600 newretval[j].resize (cellstr.
dims ());
604 new_args(0) = cellstr(i);
605 new_args(1) = cellpat(i);
610 for (
int j = 0; j < nargout; j++)
611 newretval[j](i) = tmp(j);
615 error (
"regexp: cell array arguments must be scalar or equal size");
619 for (
int j = 0; j < nargout; j++)
620 newretval[j].resize (cellstr.
dims ());
624 new_args(0) = cellstr(i);
628 for (
int j = 0; j < nargout; j++)
629 newretval[j](i) = tmp(j);
633 for (
int j = 0; j < nargout; j++)
636 else if (args(1).iscell ())
642 for (
int j = 0; j < nargout; j++)
643 newretval[j].resize (cellpat.
dims ());
647 new_args(1) = cellpat(i);
651 for (
int j = 0; j < nargout; j++)
652 newretval[j](i) = tmp(j);
655 for (
int j = 0; j < nargout; j++)
659 retval =
octregexp (args, nargout, who, case_insensitive);
886 if (args.length () < 2)
891 if (args(0).iscell () || args(1).iscell ())
892 retval = (
octcellregexp (args, (nargout > 0 ? nargout : 1),
"regexp"));
894 retval =
octregexp (args, nargout,
"regexp");
1217 DEFUN (regexpi, args, nargout,
1231 if (args.length () < 2)
1234 if (args(0).iscell () || args(1).iscell ())
1235 return octcellregexp (args, (nargout > 0 ? nargout : 1),
"regexpi",
true);
1237 return octregexp (args, nargout,
"regexpi",
true);
1380 int nargin = args.
length ();
1383 const std::string buffer = args(0).string_value ();
1385 std::string pattern = args(1).string_value ();
1390 std::string replacement = args(2).string_value ();
1393 if (args(2).is_sq_string ())
1401 for (
int i = 3; i < nargin; i++)
1403 const std::string opt = args(i).string_value ();
1404 if (opt !=
"tokenize" && opt !=
"start" && opt !=
"end"
1405 && opt !=
"tokenextents" && opt !=
"match" && opt !=
"tokens"
1406 && opt !=
"names" && opt !=
"split" && opt !=
"warnings")
1408 regexpargs(
len++) = args(i);
1414 bool extra_args =
false;
1420 DEFUN (regexprep, args, ,
1462 if (args.length () < 3)
1467 if (args(0).iscell () || args(1).iscell () || args(2).iscell ())
1473 if (args(0).iscell ())
1474 str = args(0).cell_value ();
1476 str =
Cell (args(0));
1478 if (args(1).iscell ())
1479 pat = args(1).cell_value ();
1481 pat =
Cell (args(1));
1483 if (args(2).iscell ())
1484 rep = args(2).cell_value ();
1486 rep =
Cell (args(2));
1489 if (pat.
numel () != 1)
1492 if (rep.
numel () != 1 && dv1 != rep.
dims ())
1493 error (
"regexprep: inconsistent cell array dimensions");
1495 else if (rep.
numel () != 1)
1503 new_args(0) = str(i);
1504 if (pat.
numel () == 1)
1505 new_args(1) = pat(0);
1506 if (rep.
numel () == 1)
1507 new_args(2) = rep(0);
1511 if (pat.
numel () != 1)
1512 new_args(1) = pat(j);
1513 if (rep.
numel () != 1)
1514 new_args(2) = rep(j);
1518 ret(i) = new_args(0);
1521 retval = (args(0).iscell () ?
ovl (ret) :
ovl (ret(0)));
charNDArray min(char d, const charNDArray &m)
OCTARRAY_OVERRIDABLE_FUNC_API octave_idx_type numel(void) const
Number of elements in the array.
OCTARRAY_OVERRIDABLE_FUNC_API const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
std::size_t size(void) const
Vector representing the dimensions (size) of an Array.
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
void assign(const std::string &k, const Cell &val)
Cell cell_value(void) const
void resize(octave_idx_type n, const octave_value &rfv=octave_value())
octave_idx_type length(void) const
string_vector named_patterns(void) const
void dotexceptnewline(bool val)
void lineanchors(bool val)
void case_insensitive(bool val)
void freespacing(bool val)
void emptymatch(bool val)
std::string replace(const std::string &buffer, const std::string &replacement) const
match_data match(const std::string &buffer) const
octave_idx_type numel(void) const
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
OCTINTERP_API void print_usage(void)
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
void warning(const char *fmt,...)
void error(const char *fmt,...)
ColumnVector transform(const Matrix &m, double x, double y, double z)
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
static octave_value_list octregexp(const octave_value_list &args, int nargout, const std::string &who, bool case_insensitive=false)
static void parse_options(regexp::opts &options, const octave_value_list &args, const std::string &who, int skip, bool &extra_args)
static octave_value octregexprep(const octave_value_list &args, const std::string &who)
static std::string do_regexp_rep_string_escapes(const std::string &s)
static std::string do_regexp_ptn_string_escapes(const std::string &s, bool is_sq_str)
static octave_value_list octcellregexp(const octave_value_list &args, int nargout, const std::string &who, bool case_insensitive=false)