summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Hawthorn <john@hawthorn.email>2026-05-08 12:38:55 -0700
committerJohn Hawthorn <john@hawthorn.email>2026-05-08 13:41:12 -0700
commitee19cef31e3c2e38056778103a7f878afe8d99bf (patch)
tree3ef39f126e285e4eaa60c37d082d6705bbe49708
parent3d861274e6388f28a26496c473d605a286e6d3d2 (diff)
Replace BEG/END with RMATCH_BEG/RMATCH_END
-rw-r--r--re.c28
-rw-r--r--string.c31
2 files changed, 26 insertions, 33 deletions
diff --git a/re.c b/re.c
index e4f580ecc0..de46c0e7ca 100644
--- a/re.c
+++ b/re.c
@@ -1004,7 +1004,6 @@ static void
update_char_offset(VALUE match)
{
struct RMatch *rm = RMATCH(match);
- struct re_registers *regs;
int i, num_regs, num_pos;
long c;
char *s, *p, *q;
@@ -1015,8 +1014,7 @@ update_char_offset(VALUE match)
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) {
SIZED_REALLOC_N(rm->char_offset, struct rmatch_offset, num_regs, rm->char_offset_num_allocated);
@@ -1026,8 +1024,8 @@ update_char_offset(VALUE match)
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;
}
@@ -1035,10 +1033,10 @@ update_char_offset(VALUE match)
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);
@@ -1053,17 +1051,17 @@ 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;
}
@@ -3637,7 +3635,7 @@ match_integer_at(int argc, VALUE *argv, VALUE match)
int base = 10;
VALUE idx;
- long nth;
+ int nth;
argc = rb_check_arity(argc, 1, 2);
if (FIXNUM_P(idx = argv[0])) {
@@ -3651,10 +3649,10 @@ match_integer_at(int argc, VALUE *argv, VALUE match)
rb_raise(rb_eArgError, "invalid radix %d", base);
}
- if (nth >= regs->num_regs) return Qnil;
- if (nth < 0 && (nth += regs->num_regs) <= 0) return Qnil;
+ if (nth >= RMATCH_NREGS(match)) return Qnil;
+ if (nth < 0 && (nth += RMATCH_NREGS(match)) <= 0) return Qnil;
- long start = BEG(nth), end = END(nth);
+ long start = RMATCH_BEG(match, nth), end = RMATCH_END(match, nth);
if (start < 0) return Qnil;
RUBY_ASSERT(start <= end, "%ld > %ld", start, end);
diff --git a/string.c b/string.c
index f179b816e8..2d7bd4ee74 100644
--- a/string.c
+++ b/string.c
@@ -60,9 +60,6 @@
# define HAVE_CRYPT_R 1
#endif
-#define BEG(no) (regs->beg[(no)])
-#define END(no) (regs->end[(no)])
-
#undef rb_str_new
#undef rb_usascii_str_new
#undef rb_utf8_str_new
@@ -6273,8 +6270,8 @@ rb_str_sub_bang(int argc, VALUE *argv, VALUE str)
match0 = pat;
}
else {
- beg0 = BEG(0);
- end0 = END(0);
+ beg0 = RMATCH_BEG(match, 0);
+ end0 = RMATCH_END(match, 0);
if (iter) match0 = rb_reg_nth_match(0, match);
}
@@ -6419,8 +6416,8 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang)
match0 = pat;
}
else {
- beg0 = BEG(0);
- end0 = END(0);
+ beg0 = RMATCH_BEG(match, 0);
+ end0 = RMATCH_END(match, 0);
if (mode == ITER) match0 = rb_reg_nth_match(0, match);
}
@@ -10645,17 +10642,14 @@ scan_once(VALUE str, VALUE pat, long *start, int set_backref_str)
VALUE result = Qnil;
long end, pos = rb_pat_search(pat, str, *start, set_backref_str);
if (pos >= 0) {
- VALUE match;
- struct re_registers *regs;
+ VALUE match = Qnil;
if (BUILTIN_TYPE(pat) == T_STRING) {
- regs = NULL;
end = pos + RSTRING_LEN(pat);
}
else {
match = rb_backref_get();
- regs = RMATCH_REGS(match);
- pos = BEG(0);
- end = END(0);
+ pos = RMATCH_BEG(match, 0);
+ end = RMATCH_END(match, 0);
}
if (pos == end) {
@@ -10673,16 +10667,17 @@ scan_once(VALUE str, VALUE pat, long *start, int set_backref_str)
*start = end;
}
- if (!regs || regs->num_regs == 1) {
+ if (NIL_P(match) || RMATCH_NREGS(match) == 1) {
result = rb_str_subseq(str, pos, end - pos);
return result;
}
else {
- result = rb_ary_new2(regs->num_regs);
- for (int i = 1; i < regs->num_regs; i++) {
+ int num_regs = RMATCH_NREGS(match);
+ result = rb_ary_new2(num_regs);
+ for (int i = 1; i < num_regs; i++) {
VALUE s = Qnil;
- if (BEG(i) >= 0) {
- s = rb_str_subseq(str, BEG(i), END(i)-BEG(i));
+ if (RMATCH_BEG(match, i) >= 0) {
+ s = rb_str_subseq(str, RMATCH_BEG(match, i), RMATCH_END(match, i) - RMATCH_BEG(match, i));
}
rb_ary_push(result, s);