diff options
Diffstat (limited to 'lib/rexml/encodings')
-rw-r--r-- | lib/rexml/encodings/EUC-JP.rb | 41 | ||||
-rw-r--r-- | lib/rexml/encodings/ICONV.rb | 18 | ||||
-rw-r--r-- | lib/rexml/encodings/ISO-8859-1.rb | 24 | ||||
-rw-r--r-- | lib/rexml/encodings/SHIFT-JIS.rb | 43 | ||||
-rw-r--r-- | lib/rexml/encodings/UNILE.rb | 13 | ||||
-rw-r--r-- | lib/rexml/encodings/US-ASCII.rb | 13 | ||||
-rw-r--r-- | lib/rexml/encodings/UTF-16.rb | 13 | ||||
-rw-r--r-- | lib/rexml/encodings/UTF-8.rb | 13 |
8 files changed, 77 insertions, 101 deletions
diff --git a/lib/rexml/encodings/EUC-JP.rb b/lib/rexml/encodings/EUC-JP.rb index a1314d0856..684df0bbd6 100644 --- a/lib/rexml/encodings/EUC-JP.rb +++ b/lib/rexml/encodings/EUC-JP.rb @@ -1,37 +1,20 @@ -begin - require 'iconv' +require 'uconv' - module REXML - module Encoding - @@__REXML_encoding_methods =<<-EOL - def decode(str) - return Iconv::iconv("utf-8", "euc-jp", str)[0] - end - - def encode content - return Iconv::iconv("euc-jp", "utf-8", content)[0] - end - EOL +module REXML + module Encoding + def decode_eucjp(str) + Uconv::euctou8(str) end - end -rescue LoadError - begin - require 'uconv' - module REXML - module Encoding - @@__REXML_encoding_methods =<<-EOL - def decode(str) - return Uconv::euctou8(str) - end + def encode_eucjp content + Uconv::u8toeuc(content) + end - def encode content - return Uconv::u8toeuc(content) - end - EOL + register("EUC-JP") do |obj| + class << obj + alias decode decode_eucjp + alias encode encode_eucjp end end - rescue LoadError - raise "uconv or iconv is required for Japanese encoding support." end end diff --git a/lib/rexml/encodings/ICONV.rb b/lib/rexml/encodings/ICONV.rb index 384758d7b2..172fba7cd1 100644 --- a/lib/rexml/encodings/ICONV.rb +++ b/lib/rexml/encodings/ICONV.rb @@ -3,14 +3,20 @@ raise LoadError unless defined? Iconv module REXML module Encoding - @@__REXML_encoding_methods =<<-EOL - def decode( str ) - return Iconv::iconv("utf-8", @encoding, str)[0] + def decode_iconv(str) + Iconv.conv(UTF_8, @encoding, str) end - def encode( content ) - return Iconv::iconv(@encoding, "utf-8", content)[0] + def encode_iconv(content) + Iconv.conv(@encoding, UTF_8, content) + end + + register("ICONV") do |obj| + Iconv.conv(UTF_8, obj.encoding, nil) + class << obj + alias decode decode_iconv + alias encode encode_iconv + end end - EOL end end diff --git a/lib/rexml/encodings/ISO-8859-1.rb b/lib/rexml/encodings/ISO-8859-1.rb index f4e4527c2d..2873d13bf0 100644 --- a/lib/rexml/encodings/ISO-8859-1.rb +++ b/lib/rexml/encodings/ISO-8859-1.rb @@ -1,25 +1,7 @@ +require 'rexml/encodings/US-ASCII' + module REXML module Encoding - @@__REXML_encoding_methods = %q~ - # Convert from UTF-8 - def encode content - array_utf8 = content.unpack('U*') - array_enc = [] - array_utf8.each do |num| - if num <= 0xFF - array_enc << num - else - # Numeric entity (&#nnnn;); shard by Stefan Scholl - array_enc.concat "&\##{num};".unpack('C*') - end - end - array_enc.pack('C*') - end - - # Convert to UTF-8 - def decode(str) - str.unpack('C*').pack('U*') - end - ~ + register("ISO-8859-1", &encoding_method("US-ASCII")) end end diff --git a/lib/rexml/encodings/SHIFT-JIS.rb b/lib/rexml/encodings/SHIFT-JIS.rb index f17c927864..cbbb88e683 100644 --- a/lib/rexml/encodings/SHIFT-JIS.rb +++ b/lib/rexml/encodings/SHIFT-JIS.rb @@ -1,37 +1,22 @@ -begin - require 'iconv' +require 'uconv' - module REXML - module Encoding - @@__REXML_encoding_methods =<<-EOL - def decode(str) - return Iconv::iconv("utf-8", "shift_jis", str)[0] - end - - def encode content - return Iconv::iconv("shift_jis", "utf-8", content)[0] - end - EOL +module REXML + module Encoding + def decode_sjis content + Uconv::u8tosjis(content) end - end -rescue LoadError - begin - require 'uconv' - module REXML - module Encoding - @@__REXML_encoding_methods =<<-EOL - def encode(content) - Uconv::u8tosjis(content) - end + def encode_sjis(str) + Uconv::sjistou8(str) + end - def decode(str) - Uconv::sjistou8(str) - end - EOL + b = proc do |obj| + class << obj + alias decode decode_sjis + alias encode encode_sjis end end - rescue LoadError - raise "uconv or iconv is required for Japanese encoding support." + register("SHIFT-JIS", &b) + register("SHIFT_JIS", &b) end end diff --git a/lib/rexml/encodings/UNILE.rb b/lib/rexml/encodings/UNILE.rb index 95141093b5..0560a08361 100644 --- a/lib/rexml/encodings/UNILE.rb +++ b/lib/rexml/encodings/UNILE.rb @@ -1,7 +1,6 @@ module REXML module Encoding - @@__REXML_encoding_methods =<<-EOL - def encode content + def encode_unile content array_utf8 = content.unpack("U*") array_enc = [] array_utf8.each do |num| @@ -16,7 +15,7 @@ module REXML array_enc.pack('C*') end - def decode(str) + def decode_unile(str) array_enc=str.unpack('C*') array_utf8 = [] 2.step(array_enc.size-1, 2){|i| @@ -24,6 +23,12 @@ module REXML } array_utf8.pack('U*') end - EOL + + register(UNILE) do |obj| + class << obj + alias decode decode_unile + alias encode encode_unile + end + end end end diff --git a/lib/rexml/encodings/US-ASCII.rb b/lib/rexml/encodings/US-ASCII.rb index fe8f6df303..fb4c217074 100644 --- a/lib/rexml/encodings/US-ASCII.rb +++ b/lib/rexml/encodings/US-ASCII.rb @@ -1,8 +1,7 @@ module REXML module Encoding - @@__REXML_encoding_methods = %q~ # Convert from UTF-8 - def encode content + def encode_ascii content array_utf8 = content.unpack('U*') array_enc = [] array_utf8.each do |num| @@ -17,9 +16,15 @@ module REXML end # Convert to UTF-8 - def decode(str) + def decode_ascii(str) str.unpack('C*').pack('U*') end - ~ + + register("US-ASCII") do |obj| + class << obj + alias decode decode_ascii + alias encode encode_ascii + end + end end end diff --git a/lib/rexml/encodings/UTF-16.rb b/lib/rexml/encodings/UTF-16.rb index bd92fce18d..972169755e 100644 --- a/lib/rexml/encodings/UTF-16.rb +++ b/lib/rexml/encodings/UTF-16.rb @@ -1,7 +1,6 @@ module REXML module Encoding - @@__REXML_encoding_methods =<<-EOL - def encode content + def encode_utf16 content array_utf8 = content.unpack("U*") array_enc = [] array_utf8.each do |num| @@ -16,7 +15,7 @@ module REXML array_enc.pack('C*') end - def decode(str) + def decode_utf16(str) array_enc=str.unpack('C*') array_utf8 = [] 2.step(array_enc.size-1, 2){|i| @@ -24,6 +23,12 @@ module REXML } array_utf8.pack('U*') end - EOL + + register(UTF_16) do |obj| + class << obj + alias decode decode_utf16 + alias encode encode_utf16 + end + end end end diff --git a/lib/rexml/encodings/UTF-8.rb b/lib/rexml/encodings/UTF-8.rb index 33a7e490c4..bb08f44100 100644 --- a/lib/rexml/encodings/UTF-8.rb +++ b/lib/rexml/encodings/UTF-8.rb @@ -1,13 +1,18 @@ module REXML module Encoding - @@__REXML_encoding_methods =<<-EOL - def encode content + def encode_utf8 content content end - def decode(str) + def decode_utf8(str) str end - EOL + + register(UTF_8) do |obj| + class << obj + alias decode decode_utf8 + alias encode encode_utf8 + end + end end end |