Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make String#casecmp and casecmp? spec-compliant #2076

Merged
merged 4 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions spec/core/string/casecmp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
end

it "returns nil if incompatible encodings" do
NATFIXME 'returns nil if incompatible encodings', exception: SpecFailedException do
"あれ".casecmp("れ".encode(Encoding::EUC_JP)).should be_nil
end
"あれ".casecmp("れ".encode(Encoding::EUC_JP)).should be_nil
end

describe "in UTF-8 mode" do
Expand Down Expand Up @@ -119,6 +117,11 @@
"B".casecmp(a).should == 1
end
end

it "returns 0 for empty strings in different encodings" do
''.b.casecmp('').should == 0
''.b.casecmp(''.encode("UTF-32LE")).should == 0
end
end

describe 'String#casecmp? independent of case' do
Expand All @@ -140,9 +143,7 @@
end

it "returns nil if incompatible encodings" do
NATFIXME 'returns nil if incompatible encodings', exception: SpecFailedException do
"あれ".casecmp?("れ".encode(Encoding::EUC_JP)).should be_nil
end
"あれ".casecmp?("れ".encode(Encoding::EUC_JP)).should be_nil
end

describe 'for UNICODE characters' do
Expand Down Expand Up @@ -189,12 +190,15 @@
end

it "case folds" do
NATFIXME 'Pending unicode casemap support', exception: SpecFailedException do
"ß".casecmp?("ss").should be_true
end
"ß".casecmp?("ss").should be_true
end

it "returns nil if other can't be converted to a string" do
"abc".casecmp?(mock('abc')).should be_nil
end

it "returns true for empty strings in different encodings" do
''.b.should.casecmp?('')
''.b.should.casecmp?(''.encode("UTF-32LE"))
end
end
28 changes: 25 additions & 3 deletions src/string_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,9 @@ Value StringObject::cmp(Env *env, Value other) {
return NilObject::the();
}

if (m_string.is_empty() && other_str->m_string.is_empty())
return Value::integer(0);

auto comparison = m_string.cmp(other_str->m_string);

if (comparison == 0 && !(is_ascii_only() && other->as_string()->is_ascii_only())) {
Expand Down Expand Up @@ -2798,19 +2801,38 @@ Value StringObject::casecmp(Env *env, Value other) {
other = StringObject::try_convert(env, other);
if (other->is_nil())
return NilObject::the();

auto other_str = other->as_string_or_raise(env);

if (is_empty() && other_str->is_empty())
return Value::integer(0);

if (!negotiate_compatible_encoding(other_str))
return NilObject::the();

auto str1 = this->downcase(env, "ascii"_s, nullptr);
auto str2 = other->as_string()->downcase(env, "ascii"_s, nullptr);
auto str2 = other_str->downcase(env, "ascii"_s, nullptr);
return str1->cmp(env, Value(str2));
}

Value StringObject::is_casecmp(Env *env, Value other) {
other = StringObject::try_convert(env, other);
if (other->is_nil())
return NilObject::the();
auto str1 = this->downcase(env, nullptr, nullptr);
auto str2 = other->as_string()->downcase(env, nullptr, nullptr);

auto other_str = other->as_string_or_raise(env);

if (is_empty() && other_str->is_empty())
return Value::integer(0);

if (!negotiate_compatible_encoding(other_str))
return NilObject::the();

auto str1 = this->downcase(env, "fold"_s, nullptr);
auto str2 = other_str->downcase(env, "fold"_s, nullptr);
if (str1->string() == str2->string())
return TrueObject::the();

return FalseObject::the();
}

Expand Down
Loading