RosettaCodeData/Task/Read-a-file-character-by-character-UTF8/Lua/read-a-file-character-by-character-utf8-2.lua
2026-04-30 12:34:36 -04:00

22 lines
476 B
Lua
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function read_rune (T)
local R
if io.type(T.file)~="closed file" then
for c in T.file:lines(1) do
T.buffer = (T.buffer or "") .. c
if utf8.len(T.buffer)==1 then
T.buffer, R = nil, T.buffer
return R
end
end
end
R, T.buffer = T.buffer, nil
return R
end
local file = io.tmpfile()
file:write("𝄞AöЖ€𝄞Ελληνικάy䮀成长汉\n")
file:seek"set"
for rune in read_rune, {file=file} do
io.write(rune, " ")
end