SJISTOU8 | = | '-Swm0x' | ||
U8TOSJIS | = | '-Wsm0x' | ||
EUCTOU8 | = | '-Ewm0' | ||
U8TOEUC | = | '-Wem0' | ||
UTF_8 | = | 'UTF-8' | Native, default format is UTF-8, so it is declared here rather than in an encodings/ definition. | |
UTF_16 | = | 'UTF-16' | ||
UNILE | = | 'UNILE' |
to_iso_8859_15 | -> | encode |
from_iso_8859_15 | -> | decode |
encoding | [R] | ID —> Encoding name |
# File lib/rexml/encoding.rb, line 8 8: def self.apply(obj, enc) 9: @encoding_methods[enc][obj] 10: end
# File lib/rexml/encoding.rb, line 11 11: def self.encoding_method(enc) 12: @encoding_methods[enc] 13: end
# File lib/rexml/encoding.rb, line 5 5: def self.register(enc, &block) 6: @encoding_methods[enc] = block 7: end
# File lib/rexml/encoding.rb, line 57 57: def check_encoding str 58: # We have to recognize UTF-16, LSB UTF-16, and UTF-8 59: if str[0] == 0xfe && str[1] == 0xff 60: str[0,2] = "" 61: return UTF_16 62: elsif str[0] == 0xff && str[1] == 0xfe 63: str[0,2] = "" 64: return UNILE 65: end 66: str =~ /^\s*<\?xml\s+version\s*=\s*(['"]).*?\1\s+encoding\s*=\s*(["'])(.*?)\2/um 67: return $3.upcase if $3 68: return UTF_8 69: end
Convert to UTF-8
# File lib/rexml/encodings/US-ASCII.rb, line 19 19: def decode_ascii(str) 20: str.unpack('C*').pack('U*') 21: end
Convert to UTF-8
# File lib/rexml/encodings/CP-1252.rb, line 63 63: def decode_cp1252(str) 64: array_latin9 = str.unpack('C*') 65: array_enc = [] 66: array_latin9.each do |num| 67: case num 68: # characters that added compared to iso-8859-1 69: when 0x80; array_enc << 0x20AC # 0xe2 0x82 0xac 70: when 0x82; array_enc << 0x201A # 0xe2 0x82 0x9a 71: when 0x83; array_enc << 0x0192 # 0xc6 0x92 72: when 0x84; array_enc << 0x201E # 0xe2 0x82 0x9e 73: when 0x85; array_enc << 0x2026 # 0xe2 0x80 0xa6 74: when 0x86; array_enc << 0x2020 # 0xe2 0x80 0xa0 75: when 0x87; array_enc << 0x2021 # 0xe2 0x80 0xa1 76: when 0x88; array_enc << 0x02C6 # 0xcb 0x86 77: when 0x89; array_enc << 0x2030 # 0xe2 0x80 0xb0 78: when 0x8A; array_enc << 0x0160 # 0xc5 0xa0 79: when 0x8B; array_enc << 0x2039 # 0xe2 0x80 0xb9 80: when 0x8C; array_enc << 0x0152 # 0xc5 0x92 81: when 0x8E; array_enc << 0x017D # 0xc5 0xbd 82: when 0x91; array_enc << 0x2018 # 0xe2 0x80 0x98 83: when 0x92; array_enc << 0x2019 # 0xe2 0x80 0x99 84: when 0x93; array_enc << 0x201C # 0xe2 0x80 0x9c 85: when 0x94; array_enc << 0x201D # 0xe2 0x80 0x9d 86: when 0x95; array_enc << 0x2022 # 0xe2 0x80 0xa2 87: when 0x96; array_enc << 0x2013 # 0xe2 0x80 0x93 88: when 0x97; array_enc << 0x2014 # 0xe2 0x80 0x94 89: when 0x98; array_enc << 0x02DC # 0xcb 0x9c 90: when 0x99; array_enc << 0x2122 # 0xe2 0x84 0xa2 91: when 0x9A; array_enc << 0x0161 # 0xc5 0xa1 92: when 0x9B; array_enc << 0x203A # 0xe2 0x80 0xba 93: when 0x9C; array_enc << 0x0152 # 0xc5 0x93 94: when 0x9E; array_enc << 0x017E # 0xc5 0xbe 95: when 0x9F; array_enc << 0x0178 # 0xc5 0xb8 96: else 97: array_enc << num 98: end 99: end 100: array_enc.pack('U*') 101: end
# File lib/rexml/encodings/EUC-JP.rb, line 19 19: def decode_eucjp(str) 20: NKF.nkf(EUCTOU8, str) 21: end
# File lib/rexml/encodings/ICONV.rb, line 6 6: def decode_iconv(str) 7: Iconv.conv(UTF_8, @encoding, str) 8: end
# File lib/rexml/encodings/SHIFT-JIS.rb, line 6 6: def decode_sjis content 7: Uconv::sjistou8(content) 8: end
# File lib/rexml/encodings/SHIFT-JIS.rb, line 19 19: def decode_sjis(str) 20: NKF.nkf(SJISTOU8, str) 21: end
# File lib/rexml/encodings/UNILE.rb, line 18 18: def decode_unile(str) 19: array_enc=str.unpack('C*') 20: array_utf8 = [] 21: 0.step(array_enc.size-1, 2){|i| 22: array_utf8 << (array_enc.at(i) + array_enc.at(i+1)*0x100) 23: } 24: array_utf8.pack('U*') 25: end
# File lib/rexml/encodings/UTF-16.rb, line 18 18: def decode_utf16(str) 19: str = str[2..-1] if /^\376\377/n =~ str 20: array_enc=str.unpack('C*') 21: array_utf8 = [] 22: 0.step(array_enc.size-1, 2){|i| 23: array_utf8 << (array_enc.at(i+1) + array_enc.at(i)*0x100) 24: } 25: array_utf8.pack('U*') 26: end
Convert from UTF-8
# File lib/rexml/encodings/US-ASCII.rb, line 4 4: def encode_ascii content 5: array_utf8 = content.unpack('U*') 6: array_enc = [] 7: array_utf8.each do |num| 8: if num <= 0x7F 9: array_enc << num 10: else 11: # Numeric entity (&#nnnn;); shard by Stefan Scholl 12: array_enc.concat "&\##{num};".unpack('C*') 13: end 14: end 15: array_enc.pack('C*') 16: end
Convert from UTF-8
# File lib/rexml/encodings/CP-1252.rb, line 14 14: def encode_cp1252(content) 15: array_utf8 = content.unpack('U*') 16: array_enc = [] 17: array_utf8.each do |num| 18: case num 19: # shortcut first bunch basic characters 20: when 0..0xFF; array_enc << num 21: # characters added compared to iso-8859-1 22: when 0x20AC; array_enc << 0x80 # 0xe2 0x82 0xac 23: when 0x201A; array_enc << 0x82 # 0xe2 0x82 0x9a 24: when 0x0192; array_enc << 0x83 # 0xc6 0x92 25: when 0x201E; array_enc << 0x84 # 0xe2 0x82 0x9e 26: when 0x2026; array_enc << 0x85 # 0xe2 0x80 0xa6 27: when 0x2020; array_enc << 0x86 # 0xe2 0x80 0xa0 28: when 0x2021; array_enc << 0x87 # 0xe2 0x80 0xa1 29: when 0x02C6; array_enc << 0x88 # 0xcb 0x86 30: when 0x2030; array_enc << 0x89 # 0xe2 0x80 0xb0 31: when 0x0160; array_enc << 0x8A # 0xc5 0xa0 32: when 0x2039; array_enc << 0x8B # 0xe2 0x80 0xb9 33: when 0x0152; array_enc << 0x8C # 0xc5 0x92 34: when 0x017D; array_enc << 0x8E # 0xc5 0xbd 35: when 0x2018; array_enc << 0x91 # 0xe2 0x80 0x98 36: when 0x2019; array_enc << 0x92 # 0xe2 0x80 0x99 37: when 0x201C; array_enc << 0x93 # 0xe2 0x80 0x9c 38: when 0x201D; array_enc << 0x94 # 0xe2 0x80 0x9d 39: when 0x2022; array_enc << 0x95 # 0xe2 0x80 0xa2 40: when 0x2013; array_enc << 0x96 # 0xe2 0x80 0x93 41: when 0x2014; array_enc << 0x97 # 0xe2 0x80 0x94 42: when 0x02DC; array_enc << 0x98 # 0xcb 0x9c 43: when 0x2122; array_enc << 0x99 # 0xe2 0x84 0xa2 44: when 0x0161; array_enc << 0x9A # 0xc5 0xa1 45: when 0x203A; array_enc << 0x9B # 0xe2 0x80 0xba 46: when 0x0152; array_enc << 0x9C # 0xc5 0x93 47: when 0x017E; array_enc << 0x9E # 0xc5 0xbe 48: when 0x0178; array_enc << 0x9F # 0xc5 0xb8 49: else 50: # all remaining basic characters can be used directly 51: if num <= 0xFF 52: array_enc << num 53: else 54: # Numeric entity (&#nnnn;); shard by Stefan Scholl 55: array_enc.concat "&\##{num};".unpack('C*') 56: end 57: end 58: end 59: array_enc.pack('C*') 60: end
# File lib/rexml/encodings/EUC-JP.rb, line 23 23: def encode_eucjp content 24: NKF.nkf(U8TOEUC, content) 25: end
# File lib/rexml/encodings/EUC-JP.rb, line 10 10: def encode_eucjp content 11: Uconv::u8toeuc(content) 12: end
# File lib/rexml/encodings/ICONV.rb, line 10 10: def encode_iconv(content) 11: Iconv.conv(@encoding, UTF_8, content) 12: end
# File lib/rexml/encodings/SHIFT-JIS.rb, line 10 10: def encode_sjis(str) 11: Uconv::u8tosjis(str) 12: end
# File lib/rexml/encodings/SHIFT-JIS.rb, line 23 23: def encode_sjis content 24: NKF.nkf(U8TOSJIS, content) 25: end
# File lib/rexml/encodings/UNILE.rb, line 3 3: def encode_unile content 4: array_utf8 = content.unpack("U*") 5: array_enc = [] 6: array_utf8.each do |num| 7: if ((num>>16) > 0) 8: array_enc << ?? 9: array_enc << 0 10: else 11: array_enc << (num & 0xFF) 12: array_enc << (num >> 8) 13: end 14: end 15: array_enc.pack('C*') 16: end
# File lib/rexml/encodings/UTF-16.rb, line 3 3: def encode_utf16 content 4: array_utf8 = content.unpack("U*") 5: array_enc = [] 6: array_utf8.each do |num| 7: if ((num>>16) > 0) 8: array_enc << 0 9: array_enc << ?? 10: else 11: array_enc << (num >> 8) 12: array_enc << (num & 0xFF) 13: end 14: end 15: array_enc.pack('C*') 16: end
# File lib/rexml/encoding.rb, line 23 23: def encoding=( enc ) 24: old_verbosity = $VERBOSE 25: begin 26: $VERBOSE = false 27: enc = enc.nil? ? nil : enc.upcase 28: return false if defined? @encoding and enc == @encoding 29: if enc and enc != UTF_8 30: @encoding = enc 31: raise ArgumentError, "Bad encoding name #@encoding" unless @encoding =~ /^[\w-]+$/ 32: @encoding.untaint 33: begin 34: require 'rexml/encodings/ICONV.rb' 35: Encoding.apply(self, "ICONV") 36: rescue LoadError, Exception 37: begin 38: enc_file = File.join( "rexml", "encodings", "#@encoding.rb" ) 39: require enc_file 40: Encoding.apply(self, @encoding) 41: rescue LoadError => err 42: puts err.message 43: raise ArgumentError, "No decoder found for encoding #@encoding. Please install iconv." 44: end 45: end 46: else 47: @encoding = UTF_8 48: require 'rexml/encodings/UTF-8.rb' 49: Encoding.apply(self, @encoding) 50: end 51: ensure 52: $VERBOSE = old_verbosity 53: end 54: true 55: end
Convert to UTF-8
# File lib/rexml/encodings/ISO-8859-15.rb, line 51 51: def from_iso_8859_15(str) 52: array_latin9 = str.unpack('C*') 53: array_enc = [] 54: array_latin9.each do |num| 55: case num 56: # characters that differ compared to iso-8859-1 57: when 0xA4; array_enc << 0x20AC 58: when 0xA6; array_enc << 0x0160 59: when 0xA8; array_enc << 0x0161 60: when 0xB4; array_enc << 0x017D 61: when 0xB8; array_enc << 0x017E 62: when 0xBC; array_enc << 0x0152 63: when 0xBD; array_enc << 0x0153 64: when 0xBE; array_enc << 0x0178 65: else 66: array_enc << num 67: end 68: end 69: array_enc.pack('U*') 70: end
Convert from UTF-8
# File lib/rexml/encodings/ISO-8859-15.rb, line 12 12: def to_iso_8859_15(content) 13: array_utf8 = content.unpack('U*') 14: array_enc = [] 15: array_utf8.each do |num| 16: case num 17: # shortcut first bunch basic characters 18: when 0..0xA3; array_enc << num 19: # characters removed compared to iso-8859-1 20: when 0xA4; array_enc << '¤' 21: when 0xA6; array_enc << '¦' 22: when 0xA8; array_enc << '¨' 23: when 0xB4; array_enc << '´' 24: when 0xB8; array_enc << '¸' 25: when 0xBC; array_enc << '¼' 26: when 0xBD; array_enc << '½' 27: when 0xBE; array_enc << '¾' 28: # characters added compared to iso-8859-1 29: when 0x20AC; array_enc << 0xA4 # 0xe2 0x82 0xac 30: when 0x0160; array_enc << 0xA6 # 0xc5 0xa0 31: when 0x0161; array_enc << 0xA8 # 0xc5 0xa1 32: when 0x017D; array_enc << 0xB4 # 0xc5 0xbd 33: when 0x017E; array_enc << 0xB8 # 0xc5 0xbe 34: when 0x0152; array_enc << 0xBC # 0xc5 0x92 35: when 0x0153; array_enc << 0xBD # 0xc5 0x93 36: when 0x0178; array_enc << 0xBE # 0xc5 0xb8 37: else 38: # all remaining basic characters can be used directly 39: if num <= 0xFF 40: array_enc << num 41: else 42: # Numeric entity (&#nnnn;); shard by Stefan Scholl 43: array_enc.concat "&\##{num};".unpack('C*') 44: end 45: end 46: end 47: array_enc.pack('C*') 48: end