From f2ad09d5b7cdea64963433af13a9525bebc19a78 Mon Sep 17 00:00:00 2001 From: matz Date: Mon, 11 Sep 2006 02:48:17 +0000 Subject: * lib/rexml/source.rb (REXML::Source::encoding): should not convert the body twice. [ruby-core:08828] * lib/rexml/encoding.rb (REXML::Encoding::encoding): Encoding#encoding= to return boolean value to tell if the body is really converted or not. * lib/rexml/encoding.rb (REXML::Encoding::encoding): Specific conversion library (e.g. rexml/encodings/UTF-16.rb) to have higher preceding. * lib/rexml/encodings/UTF-16.rb (REXML::Encoding::decode_utf16): UTF-16#decode_utf16 should work strings without BOM. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10910 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/rexml/encoding.rb | 30 ++++++++++++++++-------------- lib/rexml/encodings/UTF-16.rb | 3 ++- lib/rexml/source.rb | 2 +- 3 files changed, 19 insertions(+), 16 deletions(-) (limited to 'lib/rexml') diff --git a/lib/rexml/encoding.rb b/lib/rexml/encoding.rb index 8acc8a3698..51742faf44 100644 --- a/lib/rexml/encoding.rb +++ b/lib/rexml/encoding.rb @@ -24,21 +24,22 @@ module REXML old_verbosity = $VERBOSE begin $VERBOSE = false - return if defined? @encoding and enc == @encoding + enc = enc.nil? ? nil : enc.upcase + return false if defined? @encoding and enc == @encoding if enc and enc != UTF_8 - @encoding = enc.upcase - begin - require 'rexml/encodings/ICONV.rb' - Encoding.apply(self, "ICONV") - rescue LoadError, Exception => err - raise ArgumentError, "Bad encoding name #@encoding" unless @encoding =~ /^[\w-]+$/ - @encoding.untaint - enc_file = File.join( "rexml", "encodings", "#@encoding.rb" ) - begin - require enc_file - Encoding.apply(self, @encoding) - rescue LoadError - puts $!.message + @encoding = enc + raise ArgumentError, "Bad encoding name #@encoding" unless @encoding =~ /^[\w-]+$/ + @encoding.untaint + enc_file = File.join( "rexml", "encodings", "#@encoding.rb" ) + begin + require enc_file + Encoding.apply(self, @encoding) + rescue LoadError, Exception + begin + require 'rexml/encodings/ICONV.rb' + Encoding.apply(self, "ICONV") + rescue LoadError => err + puts err.message raise ArgumentError, "No decoder found for encoding #@encoding. Please install iconv." end end @@ -50,6 +51,7 @@ module REXML ensure $VERBOSE = old_verbosity end + true end def check_encoding str diff --git a/lib/rexml/encodings/UTF-16.rb b/lib/rexml/encodings/UTF-16.rb index 972169755e..792adfd44d 100644 --- a/lib/rexml/encodings/UTF-16.rb +++ b/lib/rexml/encodings/UTF-16.rb @@ -16,9 +16,10 @@ module REXML end def decode_utf16(str) + str = str[2..-1] if /^\376\377/ =~ str array_enc=str.unpack('C*') array_utf8 = [] - 2.step(array_enc.size-1, 2){|i| + 0.step(array_enc.size-1, 2){|i| array_utf8 << (array_enc.at(i+1) + array_enc.at(i)*0x100) } array_utf8.pack('U*') diff --git a/lib/rexml/source.rb b/lib/rexml/source.rb index c51f504811..3b6e813baf 100644 --- a/lib/rexml/source.rb +++ b/lib/rexml/source.rb @@ -44,7 +44,7 @@ module REXML # Inherited from Encoding # Overridden to support optimized en/decoding def encoding=(enc) - super + return unless super @line_break = encode( '>' ) if enc != UTF_8 @buffer = decode(@buffer) -- cgit v1.2.3