2003-06-09 21:31:01 -04:00
|
|
|
module REXML
|
2003-12-08 21:41:33 -05:00
|
|
|
module Encoding
|
|
|
|
# Convert from UTF-8
|
2005-04-09 13:03:32 -04:00
|
|
|
def encode_ascii content
|
2003-12-08 21:41:33 -05:00
|
|
|
array_utf8 = content.unpack('U*')
|
|
|
|
array_enc = []
|
|
|
|
array_utf8.each do |num|
|
2004-05-16 11:17:31 -04:00
|
|
|
if num <= 0x7F
|
2003-12-08 21:41:33 -05:00
|
|
|
array_enc << num
|
|
|
|
else
|
|
|
|
# Numeric entity (&#nnnn;); shard by Stefan Scholl
|
|
|
|
array_enc.concat "&\##{num};".unpack('C*')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
array_enc.pack('C*')
|
|
|
|
end
|
2003-06-09 21:31:01 -04:00
|
|
|
|
2003-12-08 21:41:33 -05:00
|
|
|
# Convert to UTF-8
|
2005-04-09 13:03:32 -04:00
|
|
|
def decode_ascii(str)
|
2003-12-23 05:07:45 -05:00
|
|
|
str.unpack('C*').pack('U*')
|
2003-12-08 21:41:33 -05:00
|
|
|
end
|
2005-04-09 13:03:32 -04:00
|
|
|
|
|
|
|
register("US-ASCII") do |obj|
|
|
|
|
class << obj
|
|
|
|
alias decode decode_ascii
|
|
|
|
alias encode encode_ascii
|
|
|
|
end
|
|
|
|
end
|
2003-12-08 21:41:33 -05:00
|
|
|
end
|
2003-06-09 21:31:01 -04:00
|
|
|
end
|