-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutf8cp.lua
More file actions
135 lines (132 loc) · 5.67 KB
/
utf8cp.lua
File metadata and controls
135 lines (132 loc) · 5.67 KB
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
function codepage(cp)
local fn,i,tab,db,txt
i=128 tab={} db={
["1251"]=[[
0402 0403 201a 0453 201e 2026 2020 2021 20ac 2030 0409 2039 040a 040c 040b 040f
0452 2018 2019 201c 201d 2022 2013 2014 XXXX 2122 0459 203a 045a 045c 045b 045f
00a0 040e 045e 0408 00a4 0490 00a6 00a7 0401 00a9 0404 00ab 00ac 00ad 00ae 0407
00b0 00b1 0406 0456 0491 00b5 00b6 00b7 0451 2116 0454 00bb 0458 0405 0455 0457
0410 0411 0412 0413 0414 0415 0416 0417 0418 0419 041a 041b 041c 041d 041e 041f
0420 0421 0422 0423 0424 0425 0426 0427 0428 0429 042a 042b 042c 042d 042e 042f
0430 0431 0432 0433 0434 0435 0436 0437 0438 0439 043a 043b 043c 043d 043e 043f
0440 0441 0442 0443 0444 0445 0446 0447 0448 0449 044a 044b 044c 044d 044e 044f
]],
["866"]=[[
0410 0411 0412 0413 0414 0415 0416 0417 0418 0419 041A 041B 041C 041D 041E 041F
0420 0421 0422 0423 0424 0425 0426 0427 0428 0429 042A 042B 042C 042D 042E 042F
0430 0431 0432 0433 0434 0435 0436 0437 0438 0439 043A 043B 043C 043D 043E 043F
2591 2592 2593 2502 2524 2561 2562 2556 2555 2563 2551 2557 255D 255C 255B 2510
2514 2534 252C 251C 2500 253C 255E 255F 255A 2554 2569 2566 2560 2550 256C 2567
2568 2564 2565 2559 2558 2552 2553 256B 256A 2518 250C 2588 2584 258C 2590 2580
0440 0441 0442 0443 0444 0445 0446 0447 0448 0449 044A 044B 044C 044D 044E 044F
0401 0451 0404 0454 0407 0457 040E 045E 00B0 2219 00B7 221A 2116 00A4 25A0 00A0
]],
["koi8r"]=[[
2500 2502 250c 2510 2514 2518 251c 2524 252c 2534 253c 2580 2584 2588 258c 2590
2591 2592 2593 2320 25a0 2219 221a 2248 2264 2265 00a0 2321 00b0 00b2 00b7 00f7
2550 2551 2552 0451 2553 2554 2555 2556 2557 2558 2559 255a 255b 255c 255d 255e
255f 2560 2561 0401 2562 2563 2564 2565 2566 2567 2568 2569 256a 256b 256c 00a9
044e 0430 0431 0446 0434 0435 0444 0433 0445 0438 0439 043a 043b 043c 043d 043e
043f 044f 0440 0441 0442 0443 0436 0432 044c 044b 0437 0448 044d 0449 0447 044a
042e 0410 0411 0426 0414 0415 0424 0413 0425 0418 0419 041a 041b 041c 041d 041e
041f 042f 0420 0421 0422 0423 0416 0412 042c 042b 0417 0428 042d 0429 0427 042a
]],
-- ["koi8u"]=[[
-- 2500 2502 250c 2510 2514 2518 251c 2524 252c 2534 253c 2580 2584 2588 258c 2590
-- 2591 2592 2593 2320 25a0 2219 221a 2248 2264 2265 00a0 2321 00b0 00b2 00b7 00f7
-- 2550 2551 2552 0451 0454 2554 0456 0457 2557 2558 2559 255a 255b 0491 255d 255e
-- 255f 2560 2561 0401 0404 2563 0406 0407 2566 2567 2568 2569 256a 0490 256c 00a9
-- 044e 0430 0431 0446 0434 0435 0444 0433 0445 0438 0439 043a 043b 043c 043d 043e
-- 043f 044f 0440 0441 0442 0443 0436 0432 044c 044b 0437 0448 044d 0449 0447 044a
-- 042e 0410 0411 0426 0414 0415 0424 0413 0425 0418 0419 041a 041b 041c 041d 041e
-- 041f 042f 0420 0421 0422 0423 0416 0412 042c 042b 0417 0428 042d 0429 0427 042a
-- ]],
-- ["iso8859-5"]=[[
-- 0080 0081 0082 0083 0084 0085 0086 0087 0088 0089 008a 008b 008c 008d 008e 008f
-- 0090 0091 0092 0093 0094 0095 0096 0097 0098 0099 009a 009b 009c 009d 009e 009f
-- 00a0 0401 0402 0403 0404 0405 0406 0407 0408 0409 040a 040b 040c 00ad 040e 040f
-- 0410 0411 0412 0413 0414 0415 0416 0417 0418 0419 041a 041b 041c 041d 041e 041f
-- 0420 0421 0422 0423 0424 0425 0426 0427 0428 0429 042a 042b 042c 042d 042e 042f
-- 0430 0431 0432 0433 0434 0435 0436 0437 0438 0439 043a 043b 043c 043d 043e 043f
-- 0440 0441 0442 0443 0444 0445 0446 0447 0448 0449 044a 044b 044c 044d 044e 044f
-- 2116 0451 0452 0453 0454 0455 0456 0457 0458 0459 045a 045b 045c 00a7 045e 045f
-- ]],
-- ["mac-cyrillic"]=[[
-- 0410 0411 0412 0413 0414 0415 0416 0417 0418 0419 041a 041b 041c 041d 041e 041f
-- 0420 0421 0422 0423 0424 0425 0426 0427 0428 0429 042a 042b 042c 042d 042e 042f
-- 2020 00b0 0490 00a3 00a7 2022 00b6 0406 00ae 00a9 2122 0402 0452 2260 0403 0453
-- 221e 00b1 2264 2265 0456 00b5 0491 0408 0404 0454 0407 0457 0409 0459 040a 045a
-- 0458 0405 00ac 221a 0192 2248 2206 00ab 00bb 2026 00a0 040b 045b 040c 045c 0455
-- 2013 2014 201c 201d 2018 2019 00f7 201e 040e 045e 040f 045f 2116 0401 0451 044f
-- 0430 0431 0432 0433 0434 0435 0436 0437 0438 0439 043a 043b 043c 043d 043e 043f
-- 0440 0441 0442 0443 0444 0445 0446 0447 0448 0449 044a 044b 044c 044d 044e 20ac
-- ]],
}
if db[cp] then txt=db[cp] else
fn="cp/"..cp..".txt"
f=io.open(fn,"r") or error("unable read file "..fn)
txt=f:read("a")
f:close()
end
txt:gsub("%w+",function(x) local c=tonumber(x,16) if c then tab[c]=i end i=i+1 end)
return tab
end
function utf8_to(cp,unk)
local i,tab,txt
cp=cp or "1251"
tab=codepage(cp)
local function utf8decode(text)
local s={text=text,pos=1,len=#text}
local u2c=function()
local r,n,m
if s.pos>s.len then return nil end
r=s.text:byte(s.pos)
s.pos=s.pos+1
if r<128 or r>=254 then return r end
if r<192 then error "invalid utf8" end
n=0 m=64 repeat m=m>>1 n=n+1 until 0==(r&m)
r=r&(m-1) -- if r==0 then error "suspecious coding" end
s.n=n+1
while n>0 do
n=n-1 r=r<<6
if s.pos>s.len then m=0 else
m=s.text:byte(s.pos)
s.pos=s.pos+1
end
if m<128 or m>=192 then error "invalid utf8" else r=r|(m-128) end
end
return r
end
return u2c()
end
unk=unk or function(c) return string.format("&#%d;",c) end
if type(unk)~="function" then
txt=unk
unk=function(c) return txt end
end
return function(utf8)
local res=utf8:gsub("[\192-\255]+[\128-\191]*",function(x)
local c=utf8decode(x)
local r=tab[c] if r==nil then return unk(c) end
return string.char(r)
end)
return res
end
end
function utf8_from(cp,unk)
local tab={}
for u,c in pairs(codepage(cp)) do tab[c]=u end
if type(unk)~="function" then
local ch=unk or "?"
unk=function(c) return ch end
end
return function(s)
return (s:gsub(".",function(c)
c=string.byte(c,1)
local u=tab[c]
if u then return utf8.char(u) end
if c>127 then return unk(c) end
return string.char(c)
end))
end
end