是否有使用 Lua C-API 连接两个 LuaL_Buffer
的有效方法?我宁愿不做任何不必要的 memcopy 并希望 luaL_pushresult()
使用结果。缓冲区包含嵌入的零,因此我无法将它们转换为字符数组并使用 luaL_addstring()
。修改任一缓冲区都是可以接受的。
luaL_Buffer buf1;
luaL_Buffer buf2;
luaL_buffinit(L, &buf1);
luaL_buffinit(L, &buf2);
luaL_addchar(&buf1, "a");
luaL_addchar(&buf2, "b");
luaL_addchar(&buf1, "\0");
luaL_addchar(&buf2, "\0");
luaL_pushresult(L, Want_this(&buf1, &buf2) ); // "a\0b\0" is now the Lua string
// at the top of the stack
最佳答案
您可以先将 buf2
压入堆栈,然后将其添加到 buf1
(弹出它),然后将 buf1
压入堆栈。
luaL_pushresult(L, &buf2); // push "b\0" onto the stack
luaL_addvalue(&buf1); // pop that string and add it to buf1
luaL_pushresult(L, &buf1); // push "a\0b\0" onto the stack
关于c - 如何连接两个 Lua lua L_Buffer 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11380144/