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

ruby-changes:64420

From: Nobuyoshi <ko1@a...>
Date: Mon, 21 Dec 2020 23:46:01 +0900 (JST)
Subject: [ruby-changes:64420] c7a5cc2c30 (master): Replaced magic numbers tr table

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

From c7a5cc2c3098ea74343a0dbab36b3a65bc7a4144 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Mon, 21 Dec 2020 21:46:10 +0900
Subject: Replaced magic numbers tr table


diff --git a/string.c b/string.c
index dfb38e4..3dab48c 100644
--- a/string.c
+++ b/string.c
@@ -7580,13 +7580,14 @@ rb_str_tr(VALUE str, VALUE src, VALUE repl) https://github.com/ruby/ruby/blob/trunk/string.c#L7580
     return str;
 }
 
-#define TR_TABLE_SIZE 257
+#define TR_TABLE_MAX (UCHAR_MAX+1)
+#define TR_TABLE_SIZE (TR_TABLE_MAX+1)
 static void
 tr_setup_table(VALUE str, char stable[TR_TABLE_SIZE], int first,
 	       VALUE *tablep, VALUE *ctablep, rb_encoding *enc)
 {
     const unsigned int errc = -1;
-    char buf[256];
+    char buf[TR_TABLE_MAX];
     struct tr tr;
     unsigned int c;
     VALUE table = 0, ptable = 0;
@@ -7600,26 +7601,26 @@ tr_setup_table(VALUE str, char stable[TR_TABLE_SIZE], int first, https://github.com/ruby/ruby/blob/trunk/string.c#L7601
 	tr.p += l;
     }
     if (first) {
-	for (i=0; i<256; i++) {
+	for (i=0; i<TR_TABLE_MAX; i++) {
 	    stable[i] = 1;
 	}
-	stable[256] = cflag;
+	stable[TR_TABLE_MAX] = cflag;
     }
-    else if (stable[256] && !cflag) {
-	stable[256] = 0;
+    else if (stable[TR_TABLE_MAX] && !cflag) {
+	stable[TR_TABLE_MAX] = 0;
     }
-    for (i=0; i<256; i++) {
+    for (i=0; i<TR_TABLE_MAX; i++) {
 	buf[i] = cflag;
     }
 
     while ((c = trnext(&tr, enc)) != errc) {
-	if (c < 256) {
-	    buf[c & 0xff] = !cflag;
+	if (c < TR_TABLE_MAX) {
+	    buf[(unsigned char)c] = !cflag;
 	}
 	else {
 	    VALUE key = UINT2NUM(c);
 
-	    if (!table && (first || *tablep || stable[256])) {
+	    if (!table && (first || *tablep || stable[TR_TABLE_MAX])) {
 		if (cflag) {
 		    ptable = *ctablep;
 		    table = ptable ? ptable : rb_hash_new();
@@ -7636,7 +7637,7 @@ tr_setup_table(VALUE str, char stable[TR_TABLE_SIZE], int first, https://github.com/ruby/ruby/blob/trunk/string.c#L7637
 	    }
 	}
     }
-    for (i=0; i<256; i++) {
+    for (i=0; i<TR_TABLE_MAX; i++) {
 	stable[i] = stable[i] && buf[i];
     }
     if (!table && !cflag) {
@@ -7648,7 +7649,7 @@ tr_setup_table(VALUE str, char stable[TR_TABLE_SIZE], int first, https://github.com/ruby/ruby/blob/trunk/string.c#L7649
 static int
 tr_find(unsigned int c, const char table[TR_TABLE_SIZE], VALUE del, VALUE nodel)
 {
-    if (c < 256) {
+    if (c < TR_TABLE_MAX) {
 	return table[c] != 0;
     }
     else {
@@ -7663,7 +7664,7 @@ tr_find(unsigned int c, const char table[TR_TABLE_SIZE], VALUE del, VALUE nodel) https://github.com/ruby/ruby/blob/trunk/string.c#L7664
 	else if (nodel && !NIL_P(rb_hash_lookup(nodel, v))) {
 	    return FALSE;
 	}
-	return table[256] ? TRUE : FALSE;
+	return table[TR_TABLE_MAX] ? TRUE : FALSE;
     }
 }
 
@@ -8647,7 +8648,7 @@ rb_str_enumerate_bytes(VALUE str, VALUE ary) https://github.com/ruby/ruby/blob/trunk/string.c#L8648
     long i;
 
     for (i=0; i<RSTRING_LEN(str); i++) {
-	ENUM_ELEM(ary, INT2FIX(RSTRING_PTR(str)[i] & 0xff));
+	ENUM_ELEM(ary, INT2FIX((unsigned char)RSTRING_PTR(str)[i]));
     }
     if (ary)
 	return ary;
-- 
cgit v0.10.2


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

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