Skip to content

Commit 12bb895

Browse files
committed
Introduce RMATCH_{BEG,END}_PTR
1 parent ee19cef commit 12bb895

2 files changed

Lines changed: 22 additions & 14 deletions

File tree

internal/re.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,28 @@
1212
#include "ruby/ruby.h" /* for VALUE */
1313
#include "ruby/re.h" /* for struct RMatch and struct re_registers */
1414

15+
static inline OnigPosition *
16+
RMATCH_BEG_PTR(VALUE match)
17+
{
18+
return RMATCH(match)->regs.beg;
19+
}
20+
21+
static inline OnigPosition *
22+
RMATCH_END_PTR(VALUE match)
23+
{
24+
return RMATCH(match)->regs.end;
25+
}
26+
1527
static inline long
1628
RMATCH_BEG(VALUE match, int i)
1729
{
18-
return RMATCH(match)->regs.beg[i];
30+
return RMATCH_BEG_PTR(match)[i];
1931
}
2032

2133
static inline long
2234
RMATCH_END(VALUE match, int i)
2335
{
24-
return RMATCH(match)->regs.end[i];
36+
return RMATCH_END_PTR(match)[i];
2537
}
2638

2739
static inline int

re.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3553,16 +3553,15 @@ rb_reg_equal(VALUE re1, VALUE re2)
35533553
static VALUE
35543554
match_hash(VALUE match)
35553555
{
3556-
const struct re_registers *regs;
35573556
st_index_t hashval;
35583557

35593558
match_check(match);
35603559
hashval = rb_hash_start(rb_str_hash(RMATCH(match)->str));
35613560
hashval = rb_hash_uint(hashval, reg_hash(match_regexp(match)));
3562-
regs = RMATCH_REGS(match);
3563-
hashval = rb_hash_uint(hashval, regs->num_regs);
3564-
hashval = rb_hash_uint(hashval, rb_memhash(regs->beg, regs->num_regs * sizeof(*regs->beg)));
3565-
hashval = rb_hash_uint(hashval, rb_memhash(regs->end, regs->num_regs * sizeof(*regs->end)));
3561+
int num_regs = RMATCH_NREGS(match);
3562+
hashval = rb_hash_uint(hashval, num_regs);
3563+
hashval = rb_hash_uint(hashval, rb_memhash(RMATCH_BEG_PTR(match), num_regs * sizeof(OnigPosition)));
3564+
hashval = rb_hash_uint(hashval, rb_memhash(RMATCH_END_PTR(match), num_regs * sizeof(OnigPosition)));
35663565
hashval = rb_hash_end(hashval);
35673566
return ST2FIX(hashval);
35683567
}
@@ -3579,18 +3578,15 @@ match_hash(VALUE match)
35793578
static VALUE
35803579
match_equal(VALUE match1, VALUE match2)
35813580
{
3582-
const struct re_registers *regs1, *regs2;
3583-
35843581
if (match1 == match2) return Qtrue;
35853582
if (!RB_TYPE_P(match2, T_MATCH)) return Qfalse;
35863583
if (!RMATCH(match1)->regexp || !RMATCH(match2)->regexp) return Qfalse;
35873584
if (!rb_str_equal(RMATCH(match1)->str, RMATCH(match2)->str)) return Qfalse;
35883585
if (!rb_reg_equal(match_regexp(match1), match_regexp(match2))) return Qfalse;
3589-
regs1 = RMATCH_REGS(match1);
3590-
regs2 = RMATCH_REGS(match2);
3591-
if (regs1->num_regs != regs2->num_regs) return Qfalse;
3592-
if (memcmp(regs1->beg, regs2->beg, regs1->num_regs * sizeof(*regs1->beg))) return Qfalse;
3593-
if (memcmp(regs1->end, regs2->end, regs1->num_regs * sizeof(*regs1->end))) return Qfalse;
3586+
int num_regs = RMATCH_NREGS(match1);
3587+
if (num_regs != RMATCH_NREGS(match2)) return Qfalse;
3588+
if (memcmp(RMATCH_BEG_PTR(match1), RMATCH_BEG_PTR(match2), num_regs * sizeof(OnigPosition))) return Qfalse;
3589+
if (memcmp(RMATCH_END_PTR(match1), RMATCH_END_PTR(match2), num_regs * sizeof(OnigPosition))) return Qfalse;
35943590
return Qtrue;
35953591
}
35963592

0 commit comments

Comments
 (0)