summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-07-25 01:48:47 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-07-25 01:48:47 +0000
commit502004f617e197bffc3689c8196e23cd31d321d8 (patch)
tree57fe142340deada55b60522d5e106b4df2b53831 /string.c
parent22be6d06ab60ac22fbaa1cf29afa048a456d4319 (diff)
string.c: cmp orders
* string.c (fstring_cmp, rb_str_hash_cmp): compare lengths first, then encodings, and contents at last. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51370 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r--string.c27
1 files changed, 14 insertions, 13 deletions
diff --git a/string.c b/string.c
index 48a9f49521..b8f55b7fac 100644
--- a/string.c
+++ b/string.c
@@ -367,11 +367,13 @@ fstring_set_class_i(st_data_t key, st_data_t val, st_data_t arg)
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)
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);
}
/*