C
この関数を関数にLua
変換する必要があります
単純なプロジェクトを LuaJIT に移植しています。移植は 99% 完了していますが、この関数には問題があります。私は何が欠けていますか?
/**
* X-Or. Does a bit-a-bit exclusive-or of two strings.
* @param s1: arbitrary binary string.
* @param s2: arbitrary binary string with same length as s1.
* @return a binary string with same length as s1 and s2,
* where each bit is the exclusive-or of the corresponding bits in s1-s2.
*/
static int ex_or (lua_State *L) {
size_t l1, l2;
const char *s1 = luaL_checklstring(L, 1, &l1);
const char *s2 = luaL_checklstring(L, 2, &l2);
luaL_Buffer b;
luaL_argcheck( L, l1 == l2, 2, "lengths must be equal" );
luaL_buffinit(L, &b);
while (l1--) luaL_putchar(&b, (*s1++)^(*s2++));
luaL_pushresult(&b);
return 1;
}
現時点で、私の現在のLua
機能は次のとおりです。
local bit = require('bit')
function ex_or(arg1, arg2)
if #arg1 == #arg2 then
local result = ""
local index = 1
while index <= #arg1 do
result = result .. bit.bxor(string.byte(arg1, index), string.byte(arg2, index))
index = index + 1
end
return result
else
error("lengths must be equal.")
end
end
前もって感謝します