forked from kkazuo/euc-jp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
euc_table.rb
57 lines (50 loc) · 1.43 KB
/
euc_table.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
euc = {}
utf = {}
# ASCII Compatible
(0x00..0x7E).each do |i|
euc["0x#{i.to_s(16).rjust(2, '0')}"] = [i].pack('c*').encode("UTF-8", "EUC-JP")
utf[[i].pack('c*').encode("UTF-8", "EUC-JP").bytes] = [i]
end
# Hiragana
(0xA1..0xDF).each do |i|
begin
euc["0x8e#{i.to_s(16).rjust(2, '0')}"] = [0x8e, i].pack('c*').encode("UTF-8", "EUC-JP")
utf[[0x8e, i].pack('c*').encode("UTF-8", "EUC-JP").bytes] = [0x8e, i]
rescue Encoding::UndefinedConversionError
# Ignore
end
end
# JIS X 0208
(0xA1..0xFE).each do |i|
(0xA1..0xFE).each do |j|
begin
euc["0x#{i.to_s(16).rjust(2, '0')}#{j.to_s(16).ljust(2, '0')}"] = [i, j].pack('c*').encode("UTF-8", "EUC-JP")
utf[[i, j].pack('c*').encode("UTF-8", "EUC-JP").bytes] = [i, j]
rescue Encoding::UndefinedConversionError
# Ignore
end
end
end
# JIS X 0212
(0xA1..0xFE).each do |i|
(0xA1..0xFE).each do |j|
begin
euc["0x8f#{i.to_s(16).rjust(2, '0')}#{j.to_s(16).ljust(2, '0')}"] = [0x8F, i, j].pack('c*').encode("UTF-8", "EUC-JP")
utf[[0x8F, i, j].pack('c*').encode("UTF-8", "EUC-JP").bytes] = [0x8F, i, j]
rescue Encoding::UndefinedConversionError
# Ignore
end
end
end
puts "const EUC_TABLE = {"
euc.keys.each do |key|
puts " #{key}: #{euc[key].unpack('C*')},"
end
puts "};"
puts
puts "const UTF_TABLE = {"
utf.keys.each do |key|
hex = key.map { |n| '%02x' % (n & 0xFF) }.join
puts " 0x#{hex}: [#{utf[key].join(', ')}],"
end
puts "};"