Module: DR::Encoding
- Defined in:
- lib/dr/base/encoding.rb
Class Method Summary collapse
-
.fix_utf8(s = nil) ⇒ Object
if a mostly utf8 has some mixed in latin1 characters, replace the invalid characters.
- .to_utf8(s = nil, from: nil) ⇒ Object
-
.to_utf8!(s = nil, from: nil) ⇒ Object
assume ruby>=1.9 here.
Class Method Details
.fix_utf8(s = nil) ⇒ Object
if a mostly utf8 has some mixed in latin1 characters, replace the invalid characters
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/dr/base/encoding.rb', line 8 def fix_utf8(s=nil) s=self if s.nil? #if we are included if String.method_defined?(:scrub) #Ruby 2.1 #cf http://ruby-doc.org/core-2.1.0/String.html#method-i-scrub return s.scrub {|bytes| '<'+bytes.unpack('H*')[0]+'>' } else return DR::Encoding.to_utf8(s) end end |
.to_utf8(s = nil, from: nil) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/dr/base/encoding.rb', line 19 def to_utf8(s=nil,from:nil) s=self if s.nil? #if we are included from=s.encoding if from.nil? if String.method_defined?(:encode) #Ruby 1.9 return s.encode('UTF-8',from, :invalid => :replace, :undef => :replace, :fallback => Proc.new { |bytes| '<'+bytes.unpack('H*')[0]+'>' } ) else #Ruby 1.8 ic = Iconv.new(from, 'UTF-8//IGNORE') return ic.iconv(s) end end |
.to_utf8!(s = nil, from: nil) ⇒ Object
assume ruby>=1.9 here
35 36 37 38 39 40 41 |
# File 'lib/dr/base/encoding.rb', line 35 def to_utf8!(s=nil,from:nil) s=self if s.nil? #if we are included from=s.encoding if from.nil? return s.encode!('UTF-8',from, :invalid => :replace, :undef => :replace, :fallback => Proc.new { |bytes| '<'+bytes.unpack('H*')[0]+'>' } ) end |