[ruby/reline] Fix Reline crash with invalid encoding history

(https://github.com/ruby/reline/pull/751)

e9d4b37e34
This commit is contained in:
tomoya ishida 2024-10-02 02:01:31 +09:00 committed by git
parent ec230ac643
commit e320da6097
5 changed files with 57 additions and 4 deletions

View file

@ -54,6 +54,22 @@ class Reline::Unicode
}.join
end
def self.safe_encode(str, encoding)
# Reline only supports utf-8 convertible string.
converted = str.encode(encoding, invalid: :replace, undef: :replace)
return converted if str.encoding == Encoding::UTF_8 || converted.encoding == Encoding::UTF_8 || converted.ascii_only?
# This code is essentially doing the same thing as
# `str.encode(utf8, **replace_options).encode(encoding, **replace_options)`
# but also avoids unneccesary irreversible encoding conversion.
converted.gsub(/\X/) do |c|
c.encode(Encoding::UTF_8)
c
rescue Encoding::UndefinedConversionError
'?'
end
end
require 'reline/unicode/east_asian_width'
def self.get_mbchar_width(mbchar)