[前][次][番号順一覧][スレッド一覧]

ruby-changes:34264

From: nobu <ko1@a...>
Date: Wed, 4 Jun 2014 21:33:27 +0900 (JST)
Subject: [ruby-changes:34264] nobu:r46345 (trunk): re.c: reduce new strings

nobu	2014-06-04 21:33:18 +0900 (Wed, 04 Jun 2014)

  New Revision: 46345

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=46345

  Log:
    re.c: reduce new strings
    
    * re.c (match_aref, rb_reg_regsub): reduce new strings creation
      for exceptions.

  Modified files:
    trunk/re.c
Index: re.c
===================================================================
--- re.c	(revision 46344)
+++ re.c	(revision 46345)
@@ -1733,20 +1733,16 @@ match_captures(VALUE match) https://github.com/ruby/ruby/blob/trunk/re.c#L1733
 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 https://github.com/ruby/ruby/blob/trunk/re.c#L1798
 		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, stru https://github.com/ruby/ruby/blob/trunk/re.c#L3418
                 }
                 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;
                 }

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]