Lua_isstring () check real strings in Lua

int lua_isstring (lua_State *L, int index);

This function returns 1 if the value with the given valid index is a string or a number (which is always converted to a string) and 0 otherwise. ( Source )

Is there a (more elegant) way to really prove if a given string is really a string and not a number in Lua? This function does not make any sense to me!

My first idea is to further examine the length of the string with

 `if(string.len(String) > 1) {/* this must be a string */}`

... but that's not so good.

+4
source share
2 answers

You can replace

lua_isstring(L, i)

which returns true for a string or number using

lua_type(L, i) == LUA_TSTRING

true .

,

lua_isnumber(L, i)

true , , ; ,

lua_type(L, i) == LUA_TNUMBER

( , lua_isstring_strict() lua_isnumber_strict().)

+3

!

Lua . , , , . . lua_isstring lua_tostring C - .

, LUA_NOCVTS2N / LUA_NOCVTN2S . , LUA_NOCVTN2S, lua_isstring false .

+2

Source: https://habr.com/ru/post/1609494/


All Articles