Search code examples
clualua-api

How to limit the amount of characters in a Lua string


I have a question: how do I make the lua_tostring function limit the amount of entries.

For example 8 characters limit:

poppy_name = "command"; // OK, within the limit;
poppy_name = "commander"; // Fail, Is out of range, show a message the permitted limit.

I searched I couldn't find anymore, is there a way to limit the lua_tostring?


Solution

  • Use lua_tolstring to obtain the string length, then use lua_error or luaL_error if the string is too long.

    size_t arg_len;
    lua_tolstring(L, 1, &arg_len);
    if (arg_len > 8) return luaL_error(L, "argument too long");