Small improvements.

This commit is contained in:
Martin Edenhofer 2012-12-05 02:34:15 +01:00
parent 5a8f37dc6f
commit db61002b0d

View file

@ -1,15 +1,15 @@
#require 'iconv' #require 'iconv'
class Encode module Encode
def self.conv (charset, string) def self.conv (charset, string)
# return if string is false
return string if !string
# if no charset is given, use LATIN1 as default # if no charset is given, use LATIN1 as default
if !charset || charset == 'US-ASCII' || charset == 'ASCII-8BIT' if !charset || charset == 'US-ASCII' || charset == 'ASCII-8BIT'
charset = 'LATIN1' charset = 'LATIN1'
end end
# return if string is false
return string if !string
# validate already existing utf8 strings # validate already existing utf8 strings
if charset.downcase == 'utf8' || charset.downcase == 'utf-8' if charset.downcase == 'utf8' || charset.downcase == 'utf-8'
begin begin
@ -22,10 +22,16 @@ class Encode
string.encode!( 'UTF-8', 'Windows-1252' ) string.encode!( 'UTF-8', 'Windows-1252' )
rescue EncodingError => e rescue EncodingError => e
puts "Bad encoding: #{new_value.inspect}" puts "Bad encoding: #{string.inspect}"
string.encode!( 'UTF-8', invalid: :replace, undef: :replace, replace: '?' ) string.encode!( 'UTF-8', invalid: :replace, undef: :replace, replace: '?' )
end end
return string return string
end end
# puts '-------' + charset
# puts string
# convert string
string.encode!( 'UTF-8', charset.upcase )
# Iconv.conv( 'UTF8', charset, string )
end end
end end