summaryrefslogtreecommitdiff
path: root/re.c
diff options
context:
space:
mode:
Diffstat (limited to 're.c')
-rw-r--r--re.c1249
1 files changed, 778 insertions, 471 deletions
diff --git a/re.c b/re.c
index aee0b741bd..63e2db81ac 100644
--- a/re.c
+++ b/re.c
@@ -16,7 +16,9 @@
#include "encindex.h"
#include "hrtime.h"
#include "internal.h"
+#include "internal/bignum.h"
#include "internal/encoding.h"
+#include "internal/error.h"
#include "internal/hash.h"
#include "internal/imemo.h"
#include "internal/re.h"
@@ -28,15 +30,13 @@
#include "ruby/encoding.h"
#include "ruby/re.h"
#include "ruby/util.h"
+#include "ractor_core.h"
VALUE rb_eRegexpError, rb_eRegexpTimeoutError;
typedef char onig_errmsg_buffer[ONIG_MAX_ERROR_MESSAGE_LEN];
#define errcpy(err, msg) strlcpy((err), (msg), ONIG_MAX_ERROR_MESSAGE_LEN)
-#define BEG(no) (regs->beg[(no)])
-#define END(no) (regs->end[(no)])
-
#if 'a' == 97 /* it's ascii */
static const char casetable[] = {
'\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
@@ -88,6 +88,9 @@ static const char casetable[] = {
# error >>> "You lose. You will need a translation table for your character set." <<<
#endif
+// The process-global timeout for regexp matching
+rb_hrtime_t rb_reg_match_time_limit = 0;
+
int
rb_memcicmp(const void *x, const void *y, long len)
{
@@ -101,7 +104,7 @@ rb_memcicmp(const void *x, const void *y, long len)
return 0;
}
-#ifdef HAVE_MEMMEM
+#if defined(HAVE_MEMMEM) && !defined(__APPLE__)
static inline long
rb_memsearch_ss(const unsigned char *xs, long m, const unsigned char *ys, long n)
{
@@ -286,11 +289,6 @@ rb_memsearch(const void *x0, long m, const void *y0, long n, rb_encoding *enc)
#define KCODE_FIXED FL_USER4
-#define ARG_REG_OPTION_MASK \
- (ONIG_OPTION_IGNORECASE|ONIG_OPTION_MULTILINE|ONIG_OPTION_EXTEND)
-#define ARG_ENCODING_FIXED 16
-#define ARG_ENCODING_NONE 32
-
static int
char_to_option(int c)
{
@@ -450,7 +448,7 @@ rb_reg_expr_str(VALUE str, const char *s, long len,
}
static VALUE
-rb_reg_desc(const char *s, long len, VALUE re)
+rb_reg_desc(VALUE re)
{
rb_encoding *enc = rb_enc_get(re);
VALUE str = rb_str_buf_new2("/");
@@ -463,7 +461,11 @@ rb_reg_desc(const char *s, long len, VALUE re)
else {
rb_enc_associate(str, rb_usascii_encoding());
}
- rb_reg_expr_str(str, s, len, enc, resenc, '/');
+
+ VALUE src_str = RREGEXP_SRC(re);
+ rb_reg_expr_str(str, RSTRING_PTR(src_str), RSTRING_LEN(src_str), enc, resenc, '/');
+ RB_GC_GUARD(src_str);
+
rb_str_buf_cat2(str, "/");
if (re) {
char opts[OPTBUF_SIZE];
@@ -522,7 +524,7 @@ rb_reg_inspect(VALUE re)
if (!RREGEXP_PTR(re) || !RREGEXP_SRC(re) || !RREGEXP_SRC_PTR(re)) {
return rb_any_to_s(re);
}
- return rb_reg_desc(RREGEXP_SRC_PTR(re), RREGEXP_SRC_LEN(re), re);
+ return rb_reg_desc(re);
}
static VALUE rb_reg_str_with_term(VALUE re, int term);
@@ -538,7 +540,7 @@ static VALUE rb_reg_str_with_term(VALUE re, int term);
*
* The returned string may be used as an argument to Regexp.new,
* or as interpolated text for a
- * {Regexp literal}[rdoc-ref:regexp.rdoc@Regexp+Literal]:
+ * {Regexp interpolation}[rdoc-ref:Regexp@Interpolation+Mode]:
*
* r1 = Regexp.new(s0) # => /(?ix-m:ab+c)/
* r2 = /#{s0}/ # => /(?ix-m:ab+c)/
@@ -565,8 +567,6 @@ rb_reg_str_with_term(VALUE re, int term)
{
int options, opt;
const int embeddable = ONIG_OPTION_MULTILINE|ONIG_OPTION_IGNORECASE|ONIG_OPTION_EXTEND;
- long len;
- const UChar* ptr;
VALUE str = rb_str_buf_new2("(?");
char optbuf[OPTBUF_SIZE + 1]; /* for '-' */
rb_encoding *enc = rb_enc_get(re);
@@ -575,8 +575,9 @@ rb_reg_str_with_term(VALUE re, int term)
rb_enc_copy(str, re);
options = RREGEXP_PTR(re)->options;
- ptr = (UChar*)RREGEXP_SRC_PTR(re);
- len = RREGEXP_SRC_LEN(re);
+ VALUE src_str = RREGEXP_SRC(re);
+ const UChar *ptr = (UChar *)RSTRING_PTR(src_str);
+ long len = RSTRING_LEN(src_str);
again:
if (len >= 4 && ptr[0] == '(' && ptr[1] == '?') {
int err = 1;
@@ -666,15 +667,17 @@ rb_reg_str_with_term(VALUE re, int term)
}
rb_enc_copy(str, re);
+ RB_GC_GUARD(src_str);
+
return str;
}
-NORETURN(static void rb_reg_raise(const char *s, long len, const char *err, VALUE re));
+NORETURN(static void rb_reg_raise(const char *err, VALUE re));
static void
-rb_reg_raise(const char *s, long len, const char *err, VALUE re)
+rb_reg_raise(const char *err, VALUE re)
{
- VALUE desc = rb_reg_desc(s, len, re);
+ VALUE desc = rb_reg_desc(re);
rb_raise(rb_eRegexpError, "%s: %"PRIsVALUE, err, desc);
}
@@ -953,24 +956,39 @@ make_regexp(const char *s, long len, rb_encoding *enc, int flags, onig_errmsg_bu
* * <code>$'</code> is Regexp.last_match<code>.post_match</code>;
* * <code>$+</code> is Regexp.last_match<code>[ -1 ]</code> (the last capture).
*
- * See also "Special global variables" section in Regexp documentation.
+ * See also Regexp@Global+Variables.
*/
VALUE rb_cMatch;
static VALUE
-match_alloc(VALUE klass)
+match_alloc_n(VALUE klass, int num_regs)
{
- NEWOBJ_OF(match, struct RMatch, klass, T_MATCH | (RGENGC_WB_PROTECTED_MATCH ? FL_WB_PROTECTED : 0));
+ int capa = num_regs * 2;
+ size_t alloc_size = offsetof(struct RMatch, as) + sizeof(OnigPosition) * capa;
+ if (alloc_size < sizeof(struct RMatch)) {
+ alloc_size = sizeof(struct RMatch);
+ }
- match->str = Qfalse;
- match->rmatch = 0;
- match->regexp = Qfalse;
- match->rmatch = ZALLOC(struct rmatch);
+ VALUE flags = T_MATCH;
+ if (!rb_gc_size_allocatable_p(alloc_size)) {
+ alloc_size = sizeof(struct RMatch);
+ flags |= RMATCH_ONIG;
+ capa = 0;
+ }
+ NEWOBJ_OF(match, struct RMatch, klass, flags, alloc_size);
+ memset(((char *)match) + sizeof(struct RBasic), 0, alloc_size - sizeof(struct RBasic));
+ match->capa = capa;
return (VALUE)match;
}
+static VALUE
+match_alloc(VALUE klass)
+{
+ return match_alloc_n(klass, 0);
+}
+
int
rb_reg_region_copy(struct re_registers *to, const struct re_registers *from)
{
@@ -982,6 +1000,54 @@ rb_reg_region_copy(struct re_registers *to, const struct re_registers *from)
return ONIGERR_MEMORY;
}
+static void
+match_to_onig(VALUE match, int num_regs, const OnigPosition *src_beg, const OnigPosition *src_end)
+{
+ struct RMatch *rm = RMATCH(match);
+ struct re_registers tmp = {0};
+ if (onig_region_resize(&tmp, num_regs)) {
+ rb_memerror();
+ }
+ memcpy(tmp.beg, src_beg, num_regs * sizeof(OnigPosition));
+ memcpy(tmp.end, src_end, num_regs * sizeof(OnigPosition));
+ rm->as.onig = tmp;
+ FL_SET_RAW(match, RMATCH_ONIG);
+}
+
+void
+rb_match_ensure_onig(VALUE match)
+{
+ if (FL_TEST_RAW(match, RMATCH_ONIG)) return;
+ struct RMatch *rm = RMATCH(match);
+ int n = rm->num_regs;
+ match_to_onig(match, n, &rm->as.embed[0], &rm->as.embed[n]);
+}
+
+/* Replace `match`'s registers with a copy of (num_regs, beg, end). If the
+ * data does not fit in the embed form, the match is evicted to onig form.
+ * Raises on OOM. */
+static void
+match_set_regs(VALUE match, int num_regs, const OnigPosition *beg, const OnigPosition *end)
+{
+ struct RMatch *rm = RMATCH(match);
+
+ if (FL_TEST_RAW(match, RMATCH_ONIG)) {
+ if (onig_region_resize(&rm->as.onig, num_regs)) {
+ rb_memerror();
+ }
+ memcpy(rm->as.onig.beg, beg, num_regs * sizeof(OnigPosition));
+ memcpy(rm->as.onig.end, end, num_regs * sizeof(OnigPosition));
+ }
+ else if (num_regs * 2 <= rm->capa) {
+ memcpy(&rm->as.embed[0], beg, num_regs * sizeof(OnigPosition));
+ memcpy(&rm->as.embed[num_regs], end, num_regs * sizeof(OnigPosition));
+ }
+ else {
+ match_to_onig(match, num_regs, beg, end);
+ }
+ rm->num_regs = num_regs;
+}
+
typedef struct {
long byte_pos;
long char_pos;
@@ -1001,41 +1067,41 @@ pair_byte_cmp(const void *pair1, const void *pair2)
static void
update_char_offset(VALUE match)
{
- struct rmatch *rm = RMATCH(match)->rmatch;
- struct re_registers *regs;
+ struct RMatch *rm = RMATCH(match);
int i, num_regs, num_pos;
long c;
char *s, *p, *q;
rb_encoding *enc;
pair_t *pairs;
+ VALUE pairs_obj = Qnil;
if (rm->char_offset_num_allocated)
return;
- regs = &rm->regs;
- num_regs = rm->regs.num_regs;
+ num_regs = RMATCH_NREGS(match);
if (rm->char_offset_num_allocated < num_regs) {
- REALLOC_N(rm->char_offset, struct rmatch_offset, num_regs);
+ SIZED_REALLOC_N(rm->char_offset, struct rmatch_offset, num_regs, rm->char_offset_num_allocated);
rm->char_offset_num_allocated = num_regs;
+ FL_SET_RAW(match, RMATCH_OFFSETS_EXTERNAL);
}
enc = rb_enc_get(RMATCH(match)->str);
if (rb_enc_mbmaxlen(enc) == 1) {
for (i = 0; i < num_regs; i++) {
- rm->char_offset[i].beg = BEG(i);
- rm->char_offset[i].end = END(i);
+ rm->char_offset[i].beg = RMATCH_BEG(match, i);
+ rm->char_offset[i].end = RMATCH_END(match, i);
}
return;
}
- pairs = ALLOCA_N(pair_t, num_regs*2);
+ pairs = RB_ALLOCV_N(pair_t, pairs_obj, num_regs * 2);
num_pos = 0;
for (i = 0; i < num_regs; i++) {
- if (BEG(i) < 0)
+ if (RMATCH_BEG(match, i) < 0)
continue;
- pairs[num_pos++].byte_pos = BEG(i);
- pairs[num_pos++].byte_pos = END(i);
+ pairs[num_pos++].byte_pos = RMATCH_BEG(match, i);
+ pairs[num_pos++].byte_pos = RMATCH_END(match, i);
}
qsort(pairs, num_pos, sizeof(pair_t), pair_byte_cmp);
@@ -1050,20 +1116,22 @@ update_char_offset(VALUE match)
for (i = 0; i < num_regs; i++) {
pair_t key, *found;
- if (BEG(i) < 0) {
+ if (RMATCH_BEG(match, i) < 0) {
rm->char_offset[i].beg = -1;
rm->char_offset[i].end = -1;
continue;
}
- key.byte_pos = BEG(i);
+ key.byte_pos = RMATCH_BEG(match, i);
found = bsearch(&key, pairs, num_pos, sizeof(pair_t), pair_byte_cmp);
rm->char_offset[i].beg = found->char_pos;
- key.byte_pos = END(i);
+ key.byte_pos = RMATCH_END(match, i);
found = bsearch(&key, pairs, num_pos, sizeof(pair_t), pair_byte_cmp);
rm->char_offset[i].end = found->char_pos;
}
+
+ RB_ALLOCV_END(pairs_obj);
}
static VALUE
@@ -1079,24 +1147,23 @@ match_check(VALUE match)
static VALUE
match_init_copy(VALUE obj, VALUE orig)
{
- struct rmatch *rm;
+ struct RMatch *rm = RMATCH(obj);
if (!OBJ_INIT_COPY(obj, orig)) return obj;
- RB_OBJ_WRITE(obj, &RMATCH(obj)->str, RMATCH(orig)->str);
- RB_OBJ_WRITE(obj, &RMATCH(obj)->regexp, RMATCH(orig)->regexp);
+ RB_OBJ_WRITE(obj, &rm->str, RMATCH(orig)->str);
+ RB_OBJ_WRITE(obj, &rm->regexp, RMATCH(orig)->regexp);
- rm = RMATCH(obj)->rmatch;
- if (rb_reg_region_copy(&rm->regs, RMATCH_REGS(orig)))
- rb_memerror();
+ match_set_regs(obj, RMATCH_NREGS(orig), RMATCH_BEG_PTR(orig), RMATCH_END_PTR(orig));
- if (RMATCH(orig)->rmatch->char_offset_num_allocated) {
- if (rm->char_offset_num_allocated < rm->regs.num_regs) {
- REALLOC_N(rm->char_offset, struct rmatch_offset, rm->regs.num_regs);
- rm->char_offset_num_allocated = rm->regs.num_regs;
+ if (RMATCH(orig)->char_offset_num_allocated) {
+ if (rm->char_offset_num_allocated < rm->num_regs) {
+ SIZED_REALLOC_N(rm->char_offset, struct rmatch_offset, rm->num_regs, rm->char_offset_num_allocated);
+ rm->char_offset_num_allocated = rm->num_regs;
+ FL_SET_RAW(obj, RMATCH_OFFSETS_EXTERNAL);
}
- MEMCPY(rm->char_offset, RMATCH(orig)->rmatch->char_offset,
- struct rmatch_offset, rm->regs.num_regs);
+ MEMCPY(rm->char_offset, RMATCH(orig)->char_offset,
+ struct rmatch_offset, rm->num_regs);
RB_GC_GUARD(orig);
}
@@ -1175,10 +1242,10 @@ static VALUE
match_size(VALUE match)
{
match_check(match);
- return INT2FIX(RMATCH_REGS(match)->num_regs);
+ return INT2FIX(RMATCH_NREGS(match));
}
-static int name_to_backref_number(struct re_registers *, VALUE, const char*, const char*);
+static int match_name_to_backref_number(VALUE match, VALUE name);
NORETURN(static void name_to_backref_error(VALUE name));
static void
@@ -1189,21 +1256,17 @@ name_to_backref_error(VALUE name)
}
static void
-backref_number_check(struct re_registers *regs, int i)
+backref_number_check(VALUE match, int i)
{
- if (i < 0 || regs->num_regs <= i)
+ if (i < 0 || RMATCH_NREGS(match) <= i)
rb_raise(rb_eIndexError, "index %d out of matches", i);
}
static int
match_backref_number(VALUE match, VALUE backref)
{
- const char *name;
int num;
- struct re_registers *regs = RMATCH_REGS(match);
- VALUE regexp = RMATCH(match)->regexp;
-
match_check(match);
if (SYMBOL_P(backref)) {
backref = rb_sym2str(backref);
@@ -1211,9 +1274,8 @@ match_backref_number(VALUE match, VALUE backref)
else if (!RB_TYPE_P(backref, T_STRING)) {
return NUM2INT(backref);
}
- name = StringValueCStr(backref);
- num = name_to_backref_number(regs, regexp, name, name + RSTRING_LEN(backref));
+ num = match_name_to_backref_number(match, backref);
if (num < 1) {
name_to_backref_error(backref);
@@ -1241,17 +1303,16 @@ static VALUE
match_offset(VALUE match, VALUE n)
{
int i = match_backref_number(match, n);
- struct re_registers *regs = RMATCH_REGS(match);
match_check(match);
- backref_number_check(regs, i);
+ backref_number_check(match, i);
- if (BEG(i) < 0)
+ if (RMATCH_BEG(match, i) < 0)
return rb_assoc_new(Qnil, Qnil);
update_char_offset(match);
- return rb_assoc_new(LONG2NUM(RMATCH(match)->rmatch->char_offset[i].beg),
- LONG2NUM(RMATCH(match)->rmatch->char_offset[i].end));
+ return rb_assoc_new(LONG2NUM(RMATCH(match)->char_offset[i].beg),
+ LONG2NUM(RMATCH(match)->char_offset[i].end));
}
/*
@@ -1276,14 +1337,59 @@ static VALUE
match_byteoffset(VALUE match, VALUE n)
{
int i = match_backref_number(match, n);
- struct re_registers *regs = RMATCH_REGS(match);
match_check(match);
- backref_number_check(regs, i);
+ backref_number_check(match, i);
- if (BEG(i) < 0)
+ if (RMATCH_BEG(match, i) < 0)
return rb_assoc_new(Qnil, Qnil);
- return rb_assoc_new(LONG2NUM(BEG(i)), LONG2NUM(END(i)));
+ return rb_assoc_new(LONG2NUM(RMATCH_BEG(match, i)), LONG2NUM(RMATCH_END(match, i)));
+}
+
+
+/*
+ * call-seq:
+ * bytebegin(n) -> integer
+ * bytebegin(name) -> integer
+ *
+ * :include: doc/matchdata/bytebegin.rdoc
+ *
+ */
+
+static VALUE
+match_bytebegin(VALUE match, VALUE n)
+{
+ int i = match_backref_number(match, n);
+
+ match_check(match);
+ backref_number_check(match, i);
+
+ if (RMATCH_BEG(match, i) < 0)
+ return Qnil;
+ return LONG2NUM(RMATCH_BEG(match, i));
+}
+
+
+/*
+ * call-seq:
+ * byteend(n) -> integer
+ * byteend(name) -> integer
+ *
+ * :include: doc/matchdata/byteend.rdoc
+ *
+ */
+
+static VALUE
+match_byteend(VALUE match, VALUE n)
+{
+ int i = match_backref_number(match, n);
+
+ match_check(match);
+ backref_number_check(match, i);
+
+ if (RMATCH_BEG(match, i) < 0)
+ return Qnil;
+ return LONG2NUM(RMATCH_END(match, i));
}
@@ -1300,16 +1406,15 @@ static VALUE
match_begin(VALUE match, VALUE n)
{
int i = match_backref_number(match, n);
- struct re_registers *regs = RMATCH_REGS(match);
match_check(match);
- backref_number_check(regs, i);
+ backref_number_check(match, i);
- if (BEG(i) < 0)
+ if (RMATCH_BEG(match, i) < 0)
return Qnil;
update_char_offset(match);
- return LONG2NUM(RMATCH(match)->rmatch->char_offset[i].beg);
+ return LONG2NUM(RMATCH(match)->char_offset[i].beg);
}
@@ -1326,16 +1431,15 @@ static VALUE
match_end(VALUE match, VALUE n)
{
int i = match_backref_number(match, n);
- struct re_registers *regs = RMATCH_REGS(match);
match_check(match);
- backref_number_check(regs, i);
+ backref_number_check(match, i);
- if (BEG(i) < 0)
+ if (RMATCH_BEG(match, i) < 0)
return Qnil;
update_char_offset(match);
- return LONG2NUM(RMATCH(match)->rmatch->char_offset[i].end);
+ return LONG2NUM(RMATCH(match)->char_offset[i].end);
}
/*
@@ -1368,11 +1472,10 @@ static VALUE
match_nth(VALUE match, VALUE n)
{
int i = match_backref_number(match, n);
- struct re_registers *regs = RMATCH_REGS(match);
- backref_number_check(regs, i);
+ backref_number_check(match, i);
- long start = BEG(i), end = END(i);
+ long start = RMATCH_BEG(match, i), end = RMATCH_END(match, i);
if (start < 0)
return Qnil;
@@ -1412,17 +1515,16 @@ static VALUE
match_nth_length(VALUE match, VALUE n)
{
int i = match_backref_number(match, n);
- struct re_registers *regs = RMATCH_REGS(match);
match_check(match);
- backref_number_check(regs, i);
+ backref_number_check(match, i);
- if (BEG(i) < 0)
+ if (RMATCH_BEG(match, i) < 0)
return Qnil;
update_char_offset(match);
const struct rmatch_offset *const ofs =
- &RMATCH(match)->rmatch->char_offset[i];
+ &RMATCH(match)->char_offset[i];
return LONG2NUM(ofs->end - ofs->beg);
}
@@ -1443,53 +1545,39 @@ rb_match_unbusy(VALUE match)
int
rb_match_count(VALUE match)
{
- struct re_registers *regs;
if (NIL_P(match)) return -1;
- regs = RMATCH_REGS(match);
- if (!regs) return -1;
- return regs->num_regs;
+ return RMATCH_NREGS(match);
}
-int
-rb_match_nth_defined(int nth, VALUE match)
+static VALUE
+match_alloc_or_reuse(VALUE existing, int num_regs)
{
- struct re_registers *regs;
- if (NIL_P(match)) return FALSE;
- regs = RMATCH_REGS(match);
- if (!regs) return FALSE;
- if (nth >= regs->num_regs) {
- return FALSE;
+ if (!NIL_P(existing) &&
+ !FL_TEST(existing, MATCH_BUSY) &&
+ RMATCH(existing)->capa >= num_regs * 2) {
+ return existing;
}
- if (nth < 0) {
- nth += regs->num_regs;
- if (nth <= 0) return FALSE;
- }
- return (BEG(nth) != -1);
+ return match_alloc_n(rb_cMatch, num_regs);
}
static void
match_set_string(VALUE m, VALUE string, long pos, long len)
{
struct RMatch *match = (struct RMatch *)m;
- struct rmatch *rmatch = match->rmatch;
- RB_OBJ_WRITE(match, &RMATCH(match)->str, string);
- RB_OBJ_WRITE(match, &RMATCH(match)->regexp, Qnil);
- int err = onig_region_resize(&rmatch->regs, 1);
- if (err) rb_memerror();
- rmatch->regs.beg[0] = pos;
- rmatch->regs.end[0] = pos + len;
+ RB_OBJ_WRITE(match, &match->str, string);
+ RB_OBJ_WRITE(match, &match->regexp, Qnil);
+ OnigPosition beg = pos, end = pos + len;
+ match_set_regs(m, 1, &beg, &end);
}
-void
+VALUE
rb_backref_set_string(VALUE string, long pos, long len)
{
- VALUE match = rb_backref_get();
- if (NIL_P(match) || FL_TEST(match, MATCH_BUSY)) {
- match = match_alloc(rb_cMatch);
- }
+ VALUE match = match_alloc_or_reuse(rb_backref_get(), 1);
match_set_string(match, string, pos, len);
rb_backref_set(match);
+ return match;
}
/*
@@ -1538,25 +1626,15 @@ reg_enc_error(VALUE re, VALUE str)
{
rb_raise(rb_eEncCompatError,
"incompatible encoding regexp match (%s regexp with %s string)",
- rb_enc_name(rb_enc_get(re)),
- rb_enc_name(rb_enc_get(str)));
-}
-
-static inline int
-str_coderange(VALUE str)
-{
- int cr = ENC_CODERANGE(str);
- if (cr == ENC_CODERANGE_UNKNOWN) {
- cr = rb_enc_str_coderange(str);
- }
- return cr;
+ rb_enc_inspect_name(rb_enc_get(re)),
+ rb_enc_inspect_name(rb_enc_get(str)));
}
static rb_encoding*
rb_reg_prepare_enc(VALUE re, VALUE str, int warn)
{
rb_encoding *enc = 0;
- int cr = str_coderange(str);
+ int cr = rb_enc_str_coderange(str);
if (cr == ENC_CODERANGE_BROKEN) {
rb_raise(rb_eArgError,
@@ -1592,24 +1670,25 @@ rb_reg_prepare_enc(VALUE re, VALUE str, int warn)
}
regex_t *
-rb_reg_prepare_re0(VALUE re, VALUE str, onig_errmsg_buffer err)
+rb_reg_prepare_re(VALUE re, VALUE str)
{
- regex_t *reg = RREGEXP_PTR(re);
int r;
OnigErrorInfo einfo;
- const char *pattern;
VALUE unescaped;
rb_encoding *fixed_enc = 0;
rb_encoding *enc = rb_reg_prepare_enc(re, str, 1);
+ regex_t *reg = RREGEXP_PTR(re);
if (reg->enc == enc) return reg;
rb_reg_check(re);
- reg = RREGEXP_PTR(re);
- pattern = RREGEXP_SRC_PTR(re);
+ VALUE src_str = RREGEXP_SRC(re);
+ const char *pattern = RSTRING_PTR(src_str);
+
+ onig_errmsg_buffer err = "";
unescaped = rb_reg_preprocess(
- pattern, pattern + RREGEXP_SRC_LEN(re), enc,
+ pattern, pattern + RSTRING_LEN(src_str), enc,
&fixed_enc, err, 0);
if (NIL_P(unescaped)) {
@@ -1622,25 +1701,74 @@ rb_reg_prepare_re0(VALUE re, VALUE str, onig_errmsg_buffer err)
const char *ptr;
long len;
RSTRING_GETMEM(unescaped, ptr, len);
- r = onig_new(&reg, (UChar *)ptr, (UChar *)(ptr + len),
- reg->options, enc,
- OnigDefaultSyntax, &einfo);
+
+ /* If there are no other users of this regex, then we can directly overwrite it. */
+ if (ruby_single_main_ractor && RREGEXP(re)->usecnt == 0) {
+ regex_t tmp_reg;
+ r = onig_new_without_alloc(&tmp_reg, (UChar *)ptr, (UChar *)(ptr + len),
+ reg->options, enc,
+ OnigDefaultSyntax, &einfo);
+
+ if (r) {
+ /* There was an error so perform cleanups. */
+ onig_free_body(&tmp_reg);
+ }
+ else {
+ onig_free_body(reg);
+ /* There are no errors so set reg to tmp_reg. */
+ *reg = tmp_reg;
+ }
+ }
+ else {
+ r = onig_new(&reg, (UChar *)ptr, (UChar *)(ptr + len),
+ reg->options, enc,
+ OnigDefaultSyntax, &einfo);
+ }
+
if (r) {
onig_error_code_to_str((UChar*)err, r, &einfo);
- rb_reg_raise(pattern, RREGEXP_SRC_LEN(re), err, re);
+ rb_reg_raise(err, re);
}
reg->timelimit = timelimit;
RB_GC_GUARD(unescaped);
+ RB_GC_GUARD(src_str);
return reg;
}
-regex_t *
-rb_reg_prepare_re(VALUE re, VALUE str)
+OnigPosition
+rb_reg_onig_match(VALUE re, VALUE str,
+ OnigPosition (*match)(regex_t *reg, VALUE str, struct re_registers *regs, void *args),
+ void *args, struct re_registers *regs)
{
- onig_errmsg_buffer err = "";
- return rb_reg_prepare_re0(re, str, err);
+ regex_t *reg = rb_reg_prepare_re(re, str);
+
+ bool tmpreg = reg != RREGEXP_PTR(re);
+ if (!tmpreg) RREGEXP(re)->usecnt++;
+
+ OnigPosition result = match(reg, str, regs, args);
+
+ if (!tmpreg) RREGEXP(re)->usecnt--;
+ if (tmpreg) {
+ onig_free(reg);
+ }
+
+ if (result < 0) {
+ switch (result) {
+ case ONIG_MISMATCH:
+ break;
+ case ONIGERR_TIMEOUT:
+ rb_raise(rb_eRegexpTimeoutError, "regexp match timeout");
+ default: {
+ onig_errmsg_buffer err = "";
+ onig_error_code_to_str((UChar*)err, (int)result);
+ rb_reg_raise(err, re);
+ }
+ }
+ }
+
+ return result;
}
long
@@ -1674,68 +1802,76 @@ rb_reg_adjust_startpos(VALUE re, VALUE str, long pos, int reverse)
return pos;
}
+struct reg_onig_search_args {
+ long pos;
+ long range;
+};
+
+static OnigPosition
+reg_onig_search(regex_t *reg, VALUE str, struct re_registers *regs, void *args_ptr)
+{
+ struct reg_onig_search_args *args = (struct reg_onig_search_args *)args_ptr;
+ const char *ptr;
+ long len;
+ RSTRING_GETMEM(str, ptr, len);
+
+ return onig_search(
+ reg,
+ (UChar *)ptr,
+ (UChar *)(ptr + len),
+ (UChar *)(ptr + args->pos),
+ (UChar *)(ptr + args->range),
+ regs,
+ ONIG_OPTION_NONE);
+}
+
/* returns byte offset */
static long
rb_reg_search_set_match(VALUE re, VALUE str, long pos, int reverse, int set_backref_str, VALUE *set_match)
{
- long result;
- VALUE match;
- struct re_registers regi, *regs = &regi;
- char *start, *range;
- long len;
- regex_t *reg;
- int tmpreg;
- onig_errmsg_buffer err = "";
-
- RSTRING_GETMEM(str, start, len);
- range = start;
+ long len = RSTRING_LEN(str);
if (pos > len || pos < 0) {
rb_backref_set(Qnil);
return -1;
}
- reg = rb_reg_prepare_re0(re, str, err);
- tmpreg = reg != RREGEXP_PTR(re);
- if (!tmpreg) RREGEXP(re)->usecnt++;
+ struct reg_onig_search_args args = {
+ .pos = pos,
+ .range = reverse ? 0 : len,
+ };
- MEMZERO(regs, struct re_registers, 1);
- if (!reverse) {
- range += len;
- }
- result = onig_search(reg,
- (UChar*)start,
- ((UChar*)(start + len)),
- ((UChar*)(start + pos)),
- ((UChar*)range),
- regs, ONIG_OPTION_NONE);
- if (!tmpreg) RREGEXP(re)->usecnt--;
- if (tmpreg) {
- if (RREGEXP(re)->usecnt) {
- onig_free(reg);
- }
- else {
- onig_free(RREGEXP_PTR(re));
- RREGEXP_PTR(re) = reg;
- }
- }
- if (result < 0) {
- if (regs == &regi)
- onig_region_free(regs, 0);
- if (result == ONIG_MISMATCH) {
- rb_backref_set(Qnil);
- return result;
- }
- else {
- onig_error_code_to_str((UChar*)err, (int)result);
- rb_reg_raise(RREGEXP_SRC_PTR(re), RREGEXP_SRC_LEN(re), err, re);
- }
+ rb_reg_check(re);
+
+ /* Stack-backed regs sized to max(num_mem+1, ONIG_NREGION) so
+ * onig_region_resize_clear takes its no-op branch. */
+ int n = RREGEXP_PTR(re)->num_mem + 1;
+ int cap = n < ONIG_NREGION ? ONIG_NREGION : n;
+ VALUE regs_buf;
+ OnigPosition *buf = ALLOCV_N(OnigPosition, regs_buf, (size_t)cap * 2);
+ struct re_registers regs = {
+ .allocated = cap,
+ .num_regs = 0,
+ .beg = buf,
+ .end = buf + cap,
+ };
+
+ OnigPosition result = rb_reg_onig_match(re, str, reg_onig_search, &args, &regs);
+
+ if (result == ONIG_MISMATCH) {
+ ALLOCV_END(regs_buf);
+ rb_backref_set(Qnil);
+ return ONIG_MISMATCH;
}
- match = match_alloc(rb_cMatch);
- memcpy(RMATCH_REGS(match), regs, sizeof(struct re_registers));
+ VALUE existing = (set_match && !NIL_P(*set_match)) ? *set_match : rb_backref_get();
+ VALUE match = match_alloc_or_reuse(existing, regs.num_regs);
+
+ match_set_regs(match, regs.num_regs, regs.beg, regs.end);
+ ALLOCV_END(regs_buf);
if (set_backref_str) {
RB_OBJ_WRITE(match, &RMATCH(match)->str, rb_str_new4(str));
+ rb_obj_reveal(match, rb_cMatch);
}
else {
/* Note that a MatchData object with RMATCH(match)->str == 0 is incomplete!
@@ -1753,82 +1889,59 @@ rb_reg_search_set_match(VALUE re, VALUE str, long pos, int reverse, int set_back
}
long
-rb_reg_search0(VALUE re, VALUE str, long pos, int reverse, int set_backref_str)
+rb_reg_search0(VALUE re, VALUE str, long pos, int reverse, int set_backref_str, VALUE *match)
{
- return rb_reg_search_set_match(re, str, pos, reverse, set_backref_str, NULL);
+ return rb_reg_search_set_match(re, str, pos, reverse, set_backref_str, match);
}
long
rb_reg_search(VALUE re, VALUE str, long pos, int reverse)
{
- return rb_reg_search0(re, str, pos, reverse, 1);
+ return rb_reg_search_set_match(re, str, pos, reverse, 1, NULL);
}
-bool
-rb_reg_start_with_p(VALUE re, VALUE str)
+static OnigPosition
+reg_onig_match(regex_t *reg, VALUE str, struct re_registers *regs, void *_)
{
- long result;
- VALUE match;
- struct re_registers regi, *regs = &regi;
- regex_t *reg;
- int tmpreg;
- onig_errmsg_buffer err = "";
-
- reg = rb_reg_prepare_re0(re, str, err);
- tmpreg = reg != RREGEXP_PTR(re);
- if (!tmpreg) RREGEXP(re)->usecnt++;
-
- match = rb_backref_get();
- if (!NIL_P(match)) {
- if (FL_TEST(match, MATCH_BUSY)) {
- match = Qnil;
- }
- else {
- regs = RMATCH_REGS(match);
- }
- }
- if (NIL_P(match)) {
- MEMZERO(regs, struct re_registers, 1);
- }
const char *ptr;
long len;
RSTRING_GETMEM(str, ptr, len);
- result = onig_match(reg,
- (UChar*)(ptr),
- ((UChar*)(ptr + len)),
- (UChar*)(ptr),
- regs, ONIG_OPTION_NONE);
- if (!tmpreg) RREGEXP(re)->usecnt--;
- if (tmpreg) {
- if (RREGEXP(re)->usecnt) {
- onig_free(reg);
- }
- else {
- onig_free(RREGEXP_PTR(re));
- RREGEXP_PTR(re) = reg;
- }
- }
- if (result < 0) {
- if (regs == &regi)
- onig_region_free(regs, 0);
- if (result == ONIG_MISMATCH) {
- rb_backref_set(Qnil);
- return false;
- }
- else {
- onig_error_code_to_str((UChar*)err, (int)result);
- rb_reg_raise(RREGEXP_SRC_PTR(re), RREGEXP_SRC_LEN(re), err, re);
- }
- }
- if (NIL_P(match)) {
- int err;
- match = match_alloc(rb_cMatch);
- err = rb_reg_region_copy(RMATCH_REGS(match), regs);
- onig_region_free(regs, 0);
- if (err) rb_memerror();
+ return onig_match(
+ reg,
+ (UChar *)ptr,
+ (UChar *)(ptr + len),
+ (UChar *)ptr,
+ regs,
+ ONIG_OPTION_NONE);
+}
+
+bool
+rb_reg_start_with_p(VALUE re, VALUE str)
+{
+ rb_reg_check(re);
+
+ int n = RREGEXP_PTR(re)->num_mem + 1;
+ int cap = n < ONIG_NREGION ? ONIG_NREGION : n;
+ VALUE regs_buf;
+ OnigPosition *buf = ALLOCV_N(OnigPosition, regs_buf, (size_t)cap * 2);
+ struct re_registers regs = {
+ .allocated = cap,
+ .num_regs = 0,
+ .beg = buf,
+ .end = buf + cap,
+ };
+
+ if (rb_reg_onig_match(re, str, reg_onig_match, NULL, &regs) == ONIG_MISMATCH) {
+ ALLOCV_END(regs_buf);
+ rb_backref_set(Qnil);
+ return false;
}
+ VALUE match = match_alloc_or_reuse(rb_backref_get(), regs.num_regs);
+ match_set_regs(match, regs.num_regs, regs.beg, regs.end);
+ ALLOCV_END(regs_buf);
+
RB_OBJ_WRITE(match, &RMATCH(match)->str, rb_str_new4(str));
RB_OBJ_WRITE(match, &RMATCH(match)->regexp, re);
rb_backref_set(match);
@@ -1839,18 +1952,17 @@ rb_reg_start_with_p(VALUE re, VALUE str)
VALUE
rb_reg_nth_defined(int nth, VALUE match)
{
- struct re_registers *regs;
if (NIL_P(match)) return Qnil;
match_check(match);
- regs = RMATCH_REGS(match);
- if (nth >= regs->num_regs) {
+ int num_regs = RMATCH_NREGS(match);
+ if (nth >= num_regs) {
return Qnil;
}
if (nth < 0) {
- nth += regs->num_regs;
+ nth += num_regs;
if (nth <= 0) return Qnil;
}
- return RBOOL(BEG(nth) != -1);
+ return RBOOL(RMATCH_BEG(match, nth) != -1);
}
VALUE
@@ -1858,21 +1970,20 @@ rb_reg_nth_match(int nth, VALUE match)
{
VALUE str;
long start, end, len;
- struct re_registers *regs;
if (NIL_P(match)) return Qnil;
match_check(match);
- regs = RMATCH_REGS(match);
- if (nth >= regs->num_regs) {
+ int num_regs = RMATCH_NREGS(match);
+ if (nth >= num_regs) {
return Qnil;
}
if (nth < 0) {
- nth += regs->num_regs;
+ nth += num_regs;
if (nth <= 0) return Qnil;
}
- start = BEG(nth);
+ start = RMATCH_BEG(match, nth);
if (start == -1) return Qnil;
- end = END(nth);
+ end = RMATCH_END(match, nth);
len = end - start;
str = rb_str_subseq(RMATCH(match)->str, start, len);
return str;
@@ -1906,13 +2017,11 @@ VALUE
rb_reg_match_pre(VALUE match)
{
VALUE str;
- struct re_registers *regs;
if (NIL_P(match)) return Qnil;
match_check(match);
- regs = RMATCH_REGS(match);
- if (BEG(0) == -1) return Qnil;
- str = rb_str_subseq(RMATCH(match)->str, 0, BEG(0));
+ if (RMATCH_BEG(match, 0) == -1) return Qnil;
+ str = rb_str_subseq(RMATCH(match)->str, 0, RMATCH_BEG(match, 0));
return str;
}
@@ -1940,33 +2049,45 @@ rb_reg_match_post(VALUE match)
{
VALUE str;
long pos;
- struct re_registers *regs;
if (NIL_P(match)) return Qnil;
match_check(match);
- regs = RMATCH_REGS(match);
- if (BEG(0) == -1) return Qnil;
+ if (RMATCH_BEG(match, 0) == -1) return Qnil;
str = RMATCH(match)->str;
- pos = END(0);
+ pos = RMATCH_END(match, 0);
str = rb_str_subseq(str, pos, RSTRING_LEN(str) - pos);
return str;
}
-VALUE
-rb_reg_match_last(VALUE match)
+static int
+match_last_index(VALUE match)
{
int i;
- struct re_registers *regs;
- if (NIL_P(match)) return Qnil;
+ if (NIL_P(match)) return -1;
match_check(match);
- regs = RMATCH_REGS(match);
- if (BEG(0) == -1) return Qnil;
+ if (RMATCH_BEG(match, 0) == -1) return -1;
- for (i=regs->num_regs-1; BEG(i) == -1 && i > 0; i--)
+ for (i = RMATCH_NREGS(match) - 1; RMATCH_BEG(match, i) == -1 && i > 0; i--)
;
- if (i == 0) return Qnil;
- return rb_reg_nth_match(i, match);
+ return i;
+}
+
+VALUE
+rb_reg_match_last(VALUE match)
+{
+ int i = match_last_index(match);
+ if (i <= 0) return Qnil;
+ long start = RMATCH_BEG(match, i);
+ return rb_str_subseq(RMATCH(match)->str, start, RMATCH_END(match, i) - start);
+}
+
+VALUE
+rb_reg_last_defined(VALUE match)
+{
+ int i = match_last_index(match);
+ if (i < 0) return Qnil;
+ return RBOOL(i);
}
static VALUE
@@ -1996,22 +2117,22 @@ last_paren_match_getter(ID _x, VALUE *_y)
static VALUE
match_array(VALUE match, int start)
{
- struct re_registers *regs;
VALUE ary;
VALUE target;
int i;
match_check(match);
- regs = RMATCH_REGS(match);
- ary = rb_ary_new2(regs->num_regs);
+ int num_regs = RMATCH_NREGS(match);
+ ary = rb_ary_new2(num_regs);
target = RMATCH(match)->str;
- for (i=start; i<regs->num_regs; i++) {
- if (regs->beg[i] == -1) {
+ for (i = start; i < num_regs; i++) {
+ long beg = RMATCH_BEG(match, i);
+ if (beg == -1) {
rb_ary_push(ary, Qnil);
}
else {
- VALUE str = rb_str_subseq(target, regs->beg[i], regs->end[i]-regs->beg[i]);
+ VALUE str = rb_str_subseq(target, beg, RMATCH_END(match, i) - beg);
rb_ary_push(ary, str);
}
}
@@ -2062,7 +2183,7 @@ match_captures(VALUE match)
}
static int
-name_to_backref_number(struct re_registers *regs, VALUE regexp, const char* name, const char* name_end)
+name_to_backref_number(const struct re_registers *regs, VALUE regexp, const char* name, const char* name_end)
{
if (NIL_P(regexp)) return -1;
return onig_name_to_backref_number(RREGEXP_PTR(regexp),
@@ -2075,7 +2196,27 @@ name_to_backref_number(struct re_registers *regs, VALUE regexp, const char* name
name_to_backref_number((regs), (re), (name_ptr), (name_end)))
static int
-namev_to_backref_number(struct re_registers *regs, VALUE re, VALUE name)
+match_name_to_backref_number(VALUE match, VALUE name)
+{
+ VALUE regexp = RMATCH(match)->regexp;
+ if (NIL_P(regexp)) return -1;
+
+ int *nums;
+ int n = onig_name_to_group_numbers(RREGEXP_PTR(regexp),
+ (const unsigned char *)RSTRING_PTR(name),
+ (const unsigned char *)RSTRING_END(name), &nums);
+ if (n < 0) return n;
+ if (n == 0) return ONIGERR_PARSER_BUG;
+ if (n == 1) return nums[0];
+ for (int i = n - 1; i >= 0; i--) {
+ if (RMATCH_BEG(match, nums[i]) != ONIG_REGION_NOTPOS)
+ return nums[i];
+ }
+ return nums[n - 1];
+}
+
+static int
+namev_to_backref_number(VALUE match, VALUE name)
{
int num;
@@ -2085,8 +2226,14 @@ namev_to_backref_number(struct re_registers *regs, VALUE re, VALUE name)
else if (!RB_TYPE_P(name, T_STRING)) {
return -1;
}
- num = NAME_TO_NUMBER(regs, re, name,
- RSTRING_PTR(name), RSTRING_END(name));
+
+ VALUE re = RMATCH(match)->regexp;
+ if (NIL_P(re) || !rb_enc_compatible(RREGEXP_SRC(re), name)) {
+ num = 0;
+ }
+ else {
+ num = match_name_to_backref_number(match, name);
+ }
if (num < 1) {
name_to_backref_error(name);
}
@@ -2096,7 +2243,7 @@ namev_to_backref_number(struct re_registers *regs, VALUE re, VALUE name)
static VALUE
match_ary_subseq(VALUE match, long beg, long len, VALUE result)
{
- long olen = RMATCH_REGS(match)->num_regs;
+ long olen = RMATCH_NREGS(match);
long j, end = olen < beg+len ? olen : beg+len;
if (NIL_P(result)) result = rb_ary_new_capa(len);
if (len == 0) return result;
@@ -2114,7 +2261,7 @@ static VALUE
match_ary_aref(VALUE match, VALUE idx, VALUE result)
{
long beg, len;
- int num_regs = RMATCH_REGS(match)->num_regs;
+ int num_regs = RMATCH_NREGS(match);
/* check if idx is Range */
switch (rb_range_beg_len(idx, &beg, &len, (long)num_regs, !NIL_P(result))) {
@@ -2131,12 +2278,12 @@ match_ary_aref(VALUE match, VALUE idx, VALUE result)
/*
* call-seq:
- * matchdata[index] -> string or nil
- * matchdata[start, length] -> array
- * matchdata[range] -> array
- * matchdata[name] -> string or nil
+ * self[offset] -> string or nil
+ * self[offset, size] -> array
+ * self[range] -> array
+ * self[name] -> string or nil
*
- * When arguments +index+, +start and +length+, or +range+ are given,
+ * When arguments +offset+, +offset+ and +size+, or +range+ are given,
* returns match and captures in the style of Array#[]:
*
* m = /(.)(.)(\d+)(\d)/.match("THX1138.")
@@ -2154,6 +2301,17 @@ match_ary_aref(VALUE match, VALUE idx, VALUE result)
* m['foo'] # => "h"
* m[:bar] # => "ge"
*
+ * If multiple captures have the same name, returns the last matched
+ * substring.
+ *
+ * m = /(?<foo>.)(?<foo>.+)/.match("hoge")
+ * # => #<MatchData "hoge" foo:"h" foo:"oge">
+ * m[:foo] #=> "oge"
+ *
+ * m = /\W(?<foo>.+)|\w(?<foo>.+)|(?<foo>.+)/.match("hoge")
+ * #<MatchData "hoge" foo:nil foo:"oge" foo:nil>
+ * m[:foo] #=> "oge"
+ *
*/
static VALUE
@@ -2169,7 +2327,7 @@ match_aref(int argc, VALUE *argv, VALUE match)
return rb_reg_nth_match(FIX2INT(idx), match);
}
else {
- int num = namev_to_backref_number(RMATCH_REGS(match), RMATCH(match)->regexp, idx);
+ int num = namev_to_backref_number(match, idx);
if (num >= 0) {
return rb_reg_nth_match(num, match);
}
@@ -2181,7 +2339,7 @@ match_aref(int argc, VALUE *argv, VALUE match)
else {
long beg = NUM2LONG(idx);
long len = NUM2LONG(length);
- long num_regs = RMATCH_REGS(match)->num_regs;
+ long num_regs = RMATCH_NREGS(match);
if (len < 0) {
return Qnil;
}
@@ -2239,7 +2397,7 @@ match_values_at(int argc, VALUE *argv, VALUE match)
rb_ary_push(result, rb_reg_nth_match(FIX2INT(argv[i]), match));
}
else {
- int num = namev_to_backref_number(RMATCH_REGS(match), RMATCH(match)->regexp, argv[i]);
+ int num = namev_to_backref_number(match, argv[i]);
if (num >= 0) {
rb_ary_push(result, rb_reg_nth_match(num, match));
}
@@ -2279,18 +2437,23 @@ match_to_s(VALUE match)
return str;
}
+struct named_captures_data {
+ VALUE hash;
+ VALUE match;
+ int symbolize;
+};
+
static int
match_named_captures_iter(const OnigUChar *name, const OnigUChar *name_end,
int back_num, int *back_refs, OnigRegex regex, void *arg)
{
- struct MEMO *memo = MEMO_CAST(arg);
- VALUE hash = memo->v1;
- VALUE match = memo->v2;
- long symbolize = memo->u3.state;
+ struct named_captures_data *data = arg;
+ VALUE hash = data->hash;
+ VALUE match = data->match;
VALUE key = rb_enc_str_new((const char *)name, name_end-name, regex->enc);
- if (symbolize > 0) {
+ if (data->symbolize) {
key = rb_str_intern(key);
}
@@ -2316,7 +2479,7 @@ match_named_captures_iter(const OnigUChar *name, const OnigUChar *name_end,
/*
* call-seq:
- * named_captures -> hash
+ * named_captures(symbolize_names: false) -> hash
*
* Returns a hash of the named captures;
* each key is a capture name; each value is its captured string or +nil+:
@@ -2337,22 +2500,47 @@ match_named_captures_iter(const OnigUChar *name, const OnigUChar *name_end,
* # => #<MatchData "01" a:"0" a:"1">
* m.named_captures #=> {"a" => "1"}
*
+ * If keyword argument +symbolize_names+ is given
+ * a true value, the keys in the resulting hash are Symbols:
+ *
+ * m = /(?<a>.)(?<a>.)/.match("01")
+ * # => #<MatchData "01" a:"0" a:"1">
+ * m.named_captures(symbolize_names: true) #=> {:a => "1"}
+ *
*/
static VALUE
-match_named_captures(VALUE match)
+match_named_captures(int argc, VALUE *argv, VALUE match)
{
VALUE hash;
- struct MEMO *memo;
match_check(match);
if (NIL_P(RMATCH(match)->regexp))
return rb_hash_new();
+ VALUE opt;
+ int symbolize_names = 0;
+
+ rb_scan_args(argc, argv, "0:", &opt);
+
+ if (!NIL_P(opt)) {
+ static ID keyword_ids[1];
+
+ VALUE symbolize_names_val;
+
+ if (!keyword_ids[0]) {
+ keyword_ids[0] = rb_intern_const("symbolize_names");
+ }
+ rb_get_kwargs(opt, keyword_ids, 0, 1, &symbolize_names_val);
+ if (!UNDEF_P(symbolize_names_val) && RTEST(symbolize_names_val)) {
+ symbolize_names = 1;
+ }
+ }
+
hash = rb_hash_new();
- memo = MEMO_NEW(hash, match, 0);
+ struct named_captures_data data = { hash, match, symbolize_names };
- onig_foreach_name(RREGEXP(RMATCH(match)->regexp)->ptr, match_named_captures_iter, (void*)memo);
+ onig_foreach_name(RREGEXP(RMATCH(match)->regexp)->ptr, match_named_captures_iter, &data);
return hash;
}
@@ -2367,7 +2555,7 @@ match_named_captures(VALUE match)
* m.deconstruct_keys([:hours, :minutes]) # => {:hours => "18", :minutes => "37"}
* m.deconstruct_keys(nil) # => {:hours => "18", :minutes => "37", :seconds => "22"}
*
- * Returns an empty hash of no named captures were defined:
+ * Returns an empty hash if no named captures were defined:
*
* m = /(\d{2}):(\d{2}):(\d{2})/.match("18:37:22")
* m.deconstruct_keys(nil) # => {}
@@ -2388,10 +2576,9 @@ match_deconstruct_keys(VALUE match, VALUE keys)
if (NIL_P(keys)) {
h = rb_hash_new_with_size(onig_number_of_names(RREGEXP_PTR(RMATCH(match)->regexp)));
- struct MEMO *memo;
- memo = MEMO_NEW(h, match, 1);
+ struct named_captures_data data = { h, match, 1 };
- onig_foreach_name(RREGEXP_PTR(RMATCH(match)->regexp), match_named_captures_iter, (void*)memo);
+ onig_foreach_name(RREGEXP_PTR(RMATCH(match)->regexp), match_named_captures_iter, &data);
return h;
}
@@ -2412,8 +2599,7 @@ match_deconstruct_keys(VALUE match, VALUE keys)
name = rb_sym2str(key);
- int num = NAME_TO_NUMBER(RMATCH_REGS(match), RMATCH(match)->regexp, RMATCH(match)->regexp,
- RSTRING_PTR(name), RSTRING_END(name));
+ int num = match_name_to_backref_number(match, name);
if (num >= 0) {
rb_hash_aset(h, key, rb_reg_nth_match(num, match));
@@ -2466,10 +2652,10 @@ match_inspect_name_iter(const OnigUChar *name, const OnigUChar *name_end,
}
/*
- * call-seq:
- * inspect -> string
+ * call-seq:
+ * inspect -> string
*
- * Returns a string representation of +self+:
+ * Returns a string representation of +self+:
*
* m = /.$/.match("foo")
* # => #<MatchData "o">
@@ -2484,7 +2670,6 @@ match_inspect_name_iter(const OnigUChar *name, const OnigUChar *name_end,
* m.inspect # => "#<MatchData \"fo\" 1:\"f\" 2:nil 3:\"o\">"
*
* Related: MatchData#to_s.
- *
*/
static VALUE
@@ -2493,9 +2678,9 @@ match_inspect(VALUE match)
VALUE cname = rb_class_path(rb_obj_class(match));
VALUE str;
int i;
- struct re_registers *regs = RMATCH_REGS(match);
- int num_regs = regs->num_regs;
+ int num_regs = RMATCH_NREGS(match);
struct backref_name_tag *names;
+ VALUE names_obj = Qnil;
VALUE regexp = RMATCH(match)->regexp;
if (regexp == 0) {
@@ -2506,7 +2691,7 @@ match_inspect(VALUE match)
cname, rb_reg_nth_match(0, match));
}
- names = ALLOCA_N(struct backref_name_tag, num_regs);
+ names = RB_ALLOCV_N(struct backref_name_tag, names_obj, num_regs);
MEMZERO(names, struct backref_name_tag, num_regs);
onig_foreach_name(RREGEXP_PTR(regexp),
@@ -2534,6 +2719,7 @@ match_inspect(VALUE match)
}
rb_str_buf_cat2(str, ">");
+ RB_ALLOCV_END(names_obj);
return str;
}
@@ -2922,7 +3108,11 @@ escape_asis:
case '#':
if (extended_mode && !in_char_class) {
/* consume and ignore comment in extended regexp */
- while ((p < end) && ((c = *p++) != '\n'));
+ while ((p < end) && ((c = *p++) != '\n')) {
+ if ((c & 0x80) && !*encp && enc == rb_utf8_encoding()) {
+ *encp = enc;
+ }
+ }
break;
}
rb_str_buf_cat(buf, (char *)&c, 1);
@@ -2957,6 +3147,9 @@ escape_asis:
switch (c = *p++) {
default:
if (!(c & 0x80)) break;
+ if (!*encp && enc == rb_utf8_encoding()) {
+ *encp = enc;
+ }
--p;
/* fallthrough */
case '\\':
@@ -2990,55 +3183,55 @@ escape_asis:
parens++;
}
- for(s = p+1; s < end; s++) {
+ for (s = p+1; s < end; s++) {
switch(*s) {
- case 'x':
- local_extend = invert ? -1 : 1;
- break;
- case '-':
- invert = 1;
- break;
- case ':':
- case ')':
- if (local_extend == 0 ||
- (local_extend == -1 && !extended_mode) ||
- (local_extend == 1 && extended_mode)) {
- /* no changes to extended flag */
- goto fallthrough;
- }
+ case 'x':
+ local_extend = invert ? -1 : 1;
+ break;
+ case '-':
+ invert = 1;
+ break;
+ case ':':
+ case ')':
+ if (local_extend == 0 ||
+ (local_extend == -1 && !extended_mode) ||
+ (local_extend == 1 && extended_mode)) {
+ /* no changes to extended flag */
+ goto fallthrough;
+ }
- if (*s == ':') {
- /* change extended flag until ')' */
- int local_options = options;
- if (local_extend == 1) {
- local_options |= ONIG_OPTION_EXTEND;
- }
- else {
- local_options &= ~ONIG_OPTION_EXTEND;
- }
-
- rb_str_buf_cat(buf, (char *)&c, 1);
- int ret = unescape_nonascii0(&p, end, enc, buf, encp,
- has_property, err,
- local_options, 1);
- if (ret < 0) return ret;
- goto begin_scan;
+ if (*s == ':') {
+ /* change extended flag until ')' */
+ int local_options = options;
+ if (local_extend == 1) {
+ local_options |= ONIG_OPTION_EXTEND;
}
else {
- /* change extended flag for rest of expression */
- extended_mode = local_extend == 1;
- goto fallthrough;
+ local_options &= ~ONIG_OPTION_EXTEND;
}
- case 'i':
- case 'm':
- case 'a':
- case 'd':
- case 'u':
- /* other option flags, ignored during scanning */
- break;
- default:
- /* other character, no extended flag change*/
+
+ rb_str_buf_cat(buf, (char *)&c, 1);
+ int ret = unescape_nonascii0(&p, end, enc, buf, encp,
+ has_property, err,
+ local_options, 1);
+ if (ret < 0) return ret;
+ goto begin_scan;
+ }
+ else {
+ /* change extended flag for rest of expression */
+ extended_mode = local_extend == 1;
goto fallthrough;
+ }
+ case 'i':
+ case 'm':
+ case 'a':
+ case 'd':
+ case 'u':
+ /* other option flags, ignored during scanning */
+ break;
+ default:
+ /* other character, no extended flag change*/
+ goto fallthrough;
}
}
}
@@ -3145,7 +3338,7 @@ rb_reg_preprocess_dregexp(VALUE ary, int options)
src_enc = rb_enc_get(str);
if (options & ARG_ENCODING_NONE &&
src_enc != ascii8bit) {
- if (str_coderange(str) != ENC_CODERANGE_7BIT)
+ if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT)
rb_raise(rb_eRegexpError, "/.../n has a non escaped non ASCII character in non ASCII-8BIT script");
else
src_enc = ascii8bit;
@@ -3180,6 +3373,15 @@ rb_reg_preprocess_dregexp(VALUE ary, int options)
return result;
}
+static void
+rb_reg_initialize_check(VALUE obj)
+{
+ rb_check_frozen(obj);
+ if (RREGEXP_PTR(obj)) {
+ rb_raise(rb_eTypeError, "already initialized regexp");
+ }
+}
+
static int
rb_reg_initialize(VALUE obj, const char *s, long len, rb_encoding *enc,
int options, onig_errmsg_buffer err,
@@ -3190,10 +3392,7 @@ rb_reg_initialize(VALUE obj, const char *s, long len, rb_encoding *enc,
rb_encoding *fixed_enc = 0;
rb_encoding *a_enc = rb_ascii8bit_encoding();
- rb_check_frozen(obj);
- if (re->ptr)
- rb_raise(rb_eTypeError, "already initialized regexp");
- re->ptr = 0;
+ rb_reg_initialize_check(obj);
if (rb_enc_dummy_p(enc)) {
errcpy(err, "can't make regexp with dummy encoding");
@@ -3231,6 +3430,9 @@ rb_reg_initialize(VALUE obj, const char *s, long len, rb_encoding *enc,
options & ARG_REG_OPTION_MASK, err,
sourcefile, sourceline);
if (!re->ptr) return -1;
+ if (RBASIC_CLASS(obj) == rb_cRegexp) {
+ OBJ_FREEZE(obj);
+ }
RB_GC_GUARD(unescaped);
return 0;
}
@@ -3239,10 +3441,13 @@ static void
reg_set_source(VALUE reg, VALUE str, rb_encoding *enc)
{
rb_encoding *regenc = rb_enc_get(reg);
+
if (regenc != enc) {
- str = rb_enc_associate(rb_str_dup(str), enc = regenc);
+ VALUE dup = rb_str_dup(str);
+ str = rb_enc_associate(dup, enc = regenc);
}
- RB_OBJ_WRITE(reg, &RREGEXP(reg)->src, rb_fstring(str));
+ str = rb_fstring(str);
+ RB_OBJ_WRITE(reg, &RREGEXP(reg)->src, str);
}
static int
@@ -3254,7 +3459,7 @@ rb_reg_initialize_str(VALUE obj, VALUE str, int options, onig_errmsg_buffer err,
if (options & ARG_ENCODING_NONE) {
rb_encoding *ascii8bit = rb_ascii8bit_encoding();
if (enc != ascii8bit) {
- if (str_coderange(str) != ENC_CODERANGE_7BIT) {
+ if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
errcpy(err, "/.../n has a non escaped non ASCII character in non ASCII-8BIT script");
return -1;
}
@@ -3267,10 +3472,10 @@ rb_reg_initialize_str(VALUE obj, VALUE str, int options, onig_errmsg_buffer err,
return ret;
}
-static VALUE
+VALUE
rb_reg_s_alloc(VALUE klass)
{
- NEWOBJ_OF(re, struct RRegexp, klass, T_REGEXP | (RGENGC_WB_PROTECTED_REGEXP ? FL_WB_PROTECTED : 0));
+ NEWOBJ_OF(re, struct RRegexp, klass, T_REGEXP, sizeof(struct RRegexp));
re->ptr = 0;
RB_OBJ_WRITE(re, &re->src, 0);
@@ -3318,11 +3523,12 @@ rb_reg_init_str_enc(VALUE re, VALUE s, rb_encoding *enc, int options)
}
VALUE
-rb_reg_new_ary(VALUE ary, int opt)
+rb_reg_new_from_values(long cnt, const VALUE *elements, int opt)
{
- VALUE re = rb_reg_new_str(rb_reg_preprocess_dregexp(ary, opt), opt);
- rb_obj_freeze(re);
- return re;
+ const VALUE ary = rb_ary_tmp_new_from_values(0, cnt, elements);
+ VALUE val = rb_reg_new_str(rb_reg_preprocess_dregexp(ary, opt), opt);
+ rb_ary_clear(ary);
+ return val;
}
VALUE
@@ -3356,7 +3562,6 @@ rb_reg_compile(VALUE str, int options, const char *sourcefile, int sourceline)
rb_set_errinfo(rb_reg_error_desc(str, options, err));
return Qnil;
}
- rb_obj_freeze(re);
return re;
}
@@ -3365,12 +3570,17 @@ static VALUE reg_cache;
VALUE
rb_reg_regcomp(VALUE str)
{
- if (reg_cache && RREGEXP_SRC_LEN(reg_cache) == RSTRING_LEN(str)
- && ENCODING_GET(reg_cache) == ENCODING_GET(str)
- && memcmp(RREGEXP_SRC_PTR(reg_cache), RSTRING_PTR(str), RSTRING_LEN(str)) == 0)
- return reg_cache;
+ if (rb_ractor_main_p()) {
+ if (reg_cache && RREGEXP_SRC_LEN(reg_cache) == RSTRING_LEN(str)
+ && ENCODING_GET(reg_cache) == ENCODING_GET(str)
+ && memcmp(RREGEXP_SRC_PTR(reg_cache), RSTRING_PTR(str), RSTRING_LEN(str)) == 0)
+ return reg_cache;
- return reg_cache = rb_reg_new_str(str, 0);
+ return reg_cache = rb_reg_new_str(str, 0);
+ }
+ else {
+ return rb_reg_new_str(str, 0);
+ }
}
static st_index_t reg_hash(VALUE re);
@@ -3405,10 +3615,10 @@ reg_hash(VALUE re)
/*
* call-seq:
- * regexp == object -> true or false
+ * self == other -> true or false
*
- * Returns +true+ if +object+ is another \Regexp whose pattern,
- * flags, and encoding are the same as +self+, +false+ otherwise:
+ * Returns whether +other+ is another \Regexp whose pattern,
+ * flags, and encoding are the same as +self+:
*
* /foo/ == Regexp.new('foo') # => true
* /foo/ == /foo/i # => false
@@ -3444,47 +3654,113 @@ rb_reg_equal(VALUE re1, VALUE re2)
static VALUE
match_hash(VALUE match)
{
- const struct re_registers *regs;
st_index_t hashval;
match_check(match);
hashval = rb_hash_start(rb_str_hash(RMATCH(match)->str));
hashval = rb_hash_uint(hashval, reg_hash(match_regexp(match)));
- regs = RMATCH_REGS(match);
- hashval = rb_hash_uint(hashval, regs->num_regs);
- hashval = rb_hash_uint(hashval, rb_memhash(regs->beg, regs->num_regs * sizeof(*regs->beg)));
- hashval = rb_hash_uint(hashval, rb_memhash(regs->end, regs->num_regs * sizeof(*regs->end)));
+ int num_regs = RMATCH_NREGS(match);
+ hashval = rb_hash_uint(hashval, num_regs);
+ hashval = rb_hash_uint(hashval, rb_memhash(RMATCH_BEG_PTR(match), num_regs * sizeof(OnigPosition)));
+ hashval = rb_hash_uint(hashval, rb_memhash(RMATCH_END_PTR(match), num_regs * sizeof(OnigPosition)));
hashval = rb_hash_end(hashval);
return ST2FIX(hashval);
}
/*
* call-seq:
- * matchdata == object -> true or false
+ * self == other -> true or false
*
- * Returns +true+ if +object+ is another \MatchData object
+ * Returns whether +other+ is another \MatchData object
* whose target string, regexp, match, and captures
- * are the same as +self+, +false+ otherwise.
+ * are the same as +self+.
*/
static VALUE
match_equal(VALUE match1, VALUE match2)
{
- const struct re_registers *regs1, *regs2;
-
if (match1 == match2) return Qtrue;
if (!RB_TYPE_P(match2, T_MATCH)) return Qfalse;
if (!RMATCH(match1)->regexp || !RMATCH(match2)->regexp) return Qfalse;
if (!rb_str_equal(RMATCH(match1)->str, RMATCH(match2)->str)) return Qfalse;
if (!rb_reg_equal(match_regexp(match1), match_regexp(match2))) return Qfalse;
- regs1 = RMATCH_REGS(match1);
- regs2 = RMATCH_REGS(match2);
- if (regs1->num_regs != regs2->num_regs) return Qfalse;
- if (memcmp(regs1->beg, regs2->beg, regs1->num_regs * sizeof(*regs1->beg))) return Qfalse;
- if (memcmp(regs1->end, regs2->end, regs1->num_regs * sizeof(*regs1->end))) return Qfalse;
+ int num_regs = RMATCH_NREGS(match1);
+ if (num_regs != RMATCH_NREGS(match2)) return Qfalse;
+ if (memcmp(RMATCH_BEG_PTR(match1), RMATCH_BEG_PTR(match2), num_regs * sizeof(OnigPosition))) return Qfalse;
+ if (memcmp(RMATCH_END_PTR(match1), RMATCH_END_PTR(match2), num_regs * sizeof(OnigPosition))) return Qfalse;
return Qtrue;
}
+/*
+ * call-seq:
+ * integer_at(index, base = 10) -> integer or nil
+ * integer_at(name, base = 10) -> integer or nil
+ *
+ * Converts the matched substring to integer and return the result.
+ * +$~.integer_at(N)+ is equivalent to +$N&.to_i+.
+ *
+ * m = /(\d+{4})(\d+{2})(\d+{2})/.match("20260308")
+ * # => #<MatchData "20260308" 1:"2026" 2:"03" 3:"08">
+ * m.integer_at(0) # => 20260308
+ * m.integer_at(1) # => 2026
+ * m.integer_at(2) # => 3
+ * m.integer_at(3) # => 8
+ *
+ * m = /(?<y>\d+{4})(?<m>\d+{2})(?<d>\d+{2})/.match("20260308")
+ * m.integer_at("y") # => 2026
+ * m.integer_at("m") # => 3
+ * m.integer_at("d") # => 8
+ *
+ * If the substring does not match, returns +nil+.
+ *
+ * re = /(\d+)?/
+ * re.match("123").integer_at(1) #=> 123
+ * re.match("abc").integer_at(1) #=> nil
+ *
+ * The string is converted in decimal by default.
+ *
+ * /\d+/.match("011").integer_at(0) #=> 10
+ * /\d+/.match("011").integer_at(0, 12) #=> 13
+ * /\d+/.match("011").integer_at(0, 0) #=> 9
+ *
+ * See also MatchData#[], String#to_i.
+ */
+static VALUE
+match_integer_at(int argc, VALUE *argv, VALUE match)
+{
+ match_check(match);
+
+ int base = 10;
+ VALUE idx;
+ int nth;
+
+ argc = rb_check_arity(argc, 1, 2);
+ if (FIXNUM_P(idx = argv[0])) {
+ nth = NUM2INT(idx);
+ }
+ else if ((nth = namev_to_backref_number(match, idx)) < 0) {
+ name_to_backref_error(idx);
+ }
+
+ if (argc > 1 && (base = NUM2INT(argv[1])) < 0) {
+ rb_raise(rb_eArgError, "invalid radix %d", base);
+ }
+
+ if (nth >= RMATCH_NREGS(match)) return Qnil;
+ if (nth < 0 && (nth += RMATCH_NREGS(match)) <= 0) return Qnil;
+
+ long start = RMATCH_BEG(match, nth), end = RMATCH_END(match, nth);
+ if (start < 0) return Qnil;
+ RUBY_ASSERT(start <= end, "%ld > %ld", start, end);
+
+ VALUE str = RMATCH(match)->str;
+ RUBY_ASSERT(end <= RSTRING_LEN(str), "%ld > %ld", end, RSTRING_LEN(str));
+
+ char *endp;
+ return rb_int_parse_cstr(RSTRING_PTR(str) + start, end - start, &endp, NULL,
+ base, RB_INT_PARSE_DEFAULT);
+}
+
static VALUE
reg_operand(VALUE s, int check)
{
@@ -3524,12 +3800,11 @@ reg_match_pos(VALUE re, VALUE *strp, long pos, VALUE* set_match)
/*
* call-seq:
- * regexp =~ string -> integer or nil
+ * self =~ other -> integer or nil
*
* Returns the integer index (in characters) of the first match
- * for +self+ and +string+, or +nil+ if none;
- * also sets the
- * {rdoc-ref:Regexp Global Variables}[rdoc-ref:Regexp@Regexp+Global+Variables]:
+ * for +self+ and +other+, or +nil+ if none;
+ * updates {Regexp-related global variables}[rdoc-ref:Regexp@Global+Variables].
*
* /at/ =~ 'input data' # => 7
* $~ # => #<MatchData "at">
@@ -3540,9 +3815,9 @@ reg_match_pos(VALUE re, VALUE *strp, long pos, VALUE* set_match)
* if and only if +self+:
*
* - Is a regexp literal;
- * see {Regexp Literals}[rdoc-ref:literals.rdoc@Regexp+Literals].
+ * see {Regexp Literals}[rdoc-ref:syntax/literals.rdoc@Regexp+Literals].
* - Does not contain interpolations;
- * see {Regexp Interpolation}[rdoc-ref:Regexp@Regexp+Interpolation].
+ * see {Regexp interpolation}[rdoc-ref:Regexp@Interpolation+Mode].
* - Is at the left of the expression.
*
* Example:
@@ -3589,9 +3864,9 @@ rb_reg_match(VALUE re, VALUE str)
/*
* call-seq:
- * regexp === string -> true or false
+ * self === other -> true or false
*
- * Returns +true+ if +self+ finds a match in +string+:
+ * Returns whether +self+ finds a match in +other+:
*
* /^[a-z]*$/ === 'HELLO' # => false
* /^[A-Z]*$/ === 'HELLO' # => true
@@ -3745,12 +4020,6 @@ rb_reg_match_m_p(int argc, VALUE *argv, VALUE re)
VALUE
rb_reg_match_p(VALUE re, VALUE str, long pos)
{
- regex_t *reg;
- onig_errmsg_buffer err = "";
- OnigPosition result;
- const UChar *start, *end;
- int tmpreg;
-
if (NIL_P(str)) return Qfalse;
str = SYMBOL_P(str) ? rb_sym2str(str) : StringValue(str);
if (pos) {
@@ -3765,33 +4034,13 @@ rb_reg_match_p(VALUE re, VALUE str, long pos)
pos = beg - RSTRING_PTR(str);
}
}
- reg = rb_reg_prepare_re0(re, str, err);
- tmpreg = reg != RREGEXP_PTR(re);
- if (!tmpreg) RREGEXP(re)->usecnt++;
- start = ((UChar*)RSTRING_PTR(str));
- end = start + RSTRING_LEN(str);
- result = onig_search(reg, start, end, start + pos, end,
- NULL, ONIG_OPTION_NONE);
- if (!tmpreg) RREGEXP(re)->usecnt--;
- if (tmpreg) {
- if (RREGEXP(re)->usecnt) {
- onig_free(reg);
- }
- else {
- onig_free(RREGEXP_PTR(re));
- RREGEXP_PTR(re) = reg;
- }
- }
- if (result < 0) {
- if (result == ONIG_MISMATCH) {
- return Qfalse;
- }
- else {
- onig_error_code_to_str((UChar*)err, (int)result);
- rb_reg_raise(RREGEXP_SRC_PTR(re), RREGEXP_SRC_LEN(re), err, re);
- }
- }
- return Qtrue;
+
+ struct reg_onig_search_args args = {
+ .pos = pos,
+ .range = RSTRING_LEN(str),
+ };
+
+ return rb_reg_onig_match(re, str, reg_onig_search, &args, NULL) == ONIG_MISMATCH ? Qfalse : Qtrue;
}
/*
@@ -3823,12 +4072,35 @@ static void
set_timeout(rb_hrtime_t *hrt, VALUE timeout)
{
double timeout_d = NIL_P(timeout) ? 0.0 : NUM2DBL(timeout);
- if (!NIL_P(timeout) && timeout_d <= 0) {
+ if (!NIL_P(timeout) && !(timeout_d > 0)) {
rb_raise(rb_eArgError, "invalid timeout: %"PRIsVALUE, timeout);
}
double2hrtime(hrt, timeout_d);
}
+static VALUE
+reg_copy(VALUE copy, VALUE orig)
+{
+ int r;
+ regex_t *re;
+
+ rb_reg_initialize_check(copy);
+ if ((r = onig_reg_copy(&re, RREGEXP_PTR(orig))) != 0) {
+ /* ONIGERR_MEMORY only */
+ rb_raise(rb_eRegexpError, "%s", onig_error_code_to_format(r));
+ }
+ RREGEXP_PTR(copy) = re;
+ RB_OBJ_WRITE(copy, &RREGEXP(copy)->src, RREGEXP(orig)->src);
+ RREGEXP_PTR(copy)->timelimit = RREGEXP_PTR(orig)->timelimit;
+ rb_enc_copy(copy, orig);
+ FL_SET_RAW(copy, FL_TEST_RAW(orig, KCODE_FIXED|REG_ENCODING_NONE));
+ if (RBASIC_CLASS(copy) == rb_cRegexp) {
+ OBJ_FREEZE(copy);
+ }
+
+ return copy;
+}
+
struct reg_init_args {
VALUE str;
VALUE timeout;
@@ -3838,7 +4110,6 @@ struct reg_init_args {
static VALUE reg_extract_args(int argc, VALUE *argv, struct reg_init_args *args);
static VALUE reg_init_args(VALUE self, VALUE str, rb_encoding *enc, int flags);
-void rb_warn_deprecated_to_remove(const char *removal, const char *fmt, const char *suggest, ...);
/*
* call-seq:
@@ -3898,11 +4169,19 @@ static VALUE
rb_reg_initialize_m(int argc, VALUE *argv, VALUE self)
{
struct reg_init_args args;
+ VALUE re = reg_extract_args(argc, argv, &args);
- reg_extract_args(argc, argv, &args);
- reg_init_args(self, args.str, args.enc, args.flags);
+ if (NIL_P(re)) {
+ reg_init_args(self, args.str, args.enc, args.flags);
+ }
+ else {
+ reg_copy(self, re);
+ }
set_timeout(&RREGEXP_PTR(self)->timelimit, args.timeout);
+ if (RBASIC_CLASS(self) == rb_cRegexp) {
+ OBJ_FREEZE(self);
+ }
return self;
}
@@ -4323,11 +4602,11 @@ rb_reg_init_copy(VALUE copy, VALUE re)
{
if (!OBJ_INIT_COPY(copy, re)) return copy;
rb_reg_check(re);
- return rb_reg_init_str(copy, RREGEXP_SRC(re), rb_reg_options(re));
+ return reg_copy(copy, re);
}
-VALUE
-rb_reg_regsub(VALUE str, VALUE src, struct re_registers *regs, VALUE regexp)
+static VALUE
+do_regsub(VALUE str, VALUE src, VALUE regexp, int num_regs, const OnigPosition *beg, const OnigPosition *end)
{
VALUE val = 0;
char *p, *s, *e;
@@ -4394,7 +4673,13 @@ rb_reg_regsub(VALUE str, VALUE src, struct re_registers *regs, VALUE regexp)
if (name_end < e) {
VALUE n = rb_str_subseq(str, (long)(name - RSTRING_PTR(str)),
(long)(name_end - name));
- if ((no = NAME_TO_NUMBER(regs, regexp, n, name, name_end)) < 1) {
+ struct re_registers tmp = {
+ .allocated = num_regs,
+ .num_regs = num_regs,
+ .beg = (OnigPosition *)beg,
+ .end = (OnigPosition *)end,
+ };
+ if ((no = NAME_TO_NUMBER(&tmp, regexp, n, name, name_end)) < 1) {
name_to_backref_error(n);
}
p = s = name_end + clen;
@@ -4414,16 +4699,16 @@ rb_reg_regsub(VALUE str, VALUE src, struct re_registers *regs, VALUE regexp)
break;
case '`':
- rb_enc_str_buf_cat(val, RSTRING_PTR(src), BEG(0), src_enc);
+ rb_enc_str_buf_cat(val, RSTRING_PTR(src), beg[0], src_enc);
continue;
case '\'':
- rb_enc_str_buf_cat(val, RSTRING_PTR(src)+END(0), RSTRING_LEN(src)-END(0), src_enc);
+ rb_enc_str_buf_cat(val, RSTRING_PTR(src)+end[0], RSTRING_LEN(src)-end[0], src_enc);
continue;
case '+':
- no = regs->num_regs-1;
- while (BEG(no) == -1 && no > 0) no--;
+ no = num_regs-1;
+ while (beg[no] == -1 && no > 0) no--;
if (no == 0) continue;
break;
@@ -4437,9 +4722,9 @@ rb_reg_regsub(VALUE str, VALUE src, struct re_registers *regs, VALUE regexp)
}
if (no >= 0) {
- if (no >= regs->num_regs) continue;
- if (BEG(no) == -1) continue;
- rb_enc_str_buf_cat(val, RSTRING_PTR(src)+BEG(no), END(no)-BEG(no), src_enc);
+ if (no >= num_regs) continue;
+ if (beg[no] == -1) continue;
+ rb_enc_str_buf_cat(val, RSTRING_PTR(src)+beg[no], end[no]-beg[no], src_enc);
}
}
@@ -4449,6 +4734,20 @@ rb_reg_regsub(VALUE str, VALUE src, struct re_registers *regs, VALUE regexp)
}
return val;
+#undef ASCGET
+}
+
+VALUE
+rb_reg_regsub(VALUE str, VALUE src, struct re_registers *regs, VALUE regexp)
+{
+ return do_regsub(str, src, regexp, regs->num_regs, regs->beg, regs->end);
+}
+
+VALUE
+rb_reg_regsub_match(VALUE str, VALUE src, VALUE match)
+{
+ return do_regsub(str, src, RMATCH(match)->regexp,
+ RMATCH_NREGS(match), RMATCH_BEG_PTR(match), RMATCH_END_PTR(match));
}
static VALUE
@@ -4495,9 +4794,9 @@ match_setter(VALUE val, ID _x, VALUE *_y)
* Regexp.last_match(n) -> string or nil
* Regexp.last_match(name) -> string or nil
*
- * With no argument, returns the value of <tt>$!</tt>,
+ * With no argument, returns the value of <tt>$~</tt>,
* which is the result of the most recent pattern match
- * (see {Regexp Global Variables}[rdoc-ref:Regexp@Regexp+Global+Variables]):
+ * (see {Regexp global variables}[rdoc-ref:Regexp@Global+Variables]):
*
* /c(.)t/ =~ 'cat' # => 0
* Regexp.last_match # => #<MatchData "cat" 1:"a">
@@ -4546,12 +4845,9 @@ re_warn(const char *s)
rb_warn("%s", s);
}
-// The process-global timeout for regexp matching
-rb_hrtime_t rb_reg_match_time_limit = 0;
-
// This function is periodically called during regexp matching
-void
-rb_reg_check_timeout(regex_t *reg, void *end_time_)
+bool
+rb_reg_timeout_p(regex_t *reg, void *end_time_)
{
rb_hrtime_t *end_time = (rb_hrtime_t *)end_time_;
@@ -4576,10 +4872,12 @@ rb_reg_check_timeout(regex_t *reg, void *end_time_)
}
else {
if (*end_time < rb_hrtime_now()) {
- // timeout is exceeded
- rb_raise(rb_eRegexpTimeoutError, "regexp match timeout");
+ // Timeout has exceeded
+ return true;
}
}
+
+ return false;
}
/*
@@ -4660,7 +4958,7 @@ rb_reg_timeout_get(VALUE re)
/*
* Document-class: Regexp
*
- * :include: doc/regexp.rdoc
+ * :include: doc/_regexp.rdoc
*/
void
@@ -4683,6 +4981,11 @@ Init_Regexp(void)
rb_gvar_ractor_local("$`");
rb_gvar_ractor_local("$'");
rb_gvar_ractor_local("$+");
+ rb_gvar_box_dynamic("$~");
+ rb_gvar_box_ready("$&");
+ rb_gvar_box_ready("$`");
+ rb_gvar_box_ready("$'");
+ rb_gvar_box_ready("$+");
rb_define_virtual_variable("$=", ignorecase_getter, ignorecase_setter);
@@ -4717,6 +5020,7 @@ Init_Regexp(void)
rb_define_method(rb_cRegexp, "named_captures", rb_reg_named_captures, 0);
rb_define_method(rb_cRegexp, "timeout", rb_reg_timeout_get, 0);
+ /* Raised when regexp matching timed out. */
rb_eRegexpTimeoutError = rb_define_class_under(rb_cRegexp, "TimeoutError", rb_eRegexpError);
rb_define_singleton_method(rb_cRegexp, "timeout", rb_reg_s_timeout_get, 0);
rb_define_singleton_method(rb_cRegexp, "timeout=", rb_reg_s_timeout_set, 1);
@@ -4746,6 +5050,8 @@ Init_Regexp(void)
rb_define_method(rb_cMatch, "length", match_size, 0);
rb_define_method(rb_cMatch, "offset", match_offset, 1);
rb_define_method(rb_cMatch, "byteoffset", match_byteoffset, 1);
+ rb_define_method(rb_cMatch, "bytebegin", match_bytebegin, 1);
+ rb_define_method(rb_cMatch, "byteend", match_byteend, 1);
rb_define_method(rb_cMatch, "begin", match_begin, 1);
rb_define_method(rb_cMatch, "end", match_end, 1);
rb_define_method(rb_cMatch, "match", match_nth, 1);
@@ -4754,7 +5060,7 @@ Init_Regexp(void)
rb_define_method(rb_cMatch, "[]", match_aref, -1);
rb_define_method(rb_cMatch, "captures", match_captures, 0);
rb_define_alias(rb_cMatch, "deconstruct", "captures");
- rb_define_method(rb_cMatch, "named_captures", match_named_captures, 0);
+ rb_define_method(rb_cMatch, "named_captures", match_named_captures, -1);
rb_define_method(rb_cMatch, "deconstruct_keys", match_deconstruct_keys, 1);
rb_define_method(rb_cMatch, "values_at", match_values_at, -1);
rb_define_method(rb_cMatch, "pre_match", rb_reg_match_pre, 0);
@@ -4765,4 +5071,5 @@ Init_Regexp(void)
rb_define_method(rb_cMatch, "hash", match_hash, 0);
rb_define_method(rb_cMatch, "eql?", match_equal, 1);
rb_define_method(rb_cMatch, "==", match_equal, 1);
+ rb_define_method(rb_cMatch, "integer_at", match_integer_at, -1);
}