mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
string.c: fix String#{casecmp,casecmp?} for non-string arguments
* string.c: make String#{casecmp,casecmp?} return nil for non-string arguments instead of raising a TypeError. * test/ruby/test_string.rb: add tests. Reported by Marcus Stollsteimer. Based on a patch by Shingo Morita. [ruby-core:80145] [Bug #13312] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58837 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
913afdf95c
commit
40bc846bf8
2 changed files with 30 additions and 8 deletions
24
string.c
24
string.c
|
@ -3234,16 +3234,21 @@ static VALUE str_casecmp_p(VALUE str1, VALUE str2);
|
|||
* "aBcDeF".casecmp("abcdefg") #=> -1
|
||||
* "abcdef".casecmp("ABCDEF") #=> 0
|
||||
*
|
||||
* +nil+ is returned if the two strings have incompatible encodings.
|
||||
* +nil+ is returned if the two strings have incompatible encodings,
|
||||
* or if +other_str+ is not a string.
|
||||
*
|
||||
* "foo".casecmp(2) #=> nil
|
||||
* "\u{e4 f6 fc}".encode("ISO-8859-1").casecmp("\u{c4 d6 dc}") #=> nil
|
||||
*/
|
||||
|
||||
static VALUE
|
||||
rb_str_casecmp(VALUE str1, VALUE str2)
|
||||
{
|
||||
StringValue(str2);
|
||||
return str_casecmp(str1, str2);
|
||||
VALUE s = rb_check_string_type(str2);
|
||||
if (NIL_P(s)) {
|
||||
return Qnil;
|
||||
}
|
||||
return str_casecmp(str1, s);
|
||||
}
|
||||
|
||||
static VALUE
|
||||
|
@ -3316,16 +3321,21 @@ str_casecmp(VALUE str1, VALUE str2)
|
|||
* "abcdef".casecmp?("ABCDEF") #=> true
|
||||
* "\u{e4 f6 fc}".casecmp?("\u{c4 d6 dc}") #=> true
|
||||
*
|
||||
* +nil+ is returned if the two strings have incompatible encodings.
|
||||
* +nil+ is returned if the two strings have incompatible encodings,
|
||||
* or if +other_str+ is not a string.
|
||||
*
|
||||
* "foo".casecmp?(2) #=> nil
|
||||
* "\u{e4 f6 fc}".encode("ISO-8859-1").casecmp?("\u{c4 d6 dc}") #=> nil
|
||||
*/
|
||||
|
||||
static VALUE
|
||||
rb_str_casecmp_p(VALUE str1, VALUE str2)
|
||||
{
|
||||
StringValue(str2);
|
||||
return str_casecmp_p(str1, str2);
|
||||
VALUE s = rb_check_string_type(str2);
|
||||
if (NIL_P(s)) {
|
||||
return Qnil;
|
||||
}
|
||||
return str_casecmp_p(str1, s);
|
||||
}
|
||||
|
||||
static VALUE
|
||||
|
@ -9939,7 +9949,6 @@ sym_cmp(VALUE sym, VALUE other)
|
|||
|
||||
/*
|
||||
* call-seq:
|
||||
*
|
||||
* sym.casecmp(other_symbol) -> -1, 0, +1, or nil
|
||||
*
|
||||
* Case-insensitive version of <code>Symbol#<=></code>.
|
||||
|
@ -9969,7 +9978,6 @@ sym_casecmp(VALUE sym, VALUE other)
|
|||
|
||||
/*
|
||||
* call-seq:
|
||||
*
|
||||
* sym.casecmp?(other_symbol) -> true, false, or nil
|
||||
*
|
||||
* Returns +true+ if +sym+ and +other_symbol+ are equal after
|
||||
|
|
|
@ -2321,6 +2321,13 @@ CODE
|
|||
assert_equal(1, "FoO".casecmp("BaR"))
|
||||
assert_equal(-1, "baR".casecmp("FoO"))
|
||||
assert_equal(1, "\u3042B".casecmp("\u3042a"))
|
||||
|
||||
assert_nil("foo".casecmp(:foo))
|
||||
assert_nil("foo".casecmp(Object.new))
|
||||
|
||||
o = Object.new
|
||||
def o.to_str; "fOO"; end
|
||||
assert_equal(0, "FoO".casecmp(o))
|
||||
end
|
||||
|
||||
def test_casecmp?
|
||||
|
@ -2328,6 +2335,13 @@ CODE
|
|||
assert_equal(false, 'FoO'.casecmp?('BaR'))
|
||||
assert_equal(false, 'baR'.casecmp?('FoO'))
|
||||
assert_equal(true, 'äöü'.casecmp?('ÄÖÜ'))
|
||||
|
||||
assert_nil("foo".casecmp?(:foo))
|
||||
assert_nil("foo".casecmp?(Object.new))
|
||||
|
||||
o = Object.new
|
||||
def o.to_str; "fOO"; end
|
||||
assert_equal(true, "FoO".casecmp?(o))
|
||||
end
|
||||
|
||||
def test_upcase2
|
||||
|
|
Loading…
Add table
Reference in a new issue