-
Notifications
You must be signed in to change notification settings - Fork 5
/
jis_table.rb
71 lines (61 loc) · 1.93 KB
/
jis_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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
jis = {}
utf = {}
# ASCII Compatible
(0x00..0x7F).each do |i|
jis["0x#{i.to_s(16).rjust(2, '0')}"] = [i].pack('c*').encode("UTF-8", "Shift_JIS")
utf[[i].pack('c*').encode("UTF-8", "Shift_JIS").bytes] = [i]
end
# Half-width Hiragana
(0xA1..0xDF).each do |i|
jis["0x#{i.to_s(16).rjust(2, '0')}"] = [i].pack('c*').encode("UTF-8", "Shift_JIS")
utf[[i].pack('c*').encode("UTF-8", "Shift_JIS").bytes] = [i]
end
# JIS X 0208
(0x81..0x9F).each do |i|
(0x40..0x7E).each do |j|
begin
jis["0x#{i.to_s(16).rjust(2, '0')}#{j.to_s(16).rjust(2, '0')}"] = [i, j].pack('c*').encode("UTF-8", "SJIS")
utf[[i, j].pack('c*').encode("UTF-8", "Shift_JIS").bytes] = [i, j]
rescue Encoding::UndefinedConversionError
# Ignore
end
end
(0x80..0xFC).each do |j|
begin
jis["0x#{i.to_s(16).rjust(2, '0')}#{j.to_s(16).rjust(2, '0')}"] = [i, j].pack('c*').encode("UTF-8", "SJIS")
utf[[i, j].pack('c*').encode("UTF-8", "Shift_JIS").bytes] = [i, j]
rescue Encoding::UndefinedConversionError
# Ignore
end
end
end
(0xE0..0xEF).each do |i|
(0x40..0x7E).each do |j|
begin
jis["0x#{i.to_s(16).rjust(2, '0')}#{j.to_s(16).rjust(2, '0')}"] = [i, j].pack('c*').encode("UTF-8", "Shift_JIS")
utf[[i, j].pack('c*').encode("UTF-8", "Shift_JIS").bytes] = [i, j]
rescue Encoding::UndefinedConversionError
# Ignore
end
end
(0x80..0xFC).each do |j|
begin
jis["0x#{i.to_s(16).rjust(2, '0')}#{j.to_s(16).rjust(2, '0')}"] = [i, j].pack('c*').encode("UTF-8", "Shift_JIS")
utf[[i, j].pack('c*').encode("UTF-8", "Shift_JIS").bytes] = [i, j]
rescue Encoding::UndefinedConversionError
# Ignore
end
end
end
puts "const JIS_TABLE = {"
jis.keys.each do |key|
puts " #{key}: #{jis[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 "};"