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

ruby-changes:69382

From: Nobuyoshi <ko1@a...>
Date: Sun, 24 Oct 2021 17:51:00 +0900 (JST)
Subject: [ruby-changes:69382] b69c38e62e (master): Refactor associated pointer

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

From b69c38e62e0b7006d2c345a88a400f2fe2cb80b9 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Sat, 23 Oct 2021 01:38:45 +0900
Subject: Refactor associated pointer

---
 pack.c | 68 ++++++++++++++++++++++++------------------------------------------
 1 file changed, 25 insertions(+), 43 deletions(-)

diff --git a/pack.c b/pack.c
index 031071b440..e6a729ab08 100644
--- a/pack.c
+++ b/pack.c
@@ -135,7 +135,23 @@ str_associate(VALUE str, VALUE add) https://github.com/ruby/ruby/blob/trunk/pack.c#L135
 static VALUE
 str_associated(VALUE str)
 {
-    return rb_ivar_lookup(str, id_associated, Qfalse);
+    VALUE associates = rb_ivar_lookup(str, id_associated, Qfalse);
+    if (!associates)
+        rb_raise(rb_eArgError, "no associated pointer");
+    return associates;
+}
+
+static VALUE
+associated_pointer(VALUE associates, const char *t)
+{
+    const VALUE *p = RARRAY_CONST_PTR(associates);
+    const VALUE *pend = p + RARRAY_LEN(associates);
+    for (; p < pend; p++) {
+	VALUE tmp = *p;
+	if (RB_TYPE_P(tmp, T_STRING) && RSTRING_PTR(tmp) == t) return tmp;
+    }
+    rb_raise(rb_eArgError, "non associated pointer");
+    UNREACHABLE_RETURN(Qnil);
 }
 
 static void
@@ -933,7 +949,7 @@ pack_unpack_internal(VALUE str, VALUE fmt, int mode) https://github.com/ruby/ruby/blob/trunk/pack.c#L949
 #define hexdigits ruby_hexdigits
     char *s, *send;
     char *p, *pend;
-    VALUE ary;
+    VALUE ary, associates = Qfalse;
     char type;
     long len;
     AVOID_CC_BUG long tmp_len;
@@ -1540,29 +1556,11 @@ pack_unpack_internal(VALUE str, VALUE fmt, int mode) https://github.com/ruby/ruby/blob/trunk/pack.c#L1556
 
 		UNPACK_FETCH(&t, char *);
 		if (t) {
-		    VALUE a;
-		    const VALUE *p, *pend;
-
-		    if (!(a = str_associated(str))) {
-			rb_raise(rb_eArgError, "no associated pointer");
-		    }
-		    p = RARRAY_CONST_PTR(a);
-		    pend = p + RARRAY_LEN(a);
-		    while (p < pend) {
-			if (RB_TYPE_P(*p, T_STRING) && RSTRING_PTR(*p) == t) {
-			    if (len < RSTRING_LEN(*p)) {
-                                tmp = rb_str_new(t, len);
-				str_associate(tmp, a);
-			    }
-			    else {
-				tmp = *p;
-			    }
-			    break;
-			}
-			p++;
-		    }
-		    if (p == pend) {
-			rb_raise(rb_eArgError, "non associated pointer");
+		    if (!associates) associates = str_associated(str);
+		    tmp = associated_pointer(associates, t);
+		    if (len < RSTRING_LEN(tmp)) {
+			tmp = rb_str_new(t, len);
+			str_associate(tmp, associates);
 		    }
 		}
 		UNPACK_PUSH(tmp);
@@ -1581,24 +1579,8 @@ pack_unpack_internal(VALUE str, VALUE fmt, int mode) https://github.com/ruby/ruby/blob/trunk/pack.c#L1579
 
 		    UNPACK_FETCH(&t, char *);
 		    if (t) {
-			VALUE a;
-			const VALUE *p, *pend;
-
-			if (!(a = str_associated(str))) {
-			    rb_raise(rb_eArgError, "no associated pointer");
-			}
-			p = RARRAY_CONST_PTR(a);
-			pend = p + RARRAY_LEN(a);
-			while (p < pend) {
-			    if (RB_TYPE_P(*p, T_STRING) && RSTRING_PTR(*p) == t) {
-				tmp = *p;
-				break;
-			    }
-			    p++;
-			}
-			if (p == pend) {
-			    rb_raise(rb_eArgError, "non associated pointer");
-			}
+			if (!associates) associates = str_associated(str);
+			tmp = associated_pointer(associates, t);
 		    }
 		    UNPACK_PUSH(tmp);
 		}
-- 
cgit v1.2.1


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

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