mirror of
https://github.com/ruby/ruby.git
synced 2025-08-27 06:56:13 +02:00
[ruby/reline] Fix Reline crash with invalid encoding history
(https://github.com/ruby/reline/pull/751)
e9d4b37e34
This commit is contained in:
parent
ec230ac643
commit
e320da6097
5 changed files with 57 additions and 4 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue