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

ruby-changes:56678

From: Nobuyoshi <ko1@a...>
Date: Sun, 28 Jul 2019 07:37:37 +0900 (JST)
Subject: [ruby-changes:56678] Nobuyoshi Nakada: 1d1f98d49c (master): Reuse match data

https://git.ruby-lang.org/ruby.git/commit/?id=1d1f98d49c

From 1d1f98d49c9908f4e3928e582d31fd2e9f252f92 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Sun, 28 Jul 2019 07:33:21 +0900
Subject: Reuse match data

* string.c (rb_str_split_m): reuse occupied match data.  [Bug #16024]

diff --git a/internal.h b/internal.h
index 4b4352d..0c6341a 100644
--- a/internal.h
+++ b/internal.h
@@ -2040,6 +2040,7 @@ long rb_reg_search0(VALUE, VALUE, long, int, int); https://github.com/ruby/ruby/blob/trunk/internal.h#L2040
 VALUE rb_reg_match_p(VALUE re, VALUE str, long pos);
 bool rb_reg_start_with_p(VALUE re, VALUE str);
 void rb_backref_set_string(VALUE string, long pos, long len);
+void rb_match_unbusy(VALUE);
 int rb_match_count(VALUE match);
 int rb_match_nth_defined(int nth, VALUE match);
 VALUE rb_reg_new_ary(VALUE ary, int options);
diff --git a/re.c b/re.c
index 969c1ec..6fd4fde 100644
--- a/re.c
+++ b/re.c
@@ -1292,6 +1292,12 @@ rb_match_busy(VALUE match) https://github.com/ruby/ruby/blob/trunk/re.c#L1292
     FL_SET(match, MATCH_BUSY);
 }
 
+void
+rb_match_unbusy(VALUE match)
+{
+    FL_UNSET(match, MATCH_BUSY);
+}
+
 int
 rb_match_count(VALUE match)
 {
diff --git a/string.c b/string.c
index 2805b4d..edcb6aa 100644
--- a/string.c
+++ b/string.c
@@ -8085,9 +8085,11 @@ rb_str_split_m(int argc, VALUE *argv, VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L8085
 	long idx;
 	int last_null = 0;
 	struct re_registers *regs;
+        VALUE match = 0;
 
-	while ((end = rb_reg_search(spat, str, start, 0)) >= 0) {
-            VALUE match = rb_backref_get();
+        for (; (end = rb_reg_search(spat, str, start, 0)) >= 0;
+             (match ? (rb_match_unbusy(match), rb_backref_set(match)) : (void)0)) {
+            match = rb_backref_get();
             if (!result) rb_match_busy(match);
             regs = RMATCH_REGS(match);
 	    if (start == end && BEG(0) == END(0)) {
@@ -8120,6 +8122,7 @@ rb_str_split_m(int argc, VALUE *argv, VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L8122
 	    }
 	    if (!NIL_P(limit) && lim <= ++i) break;
 	}
+        if (match) rb_match_unbusy(match);
     }
     if (RSTRING_LEN(str) > 0 && (!NIL_P(limit) || RSTRING_LEN(str) > beg || lim < 0)) {
 	SPLIT_STR(beg, RSTRING_LEN(str)-beg);
-- 
cgit v0.10.2


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

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