-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathtest_iconv.lua
135 lines (117 loc) · 6.65 KB
/
test_iconv.lua
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
-- -*- coding: utf-8 -*-
local iconv = require("iconv")
-- Set your terminal encoding here
-- local termcs = "iso-8859-1"
local termcs = "utf-8"
local iso88591 = "\65\111\32\108\111\110\103\101\44\32\97\111\32\108\117"
.. "\97\114\10\78\111\32\114\105\111\32\117\109\97\32\118\101\108\97\10\83"
.. "\101\114\101\110\97\32\97\32\112\97\115\115\97\114\44\10\81\117\101\32"
.. "\233\32\113\117\101\32\109\101\32\114\101\118\101\108\97\10\10\78\227"
.. "\111\32\115\101\105\44\32\109\97\115\32\109\117\101\32\115\101\114\10"
.. "\84\111\114\110\111\117\45\115\101\45\109\101\32\101\115\116\114\97\110"
.. "\104\111\44\10\69\32\101\117\32\115\111\110\104\111\32\115\101\109\32"
.. "\118\101\114\10\79\115\32\115\111\110\104\111\115\32\113\117\101\32\116"
.. "\101\110\104\111\46\10\10\81\117\101\32\97\110\103\250\115\116\105\97"
.. "\32\109\101\32\101\110\108\97\231\97\63\10\81\117\101\32\97\109\111\114"
.. "\32\110\227\111\32\115\101\32\101\120\112\108\105\99\97\63\10\201\32\97"
.. "\32\118\101\108\97\32\113\117\101\32\112\97\115\115\97\10\78\97\32\110"
.. "\111\105\116\101\32\113\117\101\32\102\105\99\97\10\10\32\32\32\32\45"
.. "\45\32\70\101\114\110\97\110\100\111\32\80\101\115\115\111\97\10"
local utf8 = "\65\111\32\108\111\110\103\101\44\32\97\111\32\108\117\97\114"
.. "\10\78\111\32\114\105\111\32\117\109\97\32\118\101\108\97\10\83\101\114"
.. "\101\110\97\32\97\32\112\97\115\115\97\114\44\10\81\117\101\32\195\169\32"
.. "\113\117\101\32\109\101\32\114\101\118\101\108\97\10\10\78\195\163\111\32"
.. "\115\101\105\44\32\109\97\115\32\109\117\101\32\115\101\114\10\84\111\114"
.. "\110\111\117\45\115\101\45\109\101\32\101\115\116\114\97\110\104\111\44"
.. "\10\69\32\101\117\32\115\111\110\104\111\32\115\101\109\32\118\101\114\10"
.. "\79\115\32\115\111\110\104\111\115\32\113\117\101\32\116\101\110\104\111"
.. "\46\10\10\81\117\101\32\97\110\103\195\186\115\116\105\97\32\109\101\32"
.. "\101\110\108\97\195\167\97\63\10\81\117\101\32\97\109\111\114\32\110\195"
.. "\163\111\32\115\101\32\101\120\112\108\105\99\97\63\10\195\137\32\97\32"
.. "\118\101\108\97\32\113\117\101\32\112\97\115\115\97\10\78\97\32\110\111"
.. "\105\116\101\32\113\117\101\32\102\105\99\97\10\10\32\32\32\32\45\45\32"
.. "\70\101\114\110\97\110\100\111\32\80\101\115\115\111\97\10"
local utf16 = "\255\254\65\0\111\0\32\0\108\0\111\0\110\0\103\0\101\0\44\0\32"
.. "\0\97\0\111\0\32\0\108\0\117\0\97\0\114\0\10\0\78\0\111\0\32\0\114\0\105"
.. "\0\111\0\32\0\117\0\109\0\97\0\32\0\118\0\101\0\108\0\97\0\10\0\83\0\101"
.. "\0\114\0\101\0\110\0\97\0\32\0\97\0\32\0\112\0\97\0\115\0\115\0\97\0\114"
.. "\0\44\0\10\0\81\0\117\0\101\0\32\0\233\0\32\0\113\0\117\0\101\0\32\0\109"
.. "\0\101\0\32\0\114\0\101\0\118\0\101\0\108\0\97\0\10\0\10\0\78\0\227\0\111"
.. "\0\32\0\115\0\101\0\105\0\44\0\32\0\109\0\97\0\115\0\32\0\109\0\117\0\101"
.. "\0\32\0\115\0\101\0\114\0\10\0\84\0\111\0\114\0\110\0\111\0\117\0\45\0\115"
.. "\0\101\0\45\0\109\0\101\0\32\0\101\0\115\0\116\0\114\0\97\0\110\0\104\0"
.. "\111\0\44\0\10\0\69\0\32\0\101\0\117\0\32\0\115\0\111\0\110\0\104\0\111"
.. "\0\32\0\115\0\101\0\109\0\32\0\118\0\101\0\114\0\10\0\79\0\115\0\32\0\115"
.. "\0\111\0\110\0\104\0\111\0\115\0\32\0\113\0\117\0\101\0\32\0\116\0\101\0"
.. "\110\0\104\0\111\0\46\0\10\0\10\0\81\0\117\0\101\0\32\0\97\0\110\0\103\0"
.. "\250\0\115\0\116\0\105\0\97\0\32\0\109\0\101\0\32\0\101\0\110\0\108\0\97"
.. "\0\231\0\97\0\63\0\10\0\81\0\117\0\101\0\32\0\97\0\109\0\111\0\114\0\32"
.. "\0\110\0\227\0\111\0\32\0\115\0\101\0\32\0\101\0\120\0\112\0\108\0\105\0"
.. "\99\0\97\0\63\0\10\0\201\0\32\0\97\0\32\0\118\0\101\0\108\0\97\0\32\0\113"
.. "\0\117\0\101\0\32\0\112\0\97\0\115\0\115\0\97\0\10\0\78\0\97\0\32\0\110"
.. "\0\111\0\105\0\116\0\101\0\32\0\113\0\117\0\101\0\32\0\102\0\105\0\99\0"
.. "\97\0\10\0\10\0\32\0\32\0\32\0\32\0\45\0\45\0\32\0\70\0\101\0\114\0\110"
.. "\0\97\0\110\0\100\0\111\0\32\0\80\0\101\0\115\0\115\0\111\0\97\0\10\0"
-- Bizarre EBCDIC-CP-ES encoding.
local ebcdic = "\193\150\64\147\150\149\135\133\107\64\129\150\64\147\164\129"
.. "\153\37\213\150\64\153\137\150\64\164\148\129\64\165\133\147\129\37\226"
.. "\133\153\133\149\129\64\129\64\151\129\162\162\129\153\107\37\216\164\133"
.. "\64\81\64\152\164\133\64\148\133\64\153\133\165\133\147\129\37\37\213\70"
.. "\150\64\162\133\137\107\64\148\129\162\64\148\164\133\64\162\133\153\37"
.. "\227\150\153\149\150\164\96\162\133\96\148\133\64\133\162\163\153\129\149"
.. "\136\150\107\37\197\64\133\164\64\162\150\149\136\150\64\162\133\148\64"
.. "\165\133\153\37\214\162\64\162\150\149\136\150\162\64\152\164\133\64\163"
.. "\133\149\136\150\75\37\37\216\164\133\64\129\149\135\222\162\163\137\129"
.. "\64\148\133\64\133\149\147\129\72\129\111\37\216\164\133\64\129\148\150"
.. "\153\64\149\70\150\64\162\133\64\133\167\151\147\137\131\129\111\37\113"
.. "\64\129\64\165\133\147\129\64\152\164\133\64\151\129\162\162\129\37\213"
.. "\129\64\149\150\137\163\133\64\152\164\133\64\134\137\131\129\37\37\64\64"
.. "\64\64\96\96\64\198\133\153\149\129\149\132\150\64\215\133\162\162\150\129"
.. "\37"
local function ErrMsg(errno)
if errno == iconv.ERROR_INCOMPLETE then
return "Incomplete input."
elseif errno == iconv.ERROR_INVALID then
return "Invalid input."
elseif errno == iconv.ERROR_NO_MEMORY then
return "Failed to allocate memory."
elseif errno == iconv.ERROR_UNKNOWN then
return "There was an unknown error."
elseif errno == iconv.FINALIZED then
return "Handle was already finalized."
end
return "Unknown error: "..tostring(errno)
end
local function check_one(to, from, text)
print("\n-- Testing conversion from " .. from .. " to " .. to)
local cd, errno = iconv.new(to .. "//TRANSLIT", from)
assert(cd, "Failed to create a converter object: " .. ErrMsg(errno))
local ostr, err = cd:iconv(text)
if err then
print("ERROR: " .. ErrMsg(err))
end
print(ostr)
end
check_one(termcs, "iso-8859-1", iso88591)
check_one(termcs, "utf8", utf8)
check_one(termcs, "utf16", utf16)
check_one(termcs, "EBCDIC-CP-ES", ebcdic)
-- The library must never crash the interpreter, even if the user tweaks
-- with the garbage collector methods.
local _, e, cd, s, gc
cd = iconv.new("iso-8859-1", "utf-8")
_, e = cd:iconv("atenção")
assert(e == nil, "Unexpected conversion error")
gc = getmetatable(cd).__gc
gc(cd)
_, e = cd:iconv("atenção")
assert(e == iconv.ERROR_FINALIZED, "Failed to detect double-freed objects")
gc(cd)
-- Test expected return values
cd = iconv.new("ascii", "utf-8")
_, e = cd:iconv("atenção")
assert(e == iconv.ERROR_INVALID, "Unexpected return value for invalid conversion")
cd = iconv.new("iso-8859-1", "utf-8")
s, e = cd:iconv("atenção")
assert(s == "aten\231\227o", "Unexpected result for valid conversion")
assert(e == nil, "Unexpected return value for valid conversion")