Skip to content
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
50 changes: 50 additions & 0 deletions bench/bench_string_casecmp_p.rb
Comment thread
headius marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true
require 'benchmark/ips'

# gem install benchmark-ips
# Compare Rubies using benchmark-ips' saved results (one file per case):
# ruby bench/bench_string_casecmp_p.rb baseline /tmp/casecmp
# jruby bench/bench_string_casecmp_p.rb candidate /tmp/casecmp
# Optional: CASE=ascii_equal_16 WARMUP=2 TIME=5
label = ARGV.fetch(0, RUBY_DESCRIPTION)
save_prefix = ARGV[1]
utf8 = Encoding::UTF_8
binary = Encoding::ASCII_8BIT

cases = [
['ascii_equal_16', :warm, 'HelloWorldAbCd00', 'hELLOwORLDaBcD00'],
['ascii_miss_first_256', :warm, 'a' + 'b' * 255, 'z' + 'B' * 255],
['binary_ascii_32', :warm, ('ab' * 16).b, ('AB' * 16).b],
['fresh_utf8_32', :fresh, ('ab' * 16).bytes, ('AB' * 16).bytes],
['unicode_full_fold', :warm, "Stra\u00dfe", 'STRASSE'],
['cold_utf8_short', :cold, "Stra\u00dfe", 'STRASSE'.bytes.pack('C*').force_encoding(utf8)],
['cold_utf8_prefix_4096', :cold, 'a' * 4094 + "\u00c4", 'A' * 4094 + "\u00e4"],
['upstream_1_cold', :cold, [*"\u{e0}".."\u{ff}"].join(''), [*"\u{c0}".."\u{df}"].join('')],
['upstream_100_cold', :cold, [*"\u{e0}".."\u{ff}"].join('') * 100, [*"\u{c0}".."\u{df}"].join('') * 100],
]

cases.each do |name, mode, left, right|
next if ENV['CASE'] && ENV['CASE'] != name
[left, right].each(&:ascii_only?) if mode == :warm
left, right = left.dup, right.dup if mode == :cold
puts "\n#{name}"
Benchmark.ips do |x|
x.config(warmup: Float(ENV.fetch('WARMUP', '2')), time: Float(ENV.fetch('TIME', '5')))
case mode
when :warm
x.report(label) { left.casecmp?(right) }
when :fresh
# Includes constructing new UTF-8 strings with unknown coderanges.
x.report(label) { left.pack('C*').force_encoding(utf8).casecmp?(right.pack('C*').force_encoding(utf8)) }
when :cold
x.report(label) do
# Includes metadata reset; same-encoding force_encoding is a no-op on CRuby.
left.force_encoding(binary).force_encoding(utf8)
right.force_encoding(binary).force_encoding(utf8)
left.casecmp?(right)
end
end
x.save! "#{save_prefix}-#{name}.json" if save_prefix
x.compare!
end
end
5 changes: 5 additions & 0 deletions core/src/main/java/org/jruby/RubyString.java
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,11 @@ public IRubyObject casecmp_p(ThreadContext context, IRubyObject other) {
Encoding enc = StringSupport.areCompatible(this, otherStr);
if (enc == null) return context.nil;

if (isAsciiOnly() && otherStr.isAsciiOnly()) {
return asBoolean(context, value.getRealSize() == otherStr.value.getRealSize()
&& value.caseInsensitiveCmp(otherStr.value) == 0);
}

int flags = Config.CASE_FOLD;
RubyString down = dupString(context, this);
down.downcase_bang(context, flags);
Expand Down
73 changes: 73 additions & 0 deletions spec/ruby/core/string/casecmp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,79 @@
end

describe 'String#casecmp? independent of case' do
it "compares frozen ASCII strings in ASCII-compatible encodings" do
[Encoding::US_ASCII, Encoding::UTF_8, Encoding::ASCII_8BIT].each do |encoding|
'aBc'.encode(encoding).freeze.casecmp?('AbC'.encode(encoding).freeze).should == true
'abc'.encode(encoding).freeze.casecmp?('abx'.encode(encoding).freeze).should == false
end
end

it "compares ASCII strings with different ASCII-compatible encodings" do
'aBc'.b.casecmp?('ABC').should == true
end

it "compares bytes after an embedded NUL" do
"aB\0cD".casecmp?("Ab\0Cd").should == true
"aB\0cD".casecmp?("Ab\0Ce").should == false
end

it "returns false for strings with different byte lengths" do
'prefix'.casecmp?('PREFIX-longer').should == false
'longer-prefix'.casecmp?('LONGER').should == false
end

it "converts the argument before reading the receiver's contents" do
receiver = +'before'
calls = 0
argument = Object.new
argument.define_singleton_method(:to_str) do
calls += 1
receiver.replace('aBc')
'ABC'
end

receiver.casecmp?(argument).should == true
calls.should == 1
receiver.should == 'aBc'
end

it "case folds Unicode characters to ASCII" do
"\u212a".casecmp?('k').should == true
end

it "case folds Latin-1 characters but not binary non-ASCII bytes" do
"\u00c4".encode('ISO-8859-1').casecmp?("\u00e4".encode('ISO-8859-1')).should == true
"\xc4".b.casecmp?("\xe4".b).should == false
end

it "case folds ASCII letters in UTF-16 and UTF-32 encodings" do
[Encoding::UTF_16LE, Encoding::UTF_16BE, Encoding::UTF_32LE, Encoding::UTF_32BE].each do |encoding|
'aBc'.encode(encoding).casecmp?('AbC'.encode(encoding)).should == true
'aBc'.casecmp?('aBc'.encode(encoding)).should == nil
end
end

it "raises ArgumentError for invalid UTF-8" do
-> { "a\xff".b.force_encoding(Encoding::UTF_8).casecmp?('a') }.should.raise(ArgumentError)
end

it "raises Encoding::CompatibilityError for a dummy encoding" do
string = 'aBc'.dup.force_encoding('ISO-2022-JP')
-> { string.casecmp?(string) }.should.raise(Encoding::CompatibilityError)
end

it "compares frozen slices without changing their source strings" do
left = ('prefix' * 128) + ("aBc\0Def" * 64) + ('suffix' * 128)
right = ('before' * 128) + ("AbC\0dEF" * 64) + ('after!' * 128)
originals = [left.dup, right.dup]
left_slice = left.byteslice(768, 448).freeze
right_slice = right.byteslice(768, 448).freeze

left_slice.casecmp?(right_slice).should == true
left_slice.casecmp?(right_slice.byteslice(0, 447)).should == false
[left, right].should == originals
end

it 'returns true when equal to other' do
'abc'.casecmp?('abc').should == true
'abc'.casecmp?('ABC').should == true
Expand Down
Loading