26 #if defined (HAVE_CONFIG_H)
55 do_regexp_ptn_string_escapes (
const std::string& s,
bool is_sq_str)
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')");
145 do_regexp_rep_string_escapes (
const std::string& s)
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 ();
375 pattern = do_regexp_ptn_string_escapes (pattern, args(1).is_sq_string ());
379 bool extra_options =
false;
380 parse_options (options, args, who, 2, extra_options);
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);
898 if (args(0).iscell () || args(1).iscell ())
899 retval = (octcellregexp (args, (nargout > 0 ? nargout : 1),
"regexp"));
901 retval = octregexp (args, nargout,
"regexp");
1224 DEFUN (regexpi, args, nargout,
1241 if (args(0).iscell () || args(1).iscell ())
1242 return octcellregexp (args, (nargout > 0 ? nargout : 1),
"regexpi",
true);
1244 return octregexp (args, nargout,
"regexpi",
true);
1387 int nargin = args.
length ();
1390 const std::string buffer = args(0).string_value ();
1392 std::string pattern = args(1).string_value ();
1395 pattern = do_regexp_ptn_string_escapes (pattern, args(1).is_sq_string ());
1397 std::string replacement = args(2).string_value ();
1400 if (args(2).is_sq_string ())
1401 replacement = do_regexp_rep_string_escapes (replacement);
1408 for (
int i = 3; i < nargin; i++)
1410 const std::string opt = args(i).string_value ();
1411 if (opt !=
"tokenize" && opt !=
"start" && opt !=
"end"
1412 && opt !=
"tokenextents" && opt !=
"match" && opt !=
"tokens"
1413 && opt !=
"names" && opt !=
"split" && opt !=
"warnings")
1415 regexpargs(
len++) = args(i);
1418 regexpargs.resize (
len);
1421 bool extra_args =
false;
1422 parse_options (options, regexpargs, who, 0, extra_args);
1427 DEFUN (regexprep, args, ,
1474 if (args(0).iscell () || args(1).iscell () || args(2).iscell ())
1480 if (args(0).iscell ())
1483 str =
Cell (args(0));
1485 if (args(1).iscell ())
1488 pat =
Cell (args(1));
1490 if (args(2).iscell ())
1493 rep =
Cell (args(2));
1496 if (pat.
numel () != 1)
1499 if (rep.
numel () != 1 && dv1 != rep.
dims ())
1500 error (
"regexprep: inconsistent cell array dimensions");
1502 else if (rep.
numel () != 1)
1510 new_args(0) = str(i);
1511 if (pat.
numel () == 1)
1512 new_args(1) = pat(0);
1513 if (rep.
numel () == 1)
1514 new_args(2) = rep(0);
1518 if (pat.
numel () != 1)
1519 new_args(1) = pat(j);
1520 if (rep.
numel () != 1)
1521 new_args(2) = rep(j);
1522 new_args(0) = octregexprep (new_args,
"regexprep");
1525 ret(i) = new_args(0);
1528 retval = (args(0).iscell () ?
ovl (ret) :
ovl (ret(0)));
1531 retval = octregexprep (args,
"regexprep");
1616 OCTAVE_END_NAMESPACE(
octave)
charNDArray min(char d, const charNDArray &m)
const dim_vector & dims() const
Return a const-reference so that dims ()(i) works efficiently.
octave_idx_type numel() const
Number of elements in the array.
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 resize(octave_idx_type n, const octave_value &rfv=octave_value())
octave_idx_type length() const
string_vector named_patterns() 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() const
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#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.