Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string_decode does not work on lua 5.3 #12

Open
learn-more opened this issue Oct 11, 2020 · 1 comment
Open

string_decode does not work on lua 5.3 #12

learn-more opened this issue Oct 11, 2020 · 1 comment
Labels

Comments

@learn-more
Copy link

LuaError
"./lua_runtime/string_decode.lua:19: attempt to index a nil value (global 'bit32')
stack traceback:
	./lua_runtime/string_decode.lua:19: in upvalue 'utf8_to_32'
	./lua_runtime/string_decode.lua:38: in function 'string_decode.decode'
	./zip.lua:"

bit32 is deprecated in lua 5.3

@MegaPiggy
Copy link
Contributor

Lua 5.3+ uses bitwise operators instead of bit32.
Here is an edited version of the string_decode file.

--
-- String decoder functions
--

local stringdecode = {}

-- From http://lua-users.org/wiki/LuaUnicode
local function utf8_to_32(utf8str)
    assert(type(utf8str) == "string")
    local res, seq, val = {}, 0, nil

    for i = 1, #utf8str do
        local c = string.byte(utf8str, i)
        if seq == 0 then
            table.insert(res, val)
            seq = c < 0x80 and 1 or c < 0xE0 and 2 or c < 0xF0 and 3 or
                  c < 0xF8 and 4 or --c < 0xFC and 5 or c < 0xFE and 6 or
                error("Invalid UTF-8 character sequence")
            val = c & (2^(8-seq) - 1)
        else
            val = (val << 6) | (c & 0x3F)
        end

        seq = seq - 1
    end

    table.insert(res, val)

    return res
end

function stringdecode.decode(str, encoding)
    local enc = encoding and encoding:lower() or "ascii"

    if enc == "ascii" then
        return str
    elseif enc == "utf-8" then
        local code_points = utf8_to_32(str)

        return utf8.char(table.unpack(code_points))
    else
        error("Encoding " .. encoding .. " not supported")
    end
end

return stringdecode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants