lua-users home
lua-l archive

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

Em qui., 26 de nov. de 2020 às 08:05, eugeny gladkih <> escreveu:

On 21 Nov 2020, at 20:30, Ranier Vilela <> wrote:

Em sáb., 21 de nov. de 2020 às 13:13, Roberto Ierusalimschy <> escreveu:
> anyway, usage of lua_pushlstring instead of lua_pushstring is much better.

 For literal strings, lua_pushstring is both simpler to use and faster. 
lua_pushstring(L, "cannot close standard file");
Can be simpler, but it is not faster than:

/* avoid strlen call or inline */
lua_pushlstring(L, "cannot close standard file", sizeof("cannot close standard file") - 1);

Another example:
void luaT_init (lua_State *L) {
  static const char *const luaT_eventname[] = {  /* ORDER TM */
    "__index", "__newindex",
    "__gc", "__mode", "__len", "__eq",
    "__add", "__sub", "__mul", "__mod", "__pow",
    "__div", "__idiv",
    "__band", "__bor", "__bxor", "__shl", "__shr",
    "__unm", "__bnot", "__lt", "__le",
    "__concat", "__call", "__close"
  unsigned int i;
  for (i=0; i<TM_N; ++i) {
    G(L)->tmname[i] = luaS_newlstr(L, luaT_eventname[i], sizeof(luaT_eventname[i]) - 1); /* build-time size */

wow… you’ll have the same “length” for all strings here 3 or 7
You are right, the use of sizeof in this case, it is totally wrong.

Ranier Vilela