Small improvements.
This commit is contained in:
parent
5a8f37dc6f
commit
db61002b0d
1 changed files with 11 additions and 5 deletions
|
@ -1,15 +1,15 @@
|
|||
#require 'iconv'
|
||||
class Encode
|
||||
module Encode
|
||||
def self.conv (charset, string)
|
||||
|
||||
# return if string is false
|
||||
return string if !string
|
||||
|
||||
# if no charset is given, use LATIN1 as default
|
||||
if !charset || charset == 'US-ASCII' || charset == 'ASCII-8BIT'
|
||||
charset = 'LATIN1'
|
||||
end
|
||||
|
||||
# return if string is false
|
||||
return string if !string
|
||||
|
||||
# validate already existing utf8 strings
|
||||
if charset.downcase == 'utf8' || charset.downcase == 'utf-8'
|
||||
begin
|
||||
|
@ -22,10 +22,16 @@ class Encode
|
|||
string.encode!( 'UTF-8', 'Windows-1252' )
|
||||
|
||||
rescue EncodingError => e
|
||||
puts "Bad encoding: #{new_value.inspect}"
|
||||
puts "Bad encoding: #{string.inspect}"
|
||||
string.encode!( 'UTF-8', invalid: :replace, undef: :replace, replace: '?' )
|
||||
end
|
||||
return string
|
||||
end
|
||||
|
||||
# puts '-------' + charset
|
||||
# puts string
|
||||
# convert string
|
||||
string.encode!( 'UTF-8', charset.upcase )
|
||||
# Iconv.conv( 'UTF8', charset, string )
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue