lua-users home
lua-l archive

Re: C API to check if string is valid varible name

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Dne 2023年03月24日 14:29, Roberto Ierusalimschy napsal:
> For instance?
I have lua embedded in system where users can define table of variables and
then write expressions to process them. However i think it would be
reasonable to have some sanity checks in place, so i don't have to explain why formulas using variable called "TIME-MAX" are not working :-) and stuff
like that...
Up to reserved words, the Lua pattern "^[%a_][%w_]*$" should do the job.
-- Roberto
So just to give happy end to this thread, thanks for your sugestion.
I've ended using this C code, probably will simplify it later:
int lua_string_match(lua_State *LUA, const char *string, const char *pattern) {
 int ret;
 lua_getglobal(LUA, "string");
 lua_getfield(LUA, -1, "match");
 lua_pushstring(LUA, string);
 lua_pushstring(LUA, pattern);
 lua_call(LUA, 2, 1); //2args, 1ret
 ret = lua_toboolean(LUA, -1);
 lua_pop(LUA, lua_gettop(LUA));
 return ret;
}
int lua_check_variable_name(lua_State *LUA, const char *string) {
return lua_string_match(LUA, string, "^[%a_][%w_]*$"); //TODO: Prepsat do lua souboru???
}
--
S pozdravem
Best regards
 Tomáš Mudruňka - SPOJE.NET s.r.o.

AltStyle によって変換されたページ (->オリジナル) /