26 #if defined (HAVE_CONFIG_H)
61 size_t len = s.length ();
67 if (s[j] ==
'\\' && j+1 <
len)
91 bool bad_esc_seq = (j+1 >=
len);
94 if (! bad_esc_seq && s[++j] ==
'{')
104 int digit = s[k] -
'0';
105 if (digit < 0 || digit > 7)
110 if (bad_esc_seq || (brace && s[k++] !=
'}'))
113 warning (R
"(malformed octal escape sequence '\o' -- converting to '\0')");
147 size_t len = s.length ();
153 if (s[j] ==
'\\' && j+1 <
len)
195 int tmpi = s[j] -
'0';
198 int digit = s[k] -
'0';
199 if (digit < 0 || digit > 7)
211 bool bad_esc_seq = (j+1 >=
len);
214 if (! bad_esc_seq && s[++j] ==
'{')
224 int digit = s[k] -
'0';
225 if (digit < 0 || digit > 7)
230 if (bad_esc_seq || (brace && s[k++] !=
'}'))
232 warning (R
"(malformed octal escape sequence '\o' -- converting to '\0')");
242 bool bad_esc_seq = (j+1 >=
len);
245 if (! bad_esc_seq && s[++j] ==
'{')
255 if (! isxdigit (s[k]))
261 tmpi += digit -
'a' + 10;
262 else if (digit >=
'A')
263 tmpi += digit -
'A' + 10;
267 if (bad_esc_seq || (brace && s[k++] !=
'}'))
269 warning (R
"(malformed hex escape sequence '\x' -- converting to '\0')");
310 const std::string& who,
int skip,
bool& extra_args)
314 for (
int i = skip; i < args.
length (); i++)
318 str = args(i).xstring_value (
"%s: optional arguments must be strings", who.c_str ());
322 if (str.find (
"once", 0) == 0)
324 else if (str.find (
"matchcase", 0) == 0)
326 else if (str.find (
"ignorecase", 0) == 0)
328 else if (str.find (
"dotall", 0) == 0)
330 else if (str.find (
"stringanchors", 0) == 0)
332 else if (str.find (
"literalspacing", 0) == 0)
334 else if (str.find (
"noemptymatch", 0) == 0)
336 else if (str.find (
"dotexceptnewline", 0) == 0)
338 else if (str.find (
"lineanchors", 0) == 0)
340 else if (str.find (
"freespacing", 0) == 0)
342 else if (str.find (
"emptymatch", 0) == 0)
344 else if (str.find (
"start", 0) == 0
345 || str.find (
"end", 0) == 0
346 || str.find (
"tokenextents", 0) == 0
347 || str.find (
"match", 0) == 0
348 || str.find (
"tokens", 0) == 0
349 || str.find (
"names", 0) == 0
350 || str.find (
"split", 0) == 0)
353 error (
"%s: unrecognized option", who.c_str ());
359 const std::string& who,
bool case_insensitive =
false)
363 int nargin = args.
length ();
366 const std::string buffer = args(0).string_value ();
368 std::string pattern = args(1).string_value ();
375 bool extra_options =
false;
383 size_t sz = rx_lst.
size ();
393 for (
int j = 0; j < named_pats.
numel (); j++)
398 for (
const auto& match_data : rx_lst)
402 ctmp(i++) = named_tokens(j);
405 nmap.
assign (named_pats(j), ctmp);
412 auto p = rx_lst.
begin ();
415 retval(3) = (sz ? p->match_string () :
"");
420 double start = p->start ();
421 double end = p->end ();
424 split(0) = buffer.substr (0, start-1);
425 split(1) = buffer.substr (end);
449 for (
const auto& match_data : rx_lst)
451 double s = match_data.start ();
452 double e = match_data.end ();
456 match_string(i) = match_data.match_string ();
457 token_extents(i) = match_data.token_extents ();
460 split(i) = buffer.substr (sp_start, s-sp_start-1);
465 split(i) = buffer.substr (sp_start);
470 retval(2) = token_extents;
481 new_retval.
resize (nargout);
485 for (
int j = 2; j < nargin; j++)
488 std::string str = args(j).string_value ();
491 if (str.find (
"once", 0) == 0
492 || str.find (
"stringanchors", 0) == 0
493 || str.find (
"lineanchors", 0) == 0
494 || str.find (
"matchcase", 0) == 0
495 || str.find (
"ignorecase", 0) == 0
496 || str.find (
"dotall", 0) == 0
497 || str.find (
"dotexceptnewline", 0) == 0
498 || str.find (
"literalspacing", 0) == 0
499 || str.find (
"freespacing", 0) == 0
500 || str.find (
"noemptymatch", 0) == 0
501 || str.find (
"emptymatch", 0) == 0)
503 else if (str.find (
"start", 0) == 0)
505 else if (str.find (
"end", 0) == 0)
507 else if (str.find (
"tokenextents", 0) == 0)
509 else if (str.find (
"match", 0) == 0)
511 else if (str.find (
"tokens", 0) == 0)
513 else if (str.find (
"names", 0) == 0)
515 else if (str.find (
"split", 0) == 0)
528 for (
int j = 0; j < 7; j++)
543 const std::string& who,
bool case_insensitive =
false)
547 if (args(0).iscell ())
552 if (args(1).iscell ())
556 if (cellpat.
numel () == 1)
558 for (
int j = 0; j < nargout; j++)
559 newretval[j].resize (cellstr.
dims ());
561 new_args(1) = cellpat(0);
565 new_args(0) = cellstr(i);
569 for (
int j = 0; j < nargout; j++)
570 newretval[j](i) = tmp(j);
573 else if (cellstr.
numel () == 1)
575 for (
int j = 0; j < nargout; j++)
576 newretval[j].resize (cellpat.
dims ());
578 new_args(0) = cellstr(0);
582 new_args(1) = cellpat(i);
586 for (
int j = 0; j < nargout; j++)
587 newretval[j](i) = tmp(j);
590 else if (cellstr.
numel () == cellpat.
numel ())
592 if (cellstr.
dims () != cellpat.
dims ())
593 error (
"%s: inconsistent cell array dimensions", who.c_str ());
595 for (
int j = 0; j < nargout; j++)
596 newretval[j].resize (cellstr.
dims ());
600 new_args(0) = cellstr(i);
601 new_args(1) = cellpat(i);
606 for (
int j = 0; j < nargout; j++)
607 newretval[j](i) = tmp(j);
611 error (
"regexp: cell array arguments must be scalar or equal size");
615 for (
int j = 0; j < nargout; j++)
616 newretval[j].resize (cellstr.
dims ());
620 new_args(0) = cellstr(i);
624 for (
int j = 0; j < nargout; j++)
625 newretval[j](i) = tmp(j);
629 for (
int j = 0; j < nargout; j++)
632 else if (args(1).iscell ())
638 for (
int j = 0; j < nargout; j++)
639 newretval[j].resize (cellpat.
dims ());
643 new_args(1) = cellpat(i);
647 for (
int j = 0; j < nargout; j++)
648 newretval[j](i) = tmp(j);
651 for (
int j = 0; j < nargout; j++)
661 DEFUN (regexp, args, nargout,
882 if (args.length () < 2)
887 if (args(0).iscell () || args(1).iscell ())
1193 DEFUN (regexpi, args, nargout,
1206 if (args.length () < 2)
1209 if (args(0).iscell () || args(1).iscell ())
1210 return octcellregexp (args, (nargout > 0 ? nargout : 1),
"regexpi",
true);
1212 return octregexp (args, nargout,
"regexpi",
true);
1352 int nargin = args.
length ();
1355 const std::string buffer = args(0).string_value ();
1357 std::string pattern = args(1).string_value ();
1362 std::string replacement = args(2).string_value ();
1365 if (args(2).is_sq_string ())
1373 for (
int i = 3; i < nargin; i++)
1375 const std::string opt = args(i).string_value ();
1376 if (opt !=
"tokenize" && opt !=
"start" && opt !=
"end"
1377 && opt !=
"tokenextents" && opt !=
"match" && opt !=
"tokens"
1378 && opt !=
"names" && opt !=
"split" && opt !=
"warnings")
1380 regexpargs(
len++) = args(i);
1386 bool extra_args =
false;
1392 DEFUN (regexprep, args, ,
1434 if (args.length () < 3)
1439 if (args(0).iscell () || args(1).iscell () || args(2).iscell ())
1445 if (args(0).iscell ())
1446 str = args(0).cell_value ();
1448 str =
Cell (args(0));
1450 if (args(1).iscell ())
1451 pat = args(1).cell_value ();
1453 pat =
Cell (args(1));
1455 if (args(2).iscell ())
1456 rep = args(2).cell_value ();
1458 rep =
Cell (args(2));
1461 if (pat.
numel () != 1)
1464 if (rep.
numel () != 1 && dv1 != rep.
dims ())
1465 error (
"regexprep: inconsistent cell array dimensions");
1467 else if (rep.
numel () != 1)
1475 new_args(0) = str(i);
1476 if (pat.
numel () == 1)
1477 new_args(1) = pat(0);
1478 if (rep.
numel () == 1)
1479 new_args(2) = rep(0);
1483 if (pat.
numel () != 1)
1484 new_args(1) = pat(j);
1485 if (rep.
numel () != 1)
1486 new_args(2) = rep(j);
1490 ret(i) = new_args(0);
1493 retval = (args(0).iscell () ?
ovl (ret) :
ovl (ret(0)));
charNDArray min(char d, const charNDArray &m)
void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
octave_idx_type numel(void) const
Number of elements in the array.
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
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.
string_vector named_patterns(void) const
void lineanchors(bool val)
void case_insensitive(bool val)
void emptymatch(bool val)
void freespacing(bool val)
void dotexceptnewline(bool val)
match_data match(const std::string &buffer)
std::string replace(const std::string &buffer, const std::string &replacement)
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
octave_idx_type numel(void) const
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::octave_value(const Array< char > &chm, char type) return retval
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(octave::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)