使用Lua C-API有没有一种有效的方法连接两个LuaL_Buffers?我不想做任何不必要的memcopy,希望luaL_pushresult()消耗结果。缓冲区包含嵌入的零,因此我无法将它们转换为char数组并使用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

10-01 07:00