diff options
-rw-r--r-- | lib/normalize_string.rb | 12 | ||||
-rw-r--r-- | spec/lib/basic_encoding_spec.rb (renamed from spec/lib/basic_encoding_tests.rb) | 20 |
2 files changed, 15 insertions, 17 deletions
diff --git a/lib/normalize_string.rb b/lib/normalize_string.rb index f02b18ee0..3b6116970 100644 --- a/lib/normalize_string.rb +++ b/lib/normalize_string.rb @@ -1,4 +1,4 @@ -require 'iconv' unless RUBY_VERSION.to_f >= 1.9 +require 'iconv' unless String.method_defined?(:encode) require 'charlock_holmes' class EncodingNormalizationError < StandardError @@ -23,17 +23,16 @@ def normalize_string_to_utf8(s, suggested_character_encoding=nil) to_try.push guessed_encoding to_try.each do |from_encoding| - if RUBY_VERSION.to_f >= 1.9 + if String.method_defined?(:encode) begin s.force_encoding from_encoding return s.encode('UTF-8') if s.valid_encoding? - rescue ArgumentError + rescue ArgumentError, Encoding::UndefinedConversionError # We get this is there are invalid bytes when # interpreted as from_encoding at the point of # the encode('UTF-8'); move onto the next one... end else - to_encoding = 'UTF-8' begin converted = Iconv.conv 'UTF-8', from_encoding, s return converted @@ -45,7 +44,6 @@ def normalize_string_to_utf8(s, suggested_character_encoding=nil) end end raise EncodingNormalizationError, "Couldn't find a valid character encoding for the string" - end def convert_string_to_utf8_or_binary(s, suggested_character_encoding=nil) @@ -69,13 +67,13 @@ def convert_string_to_utf8_or_binary(s, suggested_character_encoding=nil) result = normalize_string_to_utf8 s, suggested_character_encoding rescue EncodingNormalizationError result = s - s.force_encoding 'ASCII-8BIT' if RUBY_VERSION.to_f >= 1.9 + s.force_encoding 'ASCII-8BIT' if String.method_defined?(:encode) end result end def log_text_details(message, text) - if RUBY_VERSION.to_f >= 1.9 + if String.method_defined?(:encode) STDERR.puts "#{message}, we have text: #{text}, of class #{text.class} and encoding #{text.encoding}" else STDERR.puts "#{message}, we have text: #{text}, of class #{text.class}" diff --git a/spec/lib/basic_encoding_tests.rb b/spec/lib/basic_encoding_spec.rb index 35d35fd4a..43a65eab9 100644 --- a/spec/lib/basic_encoding_tests.rb +++ b/spec/lib/basic_encoding_spec.rb @@ -4,8 +4,8 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') def bytes_to_binary_string( bytes, claimed_encoding = nil ) claimed_encoding ||= 'ASCII-8BIT' bytes_string = bytes.pack('c*') - if RUBY_VERSION.to_f >= 1.9 - bytes_string.force_encoding! claimed_encoding + if String.method_defined?(:force_encoding) + bytes_string.force_encoding claimed_encoding end bytes_string end @@ -110,15 +110,15 @@ describe "convert_string_to_utf8_or_binary" do converted = convert_string_to_utf8_or_binary random_string converted.should == random_string - if RUBY_VERSION.to_f >= 1.9 - converted.encoding.should == 'ASCII-8BIT' + if String.method_defined?(:encode) + converted.encoding.to_s.should == 'ASCII-8BIT' end converted = convert_string_to_utf8_or_binary random_string,'UTF-8' converted.should == random_string - if RUBY_VERSION.to_f >= 1.9 - converted.encoding.should == 'ASCII-8BIT' + if String.method_defined?(:encode) + converted.encoding.to_s.should == 'ASCII-8BIT' end end @@ -132,8 +132,8 @@ describe "convert_string_to_utf8_or_binary" do converted.should == "DASH – DASH" - if RUBY_VERSION.to_f >= 1.9 - converted.encoding.should == 'UTF-8' + if String.method_defined?(:encode) + converted.encoding.to_s.should == 'UTF-8' end end @@ -147,8 +147,8 @@ describe "convert_string_to_utf8_or_binary" do converted.should start_with("贵公司负责人") - if RUBY_VERSION.to_f >= 1.9 - converted.encoding.should == 'UTF-8' + if String.method_defined?(:encode) + converted.encoding.to_s.should == 'UTF-8' end end |