diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-12-09 14:31:47 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-12-09 14:31:47 +0000 |
commit | 996803ead92e3308171a174bdbf5ed36eadb99cc (patch) | |
tree | 4e43019741ce9262036cb42049608af31272f60d /lib/rexml | |
parent | a34a7d43171511601b6dc535a9bedfccc4ccddb3 (diff) | |
download | ruby-996803ead92e3308171a174bdbf5ed36eadb99cc.tar.gz ruby-996803ead92e3308171a174bdbf5ed36eadb99cc.tar.xz ruby-996803ead92e3308171a174bdbf5ed36eadb99cc.zip |
* lib/rexml/encoding.rb (encoding=): give priority to particular
conversion to iconv. [ruby-core:06520]
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@9661 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/rexml')
-rw-r--r-- | lib/rexml/encoding.rb | 30 | ||||
-rw-r--r-- | lib/rexml/encodings/EUC-JP.rb | 29 | ||||
-rw-r--r-- | lib/rexml/encodings/SHIFT-JIS.rb | 29 | ||||
-rw-r--r-- | lib/rexml/encodings/SHIFT_JIS.rb | 2 |
4 files changed, 57 insertions, 33 deletions
diff --git a/lib/rexml/encoding.rb b/lib/rexml/encoding.rb index 3206adcc8..edd3e80df 100644 --- a/lib/rexml/encoding.rb +++ b/lib/rexml/encoding.rb @@ -25,28 +25,22 @@ module REXML begin $VERBOSE = false return if defined? @encoding and enc == @encoding - if enc and enc != UTF_8 - @encoding = enc.upcase + if enc + raise ArgumentError, "Bad encoding name #{enc}" unless /\A[\w-]+\z/n =~ enc + @encoding = enc.upcase.untaint + else + @encoding = UTF_8 + end + err = nil + [@encoding, "ICONV"].each do |enc| begin - require 'rexml/encodings/ICONV.rb' - Encoding.apply(self, "ICONV") + require File.join("rexml", "encodings", "#{enc}.rb") + return Encoding.apply(self, enc) 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 - raise ArgumentError, "No decoder found for encoding #@encoding. Please install iconv." - end end - else - @encoding = UTF_8 - require 'rexml/encodings/UTF-8.rb' - Encoding.apply(self, @encoding) end + puts err.message + raise ArgumentError, "No decoder found for encoding #@encoding. Please install iconv." ensure $VERBOSE = old_verbosity end diff --git a/lib/rexml/encodings/EUC-JP.rb b/lib/rexml/encodings/EUC-JP.rb index 684df0bbd..db37b6bf0 100644 --- a/lib/rexml/encodings/EUC-JP.rb +++ b/lib/rexml/encodings/EUC-JP.rb @@ -1,13 +1,28 @@ -require 'uconv' - module REXML module Encoding - def decode_eucjp(str) - Uconv::euctou8(str) - end + begin + require 'uconv' + + def decode_eucjp(str) + Uconv::euctou8(str) + end + + def encode_eucjp content + Uconv::u8toeuc(content) + end + rescue LoadError + require 'nkf' + + EUCTOU8 = '-Ewm0' + U8TOEUC = '-Wem0' - def encode_eucjp content - Uconv::u8toeuc(content) + def decode_eucjp(str) + NKF.nkf(EUCTOU8, str) + end + + def encode_eucjp content + NKF.nkf(U8TOEUC, content) + end end register("EUC-JP") do |obj| diff --git a/lib/rexml/encodings/SHIFT-JIS.rb b/lib/rexml/encodings/SHIFT-JIS.rb index d055d0c76..93c7877af 100644 --- a/lib/rexml/encodings/SHIFT-JIS.rb +++ b/lib/rexml/encodings/SHIFT-JIS.rb @@ -1,13 +1,28 @@ -require 'uconv' - module REXML module Encoding - def decode_sjis content - Uconv::sjistou8(content) - end + begin + require 'uconv' + + def decode_sjis content + Uconv::sjistou8(content) + end + + def encode_sjis(str) + Uconv::u8tosjis(str) + end + rescue LoadError + require 'nkf' + + SJISTOU8 = '-Swm0' + U8TOSJIS = '-Wsm0' - def encode_sjis(str) - Uconv::u8tosjis(str) + def decode_sjis(str) + NKF.nkf(SJISTOU8, str) + end + + def encode_sjis content + NKF.nkf(U8TOSJIS, content) + end end b = proc do |obj| diff --git a/lib/rexml/encodings/SHIFT_JIS.rb b/lib/rexml/encodings/SHIFT_JIS.rb index 2fc0b2830..e355704a7 100644 --- a/lib/rexml/encodings/SHIFT_JIS.rb +++ b/lib/rexml/encodings/SHIFT_JIS.rb @@ -1 +1 @@ -load 'rexml/encodings/SHIFT-JIS.rb' +require 'rexml/encodings/SHIFT-JIS' |