summaryrefslogtreecommitdiff
path: root/re.c
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2023-06-27 23:14:10 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2023-06-27 23:14:10 +0900
commit913e01e80ec5fd771ae096c992893d5e736e7fae (patch)
treed32c73f3a8cd5aa31b0bfbb0c8ce302f537c03dd /re.c
parentdf5ae0a55005f7f02adbf21790300f28dc2e12fc (diff)
Stop allocating unused backref strings at `defined?`
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/7983
Diffstat (limited to 're.c')
-rw-r--r--re.c28
1 files changed, 22 insertions, 6 deletions
diff --git a/re.c b/re.c
index 4728fca654..0937d6afac 100644
--- a/re.c
+++ b/re.c
@@ -1935,21 +1935,37 @@ rb_reg_match_post(VALUE match)
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 (BEG(0) == -1) return -1;
for (i=regs->num_regs-1; BEG(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;
+ struct re_registers *regs = RMATCH_REGS(match);
+ return rb_str_subseq(RMATCH(match)->str, BEG(i), END(i) - BEG(i));
+}
+
+VALUE
+rb_reg_last_defined(VALUE match)
+{
+ int i = match_last_index(match);
+ if (i < 0) return Qnil;
+ return RBOOL(i);
}
static VALUE