1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* include/ruby/ruby.h (RSTRING_GETMEM): new macro to get ptr and

len at once.

* string.c (rb_str_cmp, str_eql, rb_str_eql): trivial improvements.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@28691 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2010-07-20 09:39:22 +00:00
parent 6de2f601af
commit 06e935a126
3 changed files with 29 additions and 12 deletions

View file

@ -1,3 +1,10 @@
Tue Jul 20 18:39:18 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
* include/ruby/ruby.h (RSTRING_GETMEM): new macro to get ptr and
len at once.
* string.c (rb_str_cmp, str_eql, rb_str_eql): trivial improvements.
Tue Jul 20 18:23:04 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
* ext/fiddle/closure.c (closure_data_type),

View file

@ -647,10 +647,12 @@ struct RString {
#define RSTRING_NOEMBED FL_USER1
#define RSTRING_EMBED_LEN_MASK (FL_USER2|FL_USER3|FL_USER4|FL_USER5|FL_USER6)
#define RSTRING_EMBED_LEN_SHIFT (FL_USHIFT+2)
#define RSTRING_EMBED_LEN(str) \
(long)((RBASIC(str)->flags >> RSTRING_EMBED_LEN_SHIFT) & \
(RSTRING_EMBED_LEN_MASK >> RSTRING_EMBED_LEN_SHIFT))
#define RSTRING_LEN(str) \
(!(RBASIC(str)->flags & RSTRING_NOEMBED) ? \
(long)((RBASIC(str)->flags >> RSTRING_EMBED_LEN_SHIFT) & \
(RSTRING_EMBED_LEN_MASK >> RSTRING_EMBED_LEN_SHIFT)) : \
RSTRING_EMBED_LEN(str) : \
RSTRING(str)->as.heap.len)
#define RSTRING_PTR(str) \
(!(RBASIC(str)->flags & RSTRING_NOEMBED) ? \
@ -658,11 +660,13 @@ struct RString {
RSTRING(str)->as.heap.ptr)
#define RSTRING_END(str) \
(!(RBASIC(str)->flags & RSTRING_NOEMBED) ? \
(RSTRING(str)->as.ary + \
((RBASIC(str)->flags >> RSTRING_EMBED_LEN_SHIFT) & \
(RSTRING_EMBED_LEN_MASK >> RSTRING_EMBED_LEN_SHIFT))) : \
(RSTRING(str)->as.ary + RSTRING_EMBED_LEN(str)) : \
(RSTRING(str)->as.heap.ptr + RSTRING(str)->as.heap.len))
#define RSTRING_LENINT(str) rb_long2int(RSTRING_LEN(str))
#define RSTRING_GETMEM(str, ptrvar, lenvar) \
(!(RBASIC(str)->flags & RSTRING_NOEMBED) ? \
((ptrvar) = RSTRING(str)->as.ary, (lenvar) = RSTRING_EMBED_LEN(str)) : \
((ptrvar) = RSTRING(str)->as.heap.ptr, (lenvar) = RSTRING(str)->as.heap.len))
#define RARRAY_EMBED_LEN_MAX 3
struct RArray {

View file

@ -2100,13 +2100,15 @@ rb_str_comparable(VALUE str1, VALUE str2)
int
rb_str_cmp(VALUE str1, VALUE str2)
{
long len;
long len1, len2;
const char *ptr1, *ptr2;
int retval;
len = lesser(RSTRING_LEN(str1), RSTRING_LEN(str2));
retval = memcmp(RSTRING_PTR(str1), RSTRING_PTR(str2), len);
if (retval == 0) {
if (RSTRING_LEN(str1) == RSTRING_LEN(str2)) {
if (str1 == str2) return 0;
RSTRING_GETMEM(str1, ptr1, len1);
RSTRING_GETMEM(str2, ptr2, len2);
if (ptr1 == ptr2 || (retval = memcmp(ptr1, ptr2, lesser(len1, len2))) == 0) {
if (len1 == len2) {
if (!rb_str_comparable(str1, str2)) {
if (ENCODING_GET(str1) > ENCODING_GET(str2))
return 1;
@ -2114,7 +2116,7 @@ rb_str_cmp(VALUE str1, VALUE str2)
}
return 0;
}
if (RSTRING_LEN(str1) > RSTRING_LEN(str2)) return 1;
if (len1 > len2) return 1;
return -1;
}
if (retval > 0) return 1;
@ -2126,10 +2128,13 @@ static VALUE
str_eql(const VALUE str1, const VALUE str2)
{
const long len = RSTRING_LEN(str1);
const char *ptr1, *ptr2;
if (len != RSTRING_LEN(str2)) return Qfalse;
if (!rb_str_comparable(str1, str2)) return Qfalse;
if (memcmp(RSTRING_PTR(str1), RSTRING_PTR(str2), len) == 0)
if ((ptr1 = RSTRING_PTR(str1)) == (ptr2 = RSTRING_PTR(str2)))
return Qtrue;
if (memcmp(ptr1, ptr2, len) == 0)
return Qtrue;
return Qfalse;
}
@ -2165,6 +2170,7 @@ rb_str_equal(VALUE str1, VALUE str2)
static VALUE
rb_str_eql(VALUE str1, VALUE str2)
{
if (str1 == str2) return Qtrue;
if (TYPE(str2) != T_STRING) return Qfalse;
return str_eql(str1, str2);
}