* lib/rexml/encoding.rb: use Ruby native encoding mechnism. [ruby-dev:42464]
* lib/rexml/encodings/: remove. * lib/rexml/document.rb, lib/rexml/formatters/default.rb, lib/rexml/output.rb, lib/rexml/parseexception.rb, lib/rexml/parsers/baseparser.rb, lib/rexml/source.rb, lib/rexml/xmldecl.rb: use Ruby's native Encoding object. * test/rexml/, test/rss/: follow the above encoding chagnes. * NEWS: add REXML's incompatible change about encoding. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29646 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
767fe5170d
commit
994f066f76
15
ChangeLog
15
ChangeLog
@ -1,3 +1,18 @@
|
||||
Sat Oct 30 21:06:37 2010 Kouhei Sutou <kou@cozmixng.org>
|
||||
|
||||
* lib/rexml/encoding.rb: use Ruby native encoding mechnism.
|
||||
[ruby-dev:42464]
|
||||
* lib/rexml/encodings/: remove.
|
||||
|
||||
* lib/rexml/document.rb, lib/rexml/formatters/default.rb,
|
||||
lib/rexml/output.rb, lib/rexml/parseexception.rb,
|
||||
lib/rexml/parsers/baseparser.rb, lib/rexml/source.rb,
|
||||
lib/rexml/xmldecl.rb: use Ruby's native Encoding object.
|
||||
|
||||
* test/rexml/, test/rss/: follow the above encoding chagnes.
|
||||
|
||||
* NEWS: add REXML's incompatible change about encoding.
|
||||
|
||||
Sat Oct 30 17:23:19 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* util.c (ruby_strtod): get rid of overflow/underflow as possible.
|
||||
|
12
NEWS
12
NEWS
@ -74,6 +74,13 @@ with all sufficient information, see the ChangeLog file.
|
||||
* extended methods:
|
||||
* StringIO#set_encoding can get 2nd argument and optional hash.
|
||||
|
||||
* rexml
|
||||
* [incompatible] support Ruby native encoding mechanism
|
||||
and iconv dependency is droped. This means encoding
|
||||
methods (Document#encoding, XMLDecl#encoding,
|
||||
Output#encoding and Source#encoding) return an Encoding
|
||||
object instead of an encoding name.
|
||||
|
||||
=== Language changes
|
||||
|
||||
* Regexps now support Unicode 6.0 (new characters and scripts)
|
||||
@ -83,3 +90,8 @@ with all sufficient information, see the ChangeLog file.
|
||||
* Kernel#respond_to?
|
||||
|
||||
See above.
|
||||
|
||||
* REXML::Document#encoding, REXML::XMLdecl#encoding,
|
||||
REXML::Output#encoding and REXML::Source#encoding
|
||||
|
||||
Return an Encoding object not encoding name as a String.
|
||||
|
@ -131,7 +131,8 @@ module REXML
|
||||
xml_decl().version
|
||||
end
|
||||
|
||||
# @return the XMLDecl encoding of this document as a String.
|
||||
# @return the XMLDecl encoding of this document as an
|
||||
# Encoding object.
|
||||
# If no XMLDecl has been set, returns the default encoding.
|
||||
def encoding
|
||||
xml_decl().encoding
|
||||
@ -183,7 +184,7 @@ module REXML
|
||||
# that IE's limited abilities can handle. This hack inserts a space
|
||||
# before the /> on empty tags. Defaults to false
|
||||
def write( output=$stdout, indent=-1, transitive=false, ie_hack=false )
|
||||
if xml_decl.encoding != "UTF-8" && !output.kind_of?(Output)
|
||||
if xml_decl.encoding != ::Encoding::UTF_8 && !output.kind_of?(Output)
|
||||
output = Output.new( output, xml_decl.encoding )
|
||||
end
|
||||
formatter = if indent > -1
|
||||
|
@ -1,71 +1,67 @@
|
||||
# -*- mode: ruby; ruby-indent-level: 2; indent-tabs-mode: t; tab-width: 2 -*- vim: sw=2 ts=2
|
||||
module REXML
|
||||
module Encoding
|
||||
@encoding_methods = {}
|
||||
def self.register(enc, &block)
|
||||
@encoding_methods[enc] = block
|
||||
end
|
||||
def self.apply(obj, enc)
|
||||
@encoding_methods[enc][obj]
|
||||
end
|
||||
def self.encoding_method(enc)
|
||||
@encoding_methods[enc]
|
||||
end
|
||||
|
||||
# Native, default format is UTF-8, so it is declared here rather than in
|
||||
# an encodings/ definition.
|
||||
UTF_8 = 'UTF-8'
|
||||
UTF_16 = 'UTF-16'
|
||||
UNILE = 'UNILE'
|
||||
|
||||
# ID ---> Encoding name
|
||||
attr_reader :encoding
|
||||
def encoding=( enc )
|
||||
old_verbosity = $VERBOSE
|
||||
begin
|
||||
$VERBOSE = false
|
||||
enc = enc.nil? ? nil : enc.upcase
|
||||
return false if defined? @encoding and enc == @encoding
|
||||
if enc and enc != UTF_8
|
||||
@encoding = enc
|
||||
raise ArgumentError, "Bad encoding name #@encoding" unless @encoding =~ /^[\w-]+$/
|
||||
@encoding.untaint
|
||||
begin
|
||||
require 'rexml/encodings/ICONV.rb'
|
||||
Encoding.apply(self, "ICONV")
|
||||
rescue LoadError, Exception
|
||||
begin
|
||||
enc_file = File.join( "rexml", "encodings", "#@encoding.rb" )
|
||||
require enc_file
|
||||
Encoding.apply(self, @encoding)
|
||||
rescue LoadError => err
|
||||
puts err.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
|
||||
ensure
|
||||
$VERBOSE = old_verbosity
|
||||
# ID ---> Encoding object
|
||||
attr_reader :encoding
|
||||
def encoding=(encoding)
|
||||
if encoding.is_a?(String)
|
||||
original_encoding = encoding
|
||||
encoding = find_encoding(encoding)
|
||||
unless encoding
|
||||
raise ArgumentError, "Bad encoding name #{original_encoding}"
|
||||
end
|
||||
end
|
||||
return false if defined?(@encoding) and encoding == @encoding
|
||||
if encoding and encoding != ::Encoding::UTF_8
|
||||
@encoding = encoding
|
||||
else
|
||||
@encoding = ::Encoding::UTF_8
|
||||
end
|
||||
true
|
||||
end
|
||||
|
||||
def check_encoding str
|
||||
def check_encoding(xml)
|
||||
# We have to recognize UTF-16, LSB UTF-16, and UTF-8
|
||||
if str[0,2] == "\xfe\xff"
|
||||
str[0,2] = ""
|
||||
return UTF_16
|
||||
elsif str[0,2] == "\xff\xfe"
|
||||
str[0,2] = ""
|
||||
return UNILE
|
||||
end
|
||||
str =~ /^\s*<\?xml\s+version\s*=\s*(['"]).*?\1\s+encoding\s*=\s*(["'])(.*?)\2/m
|
||||
return $3.upcase if $3
|
||||
return UTF_8
|
||||
if xml[0, 2] == "\xfe\xff"
|
||||
xml[0, 2] = ""
|
||||
::Encoding::UTF_16BE
|
||||
elsif xml[0, 2] == "\xff\xfe"
|
||||
xml[0, 2] = ""
|
||||
::Encoding::UTF_16LE
|
||||
else
|
||||
if /\A\s*<\?xml\s+version\s*=\s*(['"]).*?\1
|
||||
\s+encoding\s*=\s*(["'])(.*?)\2/mx =~ xml
|
||||
encoding_name = $3
|
||||
if /\Autf-16\z/i =~ encoding_name
|
||||
::Encoding::UTF_16BE
|
||||
else
|
||||
find_encoding(encoding_name)
|
||||
end
|
||||
else
|
||||
::Encoding::UTF_8
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def encode(string)
|
||||
string.encode(@encoding)
|
||||
end
|
||||
|
||||
def decode(string)
|
||||
string.encode(::Encoding::UTF_8, @encoding)
|
||||
end
|
||||
|
||||
private
|
||||
def find_encoding(name)
|
||||
case name
|
||||
when "UTF-16"
|
||||
name = "UTF-16BE"
|
||||
when /\Ashift-jis\z/i
|
||||
name = "Shift_JIS"
|
||||
when /\ACP-(\d+)\z/
|
||||
name = "CP#{$1}"
|
||||
end
|
||||
::Encoding.find(name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -1,103 +0,0 @@
|
||||
#
|
||||
# This class was contributed by Mikko Tiihonen mikko DOT tiihonen AT hut DOT fi
|
||||
#
|
||||
module REXML
|
||||
module Encoding
|
||||
register( "CP-1252" ) do |o|
|
||||
class << o
|
||||
alias encode encode_cp1252
|
||||
alias decode decode_cp1252
|
||||
end
|
||||
end
|
||||
|
||||
# Convert from UTF-8
|
||||
def encode_cp1252(content)
|
||||
array_utf8 = content.unpack('U*')
|
||||
array_enc = []
|
||||
array_utf8.each do |num|
|
||||
case num
|
||||
# shortcut first bunch basic characters
|
||||
when 0..0xFF; array_enc << num
|
||||
# characters added compared to iso-8859-1
|
||||
when 0x20AC; array_enc << 0x80 # 0xe2 0x82 0xac
|
||||
when 0x201A; array_enc << 0x82 # 0xe2 0x82 0x9a
|
||||
when 0x0192; array_enc << 0x83 # 0xc6 0x92
|
||||
when 0x201E; array_enc << 0x84 # 0xe2 0x82 0x9e
|
||||
when 0x2026; array_enc << 0x85 # 0xe2 0x80 0xa6
|
||||
when 0x2020; array_enc << 0x86 # 0xe2 0x80 0xa0
|
||||
when 0x2021; array_enc << 0x87 # 0xe2 0x80 0xa1
|
||||
when 0x02C6; array_enc << 0x88 # 0xcb 0x86
|
||||
when 0x2030; array_enc << 0x89 # 0xe2 0x80 0xb0
|
||||
when 0x0160; array_enc << 0x8A # 0xc5 0xa0
|
||||
when 0x2039; array_enc << 0x8B # 0xe2 0x80 0xb9
|
||||
when 0x0152; array_enc << 0x8C # 0xc5 0x92
|
||||
when 0x017D; array_enc << 0x8E # 0xc5 0xbd
|
||||
when 0x2018; array_enc << 0x91 # 0xe2 0x80 0x98
|
||||
when 0x2019; array_enc << 0x92 # 0xe2 0x80 0x99
|
||||
when 0x201C; array_enc << 0x93 # 0xe2 0x80 0x9c
|
||||
when 0x201D; array_enc << 0x94 # 0xe2 0x80 0x9d
|
||||
when 0x2022; array_enc << 0x95 # 0xe2 0x80 0xa2
|
||||
when 0x2013; array_enc << 0x96 # 0xe2 0x80 0x93
|
||||
when 0x2014; array_enc << 0x97 # 0xe2 0x80 0x94
|
||||
when 0x02DC; array_enc << 0x98 # 0xcb 0x9c
|
||||
when 0x2122; array_enc << 0x99 # 0xe2 0x84 0xa2
|
||||
when 0x0161; array_enc << 0x9A # 0xc5 0xa1
|
||||
when 0x203A; array_enc << 0x9B # 0xe2 0x80 0xba
|
||||
when 0x0152; array_enc << 0x9C # 0xc5 0x93
|
||||
when 0x017E; array_enc << 0x9E # 0xc5 0xbe
|
||||
when 0x0178; array_enc << 0x9F # 0xc5 0xb8
|
||||
else
|
||||
# all remaining basic characters can be used directly
|
||||
if num <= 0xFF
|
||||
array_enc << num
|
||||
else
|
||||
# Numeric entity (&#nnnn;); shard by Stefan Scholl
|
||||
array_enc.concat "&\##{num};".unpack('C*')
|
||||
end
|
||||
end
|
||||
end
|
||||
array_enc.pack('C*')
|
||||
end
|
||||
|
||||
# Convert to UTF-8
|
||||
def decode_cp1252(str)
|
||||
array_latin9 = str.unpack('C*')
|
||||
array_enc = []
|
||||
array_latin9.each do |num|
|
||||
case num
|
||||
# characters that added compared to iso-8859-1
|
||||
when 0x80; array_enc << 0x20AC # 0xe2 0x82 0xac
|
||||
when 0x82; array_enc << 0x201A # 0xe2 0x82 0x9a
|
||||
when 0x83; array_enc << 0x0192 # 0xc6 0x92
|
||||
when 0x84; array_enc << 0x201E # 0xe2 0x82 0x9e
|
||||
when 0x85; array_enc << 0x2026 # 0xe2 0x80 0xa6
|
||||
when 0x86; array_enc << 0x2020 # 0xe2 0x80 0xa0
|
||||
when 0x87; array_enc << 0x2021 # 0xe2 0x80 0xa1
|
||||
when 0x88; array_enc << 0x02C6 # 0xcb 0x86
|
||||
when 0x89; array_enc << 0x2030 # 0xe2 0x80 0xb0
|
||||
when 0x8A; array_enc << 0x0160 # 0xc5 0xa0
|
||||
when 0x8B; array_enc << 0x2039 # 0xe2 0x80 0xb9
|
||||
when 0x8C; array_enc << 0x0152 # 0xc5 0x92
|
||||
when 0x8E; array_enc << 0x017D # 0xc5 0xbd
|
||||
when 0x91; array_enc << 0x2018 # 0xe2 0x80 0x98
|
||||
when 0x92; array_enc << 0x2019 # 0xe2 0x80 0x99
|
||||
when 0x93; array_enc << 0x201C # 0xe2 0x80 0x9c
|
||||
when 0x94; array_enc << 0x201D # 0xe2 0x80 0x9d
|
||||
when 0x95; array_enc << 0x2022 # 0xe2 0x80 0xa2
|
||||
when 0x96; array_enc << 0x2013 # 0xe2 0x80 0x93
|
||||
when 0x97; array_enc << 0x2014 # 0xe2 0x80 0x94
|
||||
when 0x98; array_enc << 0x02DC # 0xcb 0x9c
|
||||
when 0x99; array_enc << 0x2122 # 0xe2 0x84 0xa2
|
||||
when 0x9A; array_enc << 0x0161 # 0xc5 0xa1
|
||||
when 0x9B; array_enc << 0x203A # 0xe2 0x80 0xba
|
||||
when 0x9C; array_enc << 0x0152 # 0xc5 0x93
|
||||
when 0x9E; array_enc << 0x017E # 0xc5 0xbe
|
||||
when 0x9F; array_enc << 0x0178 # 0xc5 0xb8
|
||||
else
|
||||
array_enc << num
|
||||
end
|
||||
end
|
||||
array_enc.pack('U*')
|
||||
end
|
||||
end
|
||||
end
|
@ -1,35 +0,0 @@
|
||||
module REXML
|
||||
module Encoding
|
||||
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 decode_eucjp(str)
|
||||
NKF.nkf(EUCTOU8, str)
|
||||
end
|
||||
|
||||
def encode_eucjp content
|
||||
NKF.nkf(U8TOEUC, content)
|
||||
end
|
||||
end
|
||||
|
||||
register("EUC-JP") do |obj|
|
||||
class << obj
|
||||
alias decode decode_eucjp
|
||||
alias encode encode_eucjp
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -1,22 +0,0 @@
|
||||
require "iconv"
|
||||
raise LoadError unless defined? Iconv
|
||||
|
||||
module REXML
|
||||
module Encoding
|
||||
def decode_iconv(str)
|
||||
Iconv.conv(UTF_8, @encoding, str)
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
end
|
@ -1,7 +0,0 @@
|
||||
require 'rexml/encodings/US-ASCII'
|
||||
|
||||
module REXML
|
||||
module Encoding
|
||||
register("ISO-8859-1", &encoding_method("US-ASCII"))
|
||||
end
|
||||
end
|
@ -1,72 +0,0 @@
|
||||
#
|
||||
# This class was contributed by Mikko Tiihonen mikko DOT tiihonen AT hut DOT fi
|
||||
#
|
||||
module REXML
|
||||
module Encoding
|
||||
register("ISO-8859-15") do |o|
|
||||
alias encode to_iso_8859_15
|
||||
alias decode from_iso_8859_15
|
||||
end
|
||||
|
||||
# Convert from UTF-8
|
||||
def to_iso_8859_15(content)
|
||||
array_utf8 = content.unpack('U*')
|
||||
array_enc = []
|
||||
array_utf8.each do |num|
|
||||
case num
|
||||
# shortcut first bunch basic characters
|
||||
when 0..0xA3; array_enc << num
|
||||
# characters removed compared to iso-8859-1
|
||||
when 0xA4; array_enc << '¤'
|
||||
when 0xA6; array_enc << '¦'
|
||||
when 0xA8; array_enc << '¨'
|
||||
when 0xB4; array_enc << '´'
|
||||
when 0xB8; array_enc << '¸'
|
||||
when 0xBC; array_enc << '¼'
|
||||
when 0xBD; array_enc << '½'
|
||||
when 0xBE; array_enc << '¾'
|
||||
# characters added compared to iso-8859-1
|
||||
when 0x20AC; array_enc << 0xA4 # 0xe2 0x82 0xac
|
||||
when 0x0160; array_enc << 0xA6 # 0xc5 0xa0
|
||||
when 0x0161; array_enc << 0xA8 # 0xc5 0xa1
|
||||
when 0x017D; array_enc << 0xB4 # 0xc5 0xbd
|
||||
when 0x017E; array_enc << 0xB8 # 0xc5 0xbe
|
||||
when 0x0152; array_enc << 0xBC # 0xc5 0x92
|
||||
when 0x0153; array_enc << 0xBD # 0xc5 0x93
|
||||
when 0x0178; array_enc << 0xBE # 0xc5 0xb8
|
||||
else
|
||||
# all remaining basic characters can be used directly
|
||||
if num <= 0xFF
|
||||
array_enc << num
|
||||
else
|
||||
# Numeric entity (&#nnnn;); shard by Stefan Scholl
|
||||
array_enc.concat "&\##{num};".unpack('C*')
|
||||
end
|
||||
end
|
||||
end
|
||||
array_enc.pack('C*')
|
||||
end
|
||||
|
||||
# Convert to UTF-8
|
||||
def from_iso_8859_15(str)
|
||||
array_latin9 = str.unpack('C*')
|
||||
array_enc = []
|
||||
array_latin9.each do |num|
|
||||
case num
|
||||
# characters that differ compared to iso-8859-1
|
||||
when 0xA4; array_enc << 0x20AC
|
||||
when 0xA6; array_enc << 0x0160
|
||||
when 0xA8; array_enc << 0x0161
|
||||
when 0xB4; array_enc << 0x017D
|
||||
when 0xB8; array_enc << 0x017E
|
||||
when 0xBC; array_enc << 0x0152
|
||||
when 0xBD; array_enc << 0x0153
|
||||
when 0xBE; array_enc << 0x0178
|
||||
else
|
||||
array_enc << num
|
||||
end
|
||||
end
|
||||
array_enc.pack('U*')
|
||||
end
|
||||
end
|
||||
end
|
@ -1,37 +0,0 @@
|
||||
module REXML
|
||||
module Encoding
|
||||
begin
|
||||
require 'uconv'
|
||||
|
||||
def decode_sjis content
|
||||
Uconv::sjistou8(content)
|
||||
end
|
||||
|
||||
def encode_sjis(str)
|
||||
Uconv::u8tosjis(str)
|
||||
end
|
||||
rescue LoadError
|
||||
require 'nkf'
|
||||
|
||||
SJISTOU8 = '-Swm0x'
|
||||
U8TOSJIS = '-Wsm0x'
|
||||
|
||||
def decode_sjis(str)
|
||||
NKF.nkf(SJISTOU8, str)
|
||||
end
|
||||
|
||||
def encode_sjis content
|
||||
NKF.nkf(U8TOSJIS, content)
|
||||
end
|
||||
end
|
||||
|
||||
b = proc do |obj|
|
||||
class << obj
|
||||
alias decode decode_sjis
|
||||
alias encode encode_sjis
|
||||
end
|
||||
end
|
||||
register("SHIFT-JIS", &b)
|
||||
register("SHIFT_JIS", &b)
|
||||
end
|
||||
end
|
@ -1 +0,0 @@
|
||||
require 'rexml/encodings/SHIFT-JIS'
|
@ -1,34 +0,0 @@
|
||||
module REXML
|
||||
module Encoding
|
||||
def encode_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 decode_unile(str)
|
||||
array_enc=str.unpack('C*')
|
||||
array_utf8 = []
|
||||
0.step(array_enc.size-1, 2){|i|
|
||||
array_utf8 << (array_enc.at(i) + array_enc.at(i+1)*0x100)
|
||||
}
|
||||
array_utf8.pack('U*')
|
||||
end
|
||||
|
||||
register(UNILE) do |obj|
|
||||
class << obj
|
||||
alias decode decode_unile
|
||||
alias encode encode_unile
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -1,30 +0,0 @@
|
||||
module REXML
|
||||
module Encoding
|
||||
# Convert from UTF-8
|
||||
def encode_ascii content
|
||||
array_utf8 = content.unpack('U*')
|
||||
array_enc = []
|
||||
array_utf8.each do |num|
|
||||
if num <= 0x7F
|
||||
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_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
|
@ -1,35 +0,0 @@
|
||||
module REXML
|
||||
module Encoding
|
||||
def encode_utf16 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 decode_utf16(str)
|
||||
str = str[2..-1] if /^\376\377/n =~ str
|
||||
array_enc=str.unpack('C*')
|
||||
array_utf8 = []
|
||||
0.step(array_enc.size-1, 2){|i|
|
||||
array_utf8 << (array_enc.at(i+1) + array_enc.at(i)*0x100)
|
||||
}
|
||||
array_utf8.pack('U*')
|
||||
end
|
||||
|
||||
register(UTF_16) do |obj|
|
||||
class << obj
|
||||
alias decode decode_utf16
|
||||
alias encode encode_utf16
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -1,18 +0,0 @@
|
||||
module REXML
|
||||
module Encoding
|
||||
def encode_utf8 content
|
||||
content
|
||||
end
|
||||
|
||||
def decode_utf8(str)
|
||||
str
|
||||
end
|
||||
|
||||
register(UTF_8) do |obj|
|
||||
class << obj
|
||||
alias decode decode_utf8
|
||||
alias encode encode_utf8
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -22,7 +22,7 @@ module REXML
|
||||
case node
|
||||
|
||||
when Document
|
||||
if node.xml_decl.encoding != "UTF-8" && !output.kind_of?(Output)
|
||||
if node.xml_decl.encoding != ::Encoding::UTF_8 && !output.kind_of?(Output)
|
||||
output = Output.new( output, node.xml_decl.encoding )
|
||||
end
|
||||
write_document( node, output )
|
||||
|
@ -10,7 +10,7 @@ module REXML
|
||||
@output = real_IO
|
||||
self.encoding = encd
|
||||
|
||||
@to_utf = encd == UTF_8 ? false : true
|
||||
@to_utf = (@encoding != ::Encoding::UTF_8)
|
||||
end
|
||||
|
||||
def <<( content )
|
||||
|
@ -28,7 +28,7 @@ module REXML
|
||||
err << "\nLine: #{line}\n"
|
||||
err << "Position: #{position}\n"
|
||||
err << "Last 80 unconsumed characters:\n"
|
||||
err << @source.buffer[0..80].gsub(/\n/, ' ')
|
||||
err << @source.buffer[0..80].force_encoding("ASCII-8BIT").gsub(/\n/, ' ')
|
||||
end
|
||||
|
||||
err
|
||||
|
@ -248,10 +248,8 @@ module REXML
|
||||
@document_status = :after_doctype
|
||||
@source.read if @source.buffer.size<2
|
||||
md = @source.match(/\s*/um, true)
|
||||
if @source.encoding == "UTF-8"
|
||||
if @source.buffer.respond_to? :force_encoding
|
||||
@source.buffer.force_encoding(Encoding::UTF_8)
|
||||
end
|
||||
if @source.encoding == ::Encoding::UTF_8
|
||||
@source.buffer.force_encoding(::Encoding::UTF_8)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -54,13 +54,13 @@ module REXML
|
||||
def encoding=(enc)
|
||||
return unless super
|
||||
@line_break = encode( '>' )
|
||||
if enc != UTF_8
|
||||
if @encoding != ::Encoding::UTF_8
|
||||
@buffer = decode(@buffer)
|
||||
@to_utf = true
|
||||
else
|
||||
@to_utf = false
|
||||
if @buffer.respond_to? :force_encoding
|
||||
@buffer.force_encoding Encoding::UTF_8
|
||||
@buffer.force_encoding ::Encoding::UTF_8
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -109,9 +109,20 @@ module REXML
|
||||
end
|
||||
|
||||
private
|
||||
def normalized_encoding_name(_encoding)
|
||||
if _encoding == ::Encoding::UTF_16BE
|
||||
"UTF-16"
|
||||
else
|
||||
return _encoding.name
|
||||
end
|
||||
end
|
||||
|
||||
def content(enc)
|
||||
rv = "version='#@version'"
|
||||
rv << " encoding='#{enc}'" if @writeencoding || enc !~ /utf-8/i
|
||||
if @writeencoding || enc.to_s !~ /\Autf-8\z/i
|
||||
encoding_name = normalized_encoding_name(enc)
|
||||
rv << " encoding='#{encoding_name}'"
|
||||
end
|
||||
rv << " standalone='#@standalone'" if @standalone
|
||||
rv
|
||||
end
|
||||
|
@ -1,15 +1,13 @@
|
||||
#!/usr/bin/ruby -Ku
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
require 'kconv'
|
||||
require 'iconv'
|
||||
require 'rexml/encoding'
|
||||
|
||||
|
||||
class ChangingEncodings < Test::Unit::TestCase
|
||||
def initialize a
|
||||
@u = 'テスト ほげ ふが 美しい'
|
||||
@e = Kconv.toeuc(@u)
|
||||
@e = @u.encode("EUC-JP")
|
||||
@f = Foo.new
|
||||
super
|
||||
end
|
||||
|
@ -241,7 +241,7 @@ DELIMITER
|
||||
end
|
||||
|
||||
doc = REXML::Document.new(source_iso)
|
||||
assert_equal('ISO-8859-1', doc.xml_decl.encoding)
|
||||
assert_equal('ISO-8859-1', doc.xml_decl.encoding.to_s)
|
||||
assert_equal(koln_utf, doc.root.text)
|
||||
doc.write(out="")
|
||||
assert_equal(source_iso, out )
|
||||
@ -270,7 +270,7 @@ EOF
|
||||
expected_utf = expected_iso.unpack('C*').pack('U*')
|
||||
if expected_utf.respond_to? :encode
|
||||
expected_iso.force_encoding("iso-8859-1")
|
||||
expected_utf.force_encoding(Encoding::UTF_8)
|
||||
expected_utf.force_encoding(::Encoding::UTF_8)
|
||||
end
|
||||
assert_equal(expected_utf, tn.to_s.strip)
|
||||
f = REXML::Formatters::Default.new
|
||||
|
@ -433,6 +433,11 @@ class Tester < Test::Unit::TestCase
|
||||
assert_equal source, decl2.to_s
|
||||
end
|
||||
|
||||
def test_xmldecl_utf_16be_encoding_name
|
||||
assert_equal("<?xml version='1.0' encoding='UTF-16'?>",
|
||||
XMLDecl.new("1.0", "UTF-16").to_s)
|
||||
end
|
||||
|
||||
def each_test( element, xpath, num_children )
|
||||
count = 0
|
||||
element.each_element( xpath ) { |child|
|
||||
@ -1123,7 +1128,6 @@ EOL
|
||||
end
|
||||
|
||||
def test_repeated_writes
|
||||
require 'iconv'
|
||||
a = IO.read(fixture_path("iso8859-1.xml"))
|
||||
f = REXML::Formatters::Pretty.new
|
||||
|
||||
@ -1246,9 +1250,9 @@ EOL
|
||||
|
||||
def test_ticket_88
|
||||
doc = REXML::Document.new("<?xml version=\"1.0\" encoding=\"shift_jis\"?>")
|
||||
assert_equal("<?xml version='1.0' encoding='SHIFT_JIS'?>", doc.to_s)
|
||||
assert_equal("<?xml version='1.0' encoding='Shift_JIS'?>", doc.to_s)
|
||||
doc = REXML::Document.new("<?xml version = \"1.0\" encoding = \"shift_jis\"?>")
|
||||
assert_equal("<?xml version='1.0' encoding='SHIFT_JIS'?>", doc.to_s)
|
||||
assert_equal("<?xml version='1.0' encoding='Shift_JIS'?>", doc.to_s)
|
||||
end
|
||||
|
||||
def test_ticket_85
|
||||
|
@ -26,7 +26,7 @@ class EncodingTester < Test::Unit::TestCase
|
||||
def test_encoded_in_change_out
|
||||
doc = Document.new( @encoded )
|
||||
doc.xml_decl.encoding = "UTF-8"
|
||||
assert_equal( doc.encoding, "UTF-8" )
|
||||
assert_equal( ::Encoding::UTF_8, doc.encoding )
|
||||
REXML::Formatters::Default.new.write( doc.root, out="" )
|
||||
out.force_encoding('binary') if out.respond_to? :force_encoding
|
||||
assert_equal( @not_encoded, out )
|
||||
@ -47,7 +47,7 @@ class EncodingTester < Test::Unit::TestCase
|
||||
def test_in_change_out
|
||||
doc = Document.new( @not_encoded )
|
||||
doc.xml_decl.encoding = "ISO-8859-3"
|
||||
assert_equal( doc.encoding, "ISO-8859-3" )
|
||||
assert_equal( ::Encoding::ISO_8859_3, doc.encoding )
|
||||
doc.write( out="" )
|
||||
out.force_encoding('binary') if out.respond_to? :force_encoding
|
||||
assert_equal( @encoded, out )
|
||||
@ -86,7 +86,7 @@ class EncodingTester < Test::Unit::TestCase
|
||||
|
||||
def test_ticket_110
|
||||
utf16 = REXML::Document.new(File.new(fixture_path("ticket_110_utf16.xml")))
|
||||
assert_equal( "UTF-16", utf16.encoding )
|
||||
assert_equal( ::Encoding::UTF_16BE, utf16.encoding )
|
||||
assert( utf16[0].kind_of?(REXML::XMLDecl))
|
||||
end
|
||||
end
|
||||
|
@ -24,9 +24,8 @@ module RSS
|
||||
|
||||
xmldecl = doc.xml_decl
|
||||
|
||||
%w(version encoding).each do |x|
|
||||
assert_equal(instance_eval(x), xmldecl.__send__(x))
|
||||
end
|
||||
assert_equal(version, xmldecl.version)
|
||||
assert_equal(encoding, xmldecl.encoding.to_s)
|
||||
assert_equal(standalone, !xmldecl.standalone.nil?)
|
||||
|
||||
assert_equal(@rdf_uri, doc.root.namespace)
|
||||
|
@ -21,9 +21,8 @@ module RSS
|
||||
|
||||
xmldecl = doc.xml_decl
|
||||
|
||||
%w(version encoding).each do |x|
|
||||
assert_equal(instance_eval(x), xmldecl.__send__(x))
|
||||
end
|
||||
assert_equal(version, xmldecl.version)
|
||||
assert_equal(encoding, xmldecl.encoding.to_s)
|
||||
assert_equal(standalone, !xmldecl.standalone.nil?)
|
||||
|
||||
assert_equal("", doc.root.namespace)
|
||||
|
@ -69,9 +69,8 @@ module RSS
|
||||
doc = REXML::Document.new(feed.to_s)
|
||||
xmldecl = doc.xml_decl
|
||||
|
||||
%w(version encoding).each do |x|
|
||||
assert_equal(instance_eval(x), xmldecl.__send__(x))
|
||||
end
|
||||
assert_equal(version, xmldecl.version)
|
||||
assert_equal(encoding, xmldecl.encoding.to_s)
|
||||
assert_equal(standalone, !xmldecl.standalone.nil?)
|
||||
|
||||
assert_equal(@uri, doc.root.namespace)
|
||||
@ -96,9 +95,8 @@ module RSS
|
||||
doc = REXML::Document.new(entry.to_s)
|
||||
xmldecl = doc.xml_decl
|
||||
|
||||
%w(version encoding).each do |x|
|
||||
assert_equal(instance_eval(x), xmldecl.__send__(x))
|
||||
end
|
||||
assert_equal(version, xmldecl.version)
|
||||
assert_equal(encoding, xmldecl.encoding.to_s)
|
||||
assert_equal(standalone, !xmldecl.standalone.nil?)
|
||||
|
||||
assert_equal(@uri, doc.root.namespace)
|
||||
|
Loading…
x
Reference in New Issue
Block a user