-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathex11.lua
25 lines (23 loc) · 786 Bytes
/
ex11.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
-- Example 11. Handle string encoding errors
--8<----------------------------------------------------------------------------
filename = '/home/mitchell/code/textadept/init.lua'
toutf8 = function() error('conversion failed') end
--toutf8 = function() return 'utf-8' end
--8<----------------------------------------------------------------------------
local encodings = {
"UTF-8", "UTF-16", "UTF-32", "ASCII", "ISO-8859-1"
}
local f = io.open(filename, "r")
local text = f:read("a")
f:close()
for i = 1, #encodings do
-- Attempt to convert file contents to UTF-8.
local ok, conv = pcall(toutf8, text, encodings[i])
if ok then
text = conv
goto encoding_detected
end
end
error("Could not detect file encoding.")
::encoding_detected::
--[[ process UTF-8-encoded text ]]