diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-02-20 20:40:20 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-02-20 20:40:20 +0000 |
commit | 64db238388ecf74d3d2e26524bb5d0f6eb70f152 (patch) | |
tree | 79088735efd0839894fa930ce65f275cd98d6b55 /string.c | |
parent | 697462490a8099a765e34523e30867e4ca65a202 (diff) | |
download | ruby-64db238388ecf74d3d2e26524bb5d0f6eb70f152.tar.gz |
* string.c (rb_str_cmp_m): return nil if str2 does not respond to
both "to_str" and "<=>".
* compar.c (cmp_gt): return nil if "<=>" returns nil (means
incomparable).
* compar.c (cmp_ge): ditto.
* compar.c (cmp_lt): ditto.
* compar.c (cmp_between): use RTEST(), since cmp_lt and cmp_gt may
return nil.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3517 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r-- | string.c | 24 |
1 files changed, 19 insertions, 5 deletions
@@ -804,14 +804,28 @@ static VALUE rb_str_cmp_m(str1, str2) VALUE str1, str2; { - int result; + long result; if (TYPE(str2) != T_STRING) { - str2 = rb_check_string_type(str2); - if (NIL_P(str2)) return Qnil; + if (!rb_respond_to(str2, rb_intern("to_str"))) { + return Qnil; + } + else if (!rb_respond_to(str2, rb_intern("<=>"))) { + return Qnil; + } + else { + VALUE tmp = rb_funcall(str2, rb_intern("<=>"), 1, str1); + + if (!FIXNUM_P(tmp)) { + return rb_funcall(LONG2FIX(0), '-', tmp); + } + result = FIX2LONG(tmp); + } + } + else { + result = rb_str_cmp(str1, str2); } - result = rb_str_cmp(str1, str2); - return INT2FIX(result); + return LONG2FIX(result); } static VALUE |