summaryrefslogtreecommitdiff
path: root/re.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-06-04 12:33:18 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-06-04 12:33:18 (GMT)
commitea0e8e61c06d5b069bf10bfd28be6257c340ae12 (patch)
tree33777d0100b8e65696fa744a8d7eb2003570790e /re.c
parent561fe11035955f0759501442519462b0141f25ed (diff)
re.c: reduce new strings
* re.c (match_aref, rb_reg_regsub): reduce new strings creation for exceptions. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46345 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 're.c')
-rw-r--r--re.c27
1 files changed, 15 insertions, 12 deletions
diff --git a/re.c b/re.c
index dff95b7..4b28553 100644
--- a/re.c
+++ b/re.c
@@ -1733,20 +1733,16 @@ match_captures(VALUE match)
static int
name_to_backref_number(struct re_registers *regs, VALUE regexp, const char* name, const char* name_end)
{
- int num;
-
- num = onig_name_to_backref_number(RREGEXP(regexp)->ptr,
+ return onig_name_to_backref_number(RREGEXP(regexp)->ptr,
(const unsigned char* )name, (const unsigned char* )name_end, regs);
- if (num >= 1) {
- return num;
- }
- else {
- VALUE s = rb_str_new(name, (long )(name_end - name));
- rb_raise(rb_eIndexError, "undefined group name reference: %s",
- StringValuePtr(s));
- }
+}
- UNREACHABLE;
+NORETURN(static void name_to_backref_error(VALUE name));
+static void
+name_to_backref_error(VALUE name)
+{
+ rb_raise(rb_eIndexError, "undefined group name reference: % "PRIsVALUE,
+ name);
}
/*
@@ -1802,6 +1798,9 @@ match_aref(int argc, VALUE *argv, VALUE match)
p = StringValuePtr(idx);
num = name_to_backref_number(RMATCH_REGS(match),
RMATCH(match)->regexp, p, p + RSTRING_LEN(idx));
+ if (num < 1) {
+ name_to_backref_error(idx);
+ }
return rb_reg_nth_match(num, match);
default:
@@ -3419,6 +3418,10 @@ rb_reg_regsub(VALUE str, VALUE src, struct re_registers *regs, VALUE regexp)
}
if (name_end < e) {
no = name_to_backref_number(regs, regexp, name, name_end);
+ if (no < 1) {
+ VALUE n = rb_str_subseq(str, (long)(name - RSTRING_PTR(str)), (long)(name_end - name));
+ name_to_backref_error(n);
+ }
p = s = name_end + clen;
break;
}