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

ruby-changes:41460

From: nobu <ko1@a...>
Date: Thu, 14 Jan 2016 17:36:21 +0900 (JST)
Subject: [ruby-changes:41460] nobu:r53534 (trunk): variable.c: matched backrefs only

nobu	2016-01-14 17:36:49 +0900 (Thu, 14 Jan 2016)

  New Revision: 53534

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

  Log:
    variable.c: matched backrefs only
    
    * variable.c (rb_f_global_variables): add matched back references
      only, as well as defiend? operator.

  Modified files:
    trunk/ChangeLog
    trunk/internal.h
    trunk/re.c
    trunk/test/ruby/test_variable.rb
    trunk/variable.c
Index: re.c
===================================================================
--- re.c	(revision 53533)
+++ re.c	(revision 53534)
@@ -1265,6 +1265,33 @@ rb_match_busy(VALUE match) https://github.com/ruby/ruby/blob/trunk/re.c#L1265
     FL_SET(match, MATCH_BUSY);
 }
 
+int
+rb_match_count(VALUE match)
+{
+    struct re_registers *regs;
+    if (NIL_P(match)) return -1;
+    regs = RMATCH_REGS(match);
+    if (!regs) return -1;
+    return regs->num_regs;
+}
+
+int
+rb_match_nth_defined(int nth, VALUE match)
+{
+    struct re_registers *regs;
+    if (NIL_P(match)) return FALSE;
+    regs = RMATCH_REGS(match);
+    if (!regs) return FALSE;
+    if (nth >= regs->num_regs) {
+	return FALSE;
+    }
+    if (nth < 0) {
+	nth += regs->num_regs;
+	if (nth <= 0) return FALSE;
+    }
+    return (BEG(nth) != -1);
+}
+
 static void
 match_set_string(VALUE m, VALUE string, long pos, long len)
 {
Index: internal.h
===================================================================
--- internal.h	(revision 53533)
+++ internal.h	(revision 53534)
@@ -1104,6 +1104,8 @@ VALUE rb_reg_compile(VALUE str, int opti https://github.com/ruby/ruby/blob/trunk/internal.h#L1104
 VALUE rb_reg_check_preprocess(VALUE);
 long rb_reg_search0(VALUE, VALUE, long, int, int);
 void rb_backref_set_string(VALUE string, long pos, long len);
+int rb_match_count(VALUE match);
+int rb_match_nth_defined(int nth, VALUE match);
 
 /* signal.c */
 extern int ruby_enable_coredump;
Index: variable.c
===================================================================
--- variable.c	(revision 53533)
+++ variable.c	(revision 53534)
@@ -880,15 +880,25 @@ VALUE https://github.com/ruby/ruby/blob/trunk/variable.c#L880
 rb_f_global_variables(void)
 {
     VALUE ary = rb_ary_new();
+    VALUE sym, backref = rb_backref_get();
 
     rb_id_table_foreach(rb_global_tbl, gvar_i, (void *)ary);
-    if (!NIL_P(rb_backref_get())) {
+    if (!NIL_P(backref)) {
 	char buf[2];
-	int i;
+	int i, nmatch = rb_match_count(backref);
 	buf[0] = '$';
-	for (i = 1; i <= 9; ++i) {
-	    buf[1] = (char)(i + '0');
-	    rb_ary_push(ary, ID2SYM(rb_intern2(buf, 2)));
+	for (i = 1; i <= nmatch; ++i) {
+	    if (!rb_match_nth_defined(i, backref)) continue;
+	    if (i < 10) {
+		/* probably reused, make static ID */
+		buf[1] = (char)(i + '0');
+		sym = ID2SYM(rb_intern2(buf, 2));
+	    }
+	    else {
+		/* dynamic symbol */
+		sym = rb_str_intern(rb_sprintf("$%d", i));
+	    }
+	    rb_ary_push(ary, sym);
 	}
     }
     return ary;
Index: test/ruby/test_variable.rb
===================================================================
--- test/ruby/test_variable.rb	(revision 53533)
+++ test/ruby/test_variable.rb	(revision 53534)
@@ -105,9 +105,13 @@ class TestVariable < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/ruby/test_variable.rb#L105
     assert_empty(gv.grep(/\A(?!\$)/))
     assert_nil($~)
     assert_not_include(gv, :$1)
-    /.*/ =~ "global"
+    /(\w)(\d)?(.)(.)(.)(.)(.)(.)(.)(.)(\d)?(.)/ =~ "globalglobalglobal"
     assert_not_nil($~)
-    assert_include(global_variables-gv, :$1)
+    gv = global_variables - gv
+    assert_include(gv, :$1)
+    assert_not_include(gv, :$2)
+    assert_not_include(gv, :$11)
+    assert_include(gv, :$12)
   end
 
   def test_global_variable_0
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 53533)
+++ ChangeLog	(revision 53534)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Jan 14 17:36:16 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* variable.c (rb_f_global_variables): add matched back references
+	  only, as well as defiend? operator.
+
 Thu Jan 14 16:12:09 2016  Nobuyoshi Nakada  <nobu@r...>
 
 	* sprintf.c (rb_str_format): format exact number more exactly.

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

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