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

ruby-changes:39289

From: nobu <ko1@a...>
Date: Sat, 25 Jul 2015 10:49:05 +0900 (JST)
Subject: [ruby-changes:39289] nobu:r51370 (trunk): string.c: cmp orders

nobu	2015-07-25 10:48:47 +0900 (Sat, 25 Jul 2015)

  New Revision: 51370

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

  Log:
    string.c: cmp orders
    
    * string.c (fstring_cmp, rb_str_hash_cmp): compare lengths first,
      then encodings, and contents at last.

  Modified files:
    trunk/string.c
Index: string.c
===================================================================
--- string.c	(revision 51369)
+++ string.c	(revision 51370)
@@ -367,11 +367,13 @@ fstring_set_class_i(st_data_t key, st_da https://github.com/ruby/ruby/blob/trunk/string.c#L367
 static int
 fstring_cmp(VALUE a, VALUE b)
 {
-    int cmp = rb_str_hash_cmp(a, b);
-    if (cmp != 0) {
-	return cmp;
-    }
-    return ENCODING_GET(b) - ENCODING_GET(a);
+    long alen, blen;
+    const char *aptr, *bptr;
+    RSTRING_GETMEM(a, aptr, alen);
+    RSTRING_GETMEM(b, bptr, blen);
+    return (alen != blen ||
+	    ENCODING_GET(a) != ENCODING_GET(b) ||
+	    memcmp(aptr, bptr, alen) != 0);
 }
 
 static inline int
@@ -2576,14 +2578,13 @@ rb_str_hash(VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L2578
 int
 rb_str_hash_cmp(VALUE str1, VALUE str2)
 {
-    long len;
-
-    if (!rb_str_comparable(str1, str2)) return 1;
-    if (RSTRING_LEN(str1) == (len = RSTRING_LEN(str2)) &&
-	memcmp(RSTRING_PTR(str1), RSTRING_PTR(str2), len) == 0) {
-	return 0;
-    }
-    return 1;
+    long len1, len2;
+    const char *ptr1, *ptr2;
+    RSTRING_GETMEM(str1, ptr1, len1);
+    RSTRING_GETMEM(str2, ptr2, len2);
+    return (len1 != len2 ||
+	    !rb_str_comparable(str1, str2) ||
+	    memcmp(ptr1, ptr2, len1) != 0);
 }
 
 /*

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

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