Skip to content

Commit 166d69a

Browse files
committed
String#end_with? should check the base and search strings have compatible encodings.
1 parent f49bbee commit 166d69a

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

spec/ruby/core/string/end_with_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,11 @@
4747
"céréale".end_with?("réale").should be_true
4848
end
4949

50+
it "raises an Encoding::CompatibilityError if the encodings are incompatible" do
51+
pat = "ア".encode Encoding::EUC_JP
52+
lambda do
53+
"あれ".end_with?(pat)
54+
end.should raise_error(Encoding::CompatibilityError)
55+
end
56+
5057
end

src/main/ruby/core/string.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -531,12 +531,12 @@ def encode(to=undefined, from=undefined, options=undefined)
531531

532532
def end_with?(*suffixes)
533533
suffixes.each do |original_suffix|
534-
suffix = Truffle::Type.rb_check_convert_type original_suffix, String, :to_str
535-
unless suffix
536-
raise TypeError, "no implicit conversion of #{original_suffix.class} into String"
537-
end
534+
suffix = Truffle::Type.rb_convert_type original_suffix, String, :to_str
535+
Truffle::Type.compatible_encoding self, suffix
536+
538537
return true if self[-suffix.length, suffix.length] == suffix
539538
end
539+
540540
false
541541
end
542542

0 commit comments

Comments
 (0)