1

Lua C-API を使用して 2 つLuaL_Bufferの s を連結する効率的な方法はありますか? 不要な 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
4

2 に答える 2

2

代わりに C レベルで文字列全体を作成し、 を使用しますluaL_addlstring。このようにして、null 文字をバッファに安全に追加できます。

于 2012-07-08T02:27:13.943 に答える
2

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
于 2012-07-08T02:28:43.307 に答える