5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/plaintext/codeset_util.rb', line 5
def self.to_utf8(str, encoding)
return str if str.nil?
str.force_encoding('ASCII-8BIT')
if str.empty?
str.force_encoding('UTF-8')
return str
end
enc = (encoding.nil? || encoding.size == 0) ? 'UTF-8' : encoding
if enc.upcase != 'UTF-8'
str.force_encoding(enc)
str = str.encode('UTF-8', invalid: :replace,
undef: :replace, replace: '?')
else
str.force_encoding('UTF-8')
if !str.valid_encoding?
str = str.encode('US-ASCII', invalid: :replace,
undef: :replace, replace: '?').encode('UTF-8')
end
end
str
end
|