Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lua c api: how to push a string with a null character in the middle?

Tags:

c

api

lua

normally, I would just use

lua_pushstring(lua_State* L, const char* s);

however, the string I want to push might have a null character in it. How do I make that work?

like image 445
anon Avatar asked Dec 18 '22 00:12

anon


1 Answers

Use lua_pushlstring.

void lua_pushlstring (lua_State *L, const char *s, size_t len);

Pushes the string pointed to by s with size len onto the stack. Lua makes (or reuses) an internal copy of the given string, so the memory at s can be freed or reused immediately after the function returns. The string can contain embedded zeros.

like image 128
Amber Avatar answered Jan 12 '23 01:01

Amber