lua source code problem, the usage of "" s

Viewed 63

I am learning lua source code these days. And I am confused about the usage of "" s in luaS_newlstr(L, "" s, (sizeof(s)/sizeof(char))-1)

#define luaS_newliteral(L, s)   (luaS_newlstr(L, "" s, (sizeof(s)/sizeof(char))-1))

TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
  if (l <= LUAI_MAXSHORTLEN)  /* short string? */
    return internshrstr(L, str, l);
  else {
    TString *ts;
    if (unlikely(l >= (MAX_SIZE - sizeof(TString))/sizeof(char)))
      luaM_toobig(L);
    ts = luaS_createlngstrobj(L, l);
    memcpy(getstr(ts), str, l * sizeof(char));
    return ts;
  }
}
1 Answers

In C, just putting string literals next to each other concatenates them, so "foo" "bar" "baz" is equivalent to "foobarbaz", but putting a string literal next to anything else, like "foo"str, is a compiler error. By doing "" s in a macro, they're making sure that the macro only works if s is a string literal, and not a variable or something.

Related