Skip to content

Commit d2b8304

Browse files
committed
Fix String#inspect when the string uses a non-UTF-8 ASCII-compatible encoding and has non-ASCII characters.
1 parent 3fe0afa commit d2b8304

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/main/ruby/core/string.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ def inspect
578578
char = chr_at index
579579

580580
if char
581-
index += inspect_char(ascii, unicode, index, char, array)
581+
index += inspect_char(enc, result_encoding, ascii, unicode, index, char, array)
582582
else
583583
array << "\\x#{getbyte(index).to_s(16)}"
584584
index += 1
@@ -595,10 +595,10 @@ def inspect
595595
end
596596

597597
Truffle::Type.infect result, self
598-
result.force_encoding result_encoding
598+
result.encode(result_encoding, encoding)
599599
end
600600

601-
def inspect_char(ascii, unicode, index, char, array)
601+
def inspect_char(enc, result_encoding, ascii, unicode, index, char, array)
602602
consumed = char.bytesize
603603

604604
if (ascii or unicode) and consumed == 1
@@ -648,7 +648,8 @@ def inspect_char(ascii, unicode, index, char, array)
648648
end
649649
end
650650

651-
if Truffle.invoke_primitive(:character_printable_p, char)
651+
if (enc == result_encoding && Truffle.invoke_primitive(:character_printable_p, char)) ||
652+
(ascii && char.ascii_only? && Truffle.invoke_primitive(:character_printable_p, char))
652653
array << char
653654
else
654655
code = char.ord

0 commit comments

Comments
 (0)