Skip to content
Snippets Groups Projects
Commit b82f9357 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets
Browse files

Merge branch 'ensure_encoding' into 'master'

Try to ensure that encoding is utf-8

This should resolve the issue of concatenating strings with different encodings which produce the "Encoding::CompatibilityError (incompatible character encodings: UTF-8 and ASCII-8BIT)" error by ensuring that we always attempt to return string with same encoding.
parents 49c39b82 b25f7451
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -14,11 +14,11 @@ module GritExt
 
# if message is utf-8 encoding, just return it
message.force_encoding("UTF-8")
return message if message.valid_encoding?
return ensure_encoding(message) if message.valid_encoding?
 
# return message if message type is binary
detect = CharlockHolmes::EncodingDetector.detect(message)
return message.force_encoding("BINARY") if detect && detect[:type] == :binary
return ensure_encoding(message.force_encoding("BINARY")) if detect && detect[:type] == :binary
 
# encoding message to detect encoding
if detect && detect[:encoding]
Loading
Loading
@@ -26,16 +26,22 @@ module GritExt
end
 
# encode and clean the bad chars
message.replace clean(message)
message = message.replace clean(message)
ensure_encoding(message)
rescue
encoding = detect ? detect[:encoding] : "unknown"
"--broken encoding: #{encoding}"
end
 
private
def clean(message)
message.encode("UTF-16BE", :undef => :replace, :invalid => :replace, :replace => "")
.encode("UTF-8")
.gsub("\0".encode("UTF-8"), "")
end
def ensure_encoding(message)
message.encode('UTF-8', :invalid => :replace, :undef => :replace, :replace => "")
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment