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

ruby-changes:59681

From: Nobuyoshi <ko1@a...>
Date: Sat, 11 Jan 2020 20:20:44 +0900 (JST)
Subject: [ruby-changes:59681] 012f297311 (master): Get rid of use of magic number 'E'

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

From 012f297311817ecb19f78c55854b033bb4b0397c Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Sat, 11 Jan 2020 16:32:56 +0900
Subject: Get rid of use of magic number 'E'


diff --git a/marshal.c b/marshal.c
index 2999027..5326271 100644
--- a/marshal.c
+++ b/marshal.c
@@ -585,7 +585,7 @@ w_obj_each(st_data_t key, st_data_t val, st_data_t a) https://github.com/ruby/ruby/blob/trunk/marshal.c#L585
 
     if (to_be_skipped_id(id)) {
         if (id == s_encoding_short) {
-            rb_warn("instance variable `E' on class %"PRIsVALUE" is not dumped",
+            rb_warn("instance variable `"name_s_encoding_short"' on class %"PRIsVALUE" is not dumped",
                     CLASS_OF(arg->obj));
         }
         return ST_CONTINUE;
@@ -1371,6 +1371,13 @@ r_bytes0(long len, struct load_arg *arg) https://github.com/ruby/ruby/blob/trunk/marshal.c#L1371
     return str;
 }
 
+static inline int
+name_equal(const char *name, size_t nlen, const char *p, long l)
+{
+    if ((size_t)l != nlen || *p != *name) return 0;
+    return nlen == 1 || memcmp(p+1, name+1, nlen-1) == 0;
+}
+
 static int
 sym2encidx(VALUE sym, VALUE val)
 {
@@ -1380,12 +1387,11 @@ sym2encidx(VALUE sym, VALUE val) https://github.com/ruby/ruby/blob/trunk/marshal.c#L1387
     if (rb_enc_get_index(sym) != ENCINDEX_US_ASCII) return -1;
     RSTRING_GETMEM(sym, p, l);
     if (l <= 0) return -1;
-    if (l == sizeof(name_encoding) &&
-	memcmp(p, name_encoding, sizeof(name_encoding)) == 0) {
+    if (name_equal(name_encoding, sizeof(name_encoding), p, l)) {
 	int idx = rb_enc_find_index(StringValueCStr(val));
 	return idx;
     }
-    else if (l == 1 && *p == 'E') {
+    if (name_equal(name_s_encoding_short, rb_strlen_lit(name_s_encoding_short), p, l)) {
 	if (val == Qfalse) return rb_usascii_encindex();
 	else if (val == Qtrue) return rb_utf8_encindex();
 	/* bogus ignore */
-- 
cgit v0.10.2


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

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