This function:
static int function_name(lua_State *L) {
const char* str = lua_tostring(L, 1);
const char* substr = lua_tostring(L, 2); // passing "how" results in "how-" ???
char *pch = strstr(str, substr);
if (pch != NULL)
lua_pushinteger(L, pch - str);
else
lua_pushnil(L);
return 1;
}
Invoking the function (i.e, function_name("hello how world", "how"
) produces this result. Why would lua_tostring
return such an obfuscated result? This issue only occurs when I pass a string equal to the length of the substring I want to match to. For example
"hello how world", "hell" -> works fine
"hello how world", "hello" -> fails, thinks "hello-" is passed.
"hello how world", "ho" -> works fine
"hello how world", "how" -> fails, thinks "how-" is passed.
I'm printing substr
immediately after I pull it from the stack to observe results. I don't believe any middle-men could mess it up.