Reputation: 3091
Using the Lua C-API is there an efficient way to concatenate two LuaL_Buffer
s? I'd rather not do any unnecessary memcopys and want the result consumed by luaL_pushresult()
. The buffers contain embedded zeros so I can't convert them to char arrays and use luaL_addstring()
. It is acceptable to modify either buffer.
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
Upvotes: 1
Views: 455
Reputation: 28991
You could push buf2
onto the stack first, add it to buf1
(which pops it), then push buf1
onto the stack.
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
Upvotes: 2
Reputation: 9340
Create the whole string at C level instead and use luaL_addlstring
, this way null character can be safely added to the buffer.
Upvotes: 2