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

ruby-changes:43468

From: nobu <ko1@a...>
Date: Thu, 30 Jun 2016 14:45:39 +0900 (JST)
Subject: [ruby-changes:43468] nobu:r55542 (trunk): Magic numbers

nobu	2016-06-30 14:45:35 +0900 (Thu, 30 Jun 2016)

  New Revision: 55542

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

  Log:
    Magic numbers
    
    * ext/cgi/escape/escape.c (optimized_unescape_html): remove magic
      numbers for literal lengths.

  Modified files:
    trunk/ext/cgi/escape/escape.c
Index: ext/cgi/escape/escape.c
===================================================================
--- ext/cgi/escape/escape.c	(revision 55541)
+++ ext/cgi/escape/escape.c	(revision 55542)
@@ -105,40 +105,38 @@ optimized_unescape_html(VALUE str) https://github.com/ruby/ruby/blob/trunk/ext/cgi/escape/escape.c#L105
 	plen = i - beg;
 	if (++i >= len) break;
 	c = (unsigned char)cstr[i];
+#define MATCH(s) (len - i >= (int)rb_strlen_lit(s) && \
+		  memcmp(&cstr[i], s, rb_strlen_lit(s)) == 0 && \
+		  (i += rb_strlen_lit(s) - 1, 1))
 	switch (c) {
 	  case 'a':
 	    ++i;
-	    if (len - i >= 4 && memcmp(&cstr[i], "pos;", 4) == 0) {
+	    if (MATCH("pos;")) {
 		c = '\'';
-		i += 3;
 	    }
-	    else if (len - i >= 3 && memcmp(&cstr[i], "mp;", 3) == 0) {
+	    else if (MATCH("mp;")) {
 		c = '&';
-		i += 2;
 	    }
 	    else continue;
 	    break;
 	  case 'q':
 	    ++i;
-	    if (len - i >= 4 && memcmp(&cstr[i], "uot;", 4) == 0) {
+	    if (MATCH("uot;")) {
 		c = '"';
-		i += 3;
 	    }
 	    else continue;
 	    break;
 	  case 'g':
 	    ++i;
-	    if (len - i >= 2 && memcmp(&cstr[i], "t;", 2) == 0) {
+	    if (MATCH("t;")) {
 		c = '>';
-		i += 1;
 	    }
 	    else continue;
 	    break;
 	  case 'l':
 	    ++i;
-	    if (len - i >= 2 && memcmp(&cstr[i], "t;", 2) == 0) {
+	    if (MATCH("t;")) {
 		c = '<';
-		i += 1;
 	    }
 	    else continue;
 	    break;

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

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