lib/sup/util.rb in sup-0.14.0 vs lib/sup/util.rb in sup-0.14.1

- old
+ new

@@ -123,11 +123,11 @@ EXTRACT_FIELD_NAME_RE = /\A([^\x00-\x1f\x7f-\xff :]+):\s*/no class << self def parse(field) field = field.dup.to_s - field = field.fix_encoding.ascii + field = field.fix_encoding!.ascii if field =~ EXTRACT_FIELD_NAME_RE [ $1, $'.chomp ] else [ "", Field.value_strip(field) ] end @@ -254,11 +254,11 @@ end end class String def display_length - @display_length ||= Unicode.width(self, false) + @display_length ||= Unicode.width(self.fix_encoding!, false) end def slice_by_display_length len each_char.each_with_object "" do |c, buffer| len -= c.display_length @@ -364,11 +364,11 @@ # Fix the damn string! make sure it is valid utf-8, then convert to # user encoding. # # Not Ruby 1.8 compatible - def fix_encoding + def fix_encoding! # first try to encode to utf-8 from whatever current encoding encode!('UTF-8', :invalid => :replace, :undef => :replace) # do this anyway in case string is set to be UTF-8, encoding to # something else (UTF-16 which can fully represent UTF-8) and back @@ -400,20 +400,20 @@ encode!(to_encoding, 'UTF-16', :invalid => :replace, :undef => :replace) end rescue Encoding::ConverterNotFoundError debug "Encoding converter not found for #{from_encoding.inspect} or #{to_encoding.inspect}, fixing string: '#{self.to_s}', but expect weird characters." - fix_encoding + fix_encoding! end fail "Could not create valid #{to_encoding.inspect} string out of: '#{self.to_s}'." unless valid_encoding? self end def normalize_whitespace - fix_encoding + fix_encoding! gsub(/\t/, " ").gsub(/\r/, "") end unless method_defined? :ord def ord @@ -451,10 +451,10 @@ out << "\\x#{b.to_s 16}" else out << b.chr end end - out = out.fix_encoding # this should now be an utf-8 string of ascii + out = out.fix_encoding! # this should now be an utf-8 string of ascii # compat chars. end unless method_defined? :ascii_only? def ascii_only?