diff options
author | ser <ser@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-06-10 01:31:01 +0000 |
---|---|---|
committer | ser <ser@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-06-10 01:31:01 +0000 |
commit | ea7a527a2ae7024a5cf2885dee8f7a5c21fedd5d (patch) | |
tree | d3e1f95a5acf262a9dd46e9663b7034bb285b406 /lib/rexml/encodings | |
parent | ca02190d8887ecd852e4e3f18f3a3ea91e9c6f7a (diff) | |
download | ruby-ea7a527a2ae7024a5cf2885dee8f7a5c21fedd5d.tar.gz |
Initial revision
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3925 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/rexml/encodings')
-rw-r--r-- | lib/rexml/encodings/EUC-JP.rb | 17 | ||||
-rw-r--r-- | lib/rexml/encodings/EUC-JP_decl.rb | 6 | ||||
-rw-r--r-- | lib/rexml/encodings/ISO-8859-1.rb | 23 | ||||
-rw-r--r-- | lib/rexml/encodings/ISO-8859-1_decl.rb | 6 | ||||
-rw-r--r-- | lib/rexml/encodings/Shift-JIS.rb | 17 | ||||
-rw-r--r-- | lib/rexml/encodings/Shift-JIS_decl.rb | 6 | ||||
-rw-r--r-- | lib/rexml/encodings/Shift_JIS.rb | 17 | ||||
-rw-r--r-- | lib/rexml/encodings/UNILE.rb | 27 | ||||
-rw-r--r-- | lib/rexml/encodings/UNILE_decl.rb | 6 | ||||
-rw-r--r-- | lib/rexml/encodings/US-ASCII.rb | 23 | ||||
-rw-r--r-- | lib/rexml/encodings/US-ASCII_decl.rb | 6 | ||||
-rw-r--r-- | lib/rexml/encodings/UTF-16.rb | 27 | ||||
-rw-r--r-- | lib/rexml/encodings/UTF-16_decl.rb | 6 |
13 files changed, 187 insertions, 0 deletions
diff --git a/lib/rexml/encodings/EUC-JP.rb b/lib/rexml/encodings/EUC-JP.rb new file mode 100644 index 0000000000..cedd6751e7 --- /dev/null +++ b/lib/rexml/encodings/EUC-JP.rb @@ -0,0 +1,17 @@ +begin + require 'uconv' + + module REXML + module Encoding + def from_euc_jp(str) + return Uconv::euctou8(str) + end + + def to_euc_jp content + return Uconv::u8toeuc(content) + end + end + end +rescue LoadError + raise "uconv is required for Japanese encoding support." +end diff --git a/lib/rexml/encodings/EUC-JP_decl.rb b/lib/rexml/encodings/EUC-JP_decl.rb new file mode 100644 index 0000000000..4c7cd828a6 --- /dev/null +++ b/lib/rexml/encodings/EUC-JP_decl.rb @@ -0,0 +1,6 @@ +module REXML + module Encoding + EUC_JP = 'EUC-JP' + claim( EUC_JP ) + end +end diff --git a/lib/rexml/encodings/ISO-8859-1.rb b/lib/rexml/encodings/ISO-8859-1.rb new file mode 100644 index 0000000000..98c5aff3b2 --- /dev/null +++ b/lib/rexml/encodings/ISO-8859-1.rb @@ -0,0 +1,23 @@ +module REXML + module Encoding + # Convert from UTF-8 + def to_iso_8859_1 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 from_iso_8859_1(str) + str.unpack('C*').pack('U*') + end + end +end diff --git a/lib/rexml/encodings/ISO-8859-1_decl.rb b/lib/rexml/encodings/ISO-8859-1_decl.rb new file mode 100644 index 0000000000..a738d30472 --- /dev/null +++ b/lib/rexml/encodings/ISO-8859-1_decl.rb @@ -0,0 +1,6 @@ +module REXML + module Encoding + ISO_8859_1 = 'ISO-8859-1' + claim( ISO_8859_1 ) + end +end diff --git a/lib/rexml/encodings/Shift-JIS.rb b/lib/rexml/encodings/Shift-JIS.rb new file mode 100644 index 0000000000..8650174538 --- /dev/null +++ b/lib/rexml/encodings/Shift-JIS.rb @@ -0,0 +1,17 @@ +begin + require 'uconv' + + module REXML + module Encoding + def to_shift_jis content + Uconv::u8tosjis(content) + end + + def from_shift_jis(str) + Uconv::sjistou8(str) + end + end + end +rescue LoadError + raise "uconv is required for Japanese encoding support." +end diff --git a/lib/rexml/encodings/Shift-JIS_decl.rb b/lib/rexml/encodings/Shift-JIS_decl.rb new file mode 100644 index 0000000000..66f650144a --- /dev/null +++ b/lib/rexml/encodings/Shift-JIS_decl.rb @@ -0,0 +1,6 @@ +module REXML + module Encoding + claim( 'Shift-JIS' ) + claim( 'Shift_JIS' ) + end +end diff --git a/lib/rexml/encodings/Shift_JIS.rb b/lib/rexml/encodings/Shift_JIS.rb new file mode 100644 index 0000000000..8650174538 --- /dev/null +++ b/lib/rexml/encodings/Shift_JIS.rb @@ -0,0 +1,17 @@ +begin + require 'uconv' + + module REXML + module Encoding + def to_shift_jis content + Uconv::u8tosjis(content) + end + + def from_shift_jis(str) + Uconv::sjistou8(str) + end + end + end +rescue LoadError + raise "uconv is required for Japanese encoding support." +end diff --git a/lib/rexml/encodings/UNILE.rb b/lib/rexml/encodings/UNILE.rb new file mode 100644 index 0000000000..74bed14340 --- /dev/null +++ b/lib/rexml/encodings/UNILE.rb @@ -0,0 +1,27 @@ +module REXML + module Encoding + def to_unile content + array_utf8 = content.unpack("U*") + array_enc = [] + array_utf8.each do |num| + if ((num>>16) > 0) + array_enc << ?? + array_enc << 0 + else + array_enc << (num & 0xFF) + array_enc << (num >> 8) + end + end + array_enc.pack('C*') + end + + def from_unile(str) + array_enc=str.unpack('C*') + array_utf8 = [] + 2.step(array_enc.size-1, 2){|i| + array_utf8 << (array_enc.at(i) + array_enc.at(i+1)*0x100) + } + array_utf8.pack('U*') + end + end +end diff --git a/lib/rexml/encodings/UNILE_decl.rb b/lib/rexml/encodings/UNILE_decl.rb new file mode 100644 index 0000000000..9e1c11dc03 --- /dev/null +++ b/lib/rexml/encodings/UNILE_decl.rb @@ -0,0 +1,6 @@ +module REXML + module Encoding + UNILE = 'UNILE' + claim( UNILE, /^\377\376/ ) + end +end diff --git a/lib/rexml/encodings/US-ASCII.rb b/lib/rexml/encodings/US-ASCII.rb new file mode 100644 index 0000000000..4ca2c82a83 --- /dev/null +++ b/lib/rexml/encodings/US-ASCII.rb @@ -0,0 +1,23 @@ +module REXML + module Encoding + # Convert from UTF-8 + def to_us_ascii 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 from_us_ascii(str) + str.unpack('C*').pack('U*') + end + end +end diff --git a/lib/rexml/encodings/US-ASCII_decl.rb b/lib/rexml/encodings/US-ASCII_decl.rb new file mode 100644 index 0000000000..1e69234fff --- /dev/null +++ b/lib/rexml/encodings/US-ASCII_decl.rb @@ -0,0 +1,6 @@ +module REXML + module Encoding + US_ASCII = 'US-ASCII' + claim( US_ASCII ) + end +end diff --git a/lib/rexml/encodings/UTF-16.rb b/lib/rexml/encodings/UTF-16.rb new file mode 100644 index 0000000000..2aeef76a0c --- /dev/null +++ b/lib/rexml/encodings/UTF-16.rb @@ -0,0 +1,27 @@ +module REXML + module Encoding + def to_utf_16 content + array_utf8 = content.unpack("U*") + array_enc = [] + array_utf8.each do |num| + if ((num>>16) > 0) + array_enc << 0 + array_enc << ?? + else + array_enc << (num >> 8) + array_enc << (num & 0xFF) + end + end + array_enc.pack('C*') + end + + def from_utf_16(str) + array_enc=str.unpack('C*') + array_utf8 = [] + 2.step(arrayEnc.size-1, 2){|i| + array_utf8 << (array_enc.at(i+1) + array_enc.at(i)*0x100) + } + array_utf8.pack('U*') + end + end +end diff --git a/lib/rexml/encodings/UTF-16_decl.rb b/lib/rexml/encodings/UTF-16_decl.rb new file mode 100644 index 0000000000..f405a9f259 --- /dev/null +++ b/lib/rexml/encodings/UTF-16_decl.rb @@ -0,0 +1,6 @@ +module REXML + module Encoding + UTF_16 = 'UTF-16' + claim( UTF_16, /^\376\377/ ) + end +end |