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
8 changes: 3 additions & 5 deletions core/src/main/java/org/jruby/RubyEncoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,11 @@ public static Encoding areCompatible(IRubyObject obj1, IRubyObject obj2) {
if (!(obj1 instanceof RubyString) && enc1 instanceof USASCIIEncoding) return enc2;

if (!(obj1 instanceof RubyString)) {
IRubyObject objTmp = obj1; // swap1 obj1 & obj2
IRubyObject objTmp = obj1; // swap obj1 & obj2
obj1 = obj2;
obj2 = objTmp;

Encoding encTmp = enc1; // swap their encodings
enc1 = enc2;
enc2 = encTmp;
// Note: enc1/enc2 are NOT swapped — they retain the
// original argument order, matching CRuby's rb_enc_compatible.
}

if (obj1 instanceof RubyString) {
Expand Down
14 changes: 14 additions & 0 deletions spec/ruby/core/encoding/compatible_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,11 @@
[Encoding, "\x82\xa0".dup.force_encoding("shift_jis"), Encoding::Shift_JIS],
].should be_computed_by(:compatible?, /abc/)
end

it "returns the Regexp's Encoding if the String is ASCII only and the Regexp is not" do
r = Regexp.new("\xa4\xa2".dup.force_encoding("euc-jp"))
Encoding.compatible?("hello".dup.force_encoding("utf-8"), r).should == Encoding::EUC_JP
end
end

describe "Encoding.compatible? String, Symbol" do
Expand Down Expand Up @@ -619,6 +624,15 @@
Encoding.compatible?(/abc/, str).should == Encoding::US_ASCII
end

it "returns the String's Encoding when the String is ASCII only with a different encoding" do
r = Regexp.new("\xa4\xa2".dup.force_encoding("euc-jp"))
Encoding.compatible?(r, "hello".dup.force_encoding("utf-8")).should == Encoding::UTF_8
end

it "returns the Regexp's Encoding if the String has the same non-ASCII encoding" do
r = Regexp.new("\xa4\xa2".dup.force_encoding("euc-jp"))
Encoding.compatible?(r, "hello".dup.force_encoding("euc-jp")).should == Encoding::EUC_JP
end
end

describe "Encoding.compatible? Regexp, Regexp" do
Expand Down