lua源码问题,""s的用法
lua source code problem, the usage of "" s
我最近在学习lua源代码。我对 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;
}
}
在 C 中,只需将字符串文字并排放置即可将它们连接起来,因此 "foo" "bar" "baz"
等同于 "foobarbaz"
,但将字符串文字放在其他任何内容旁边,例如 "foo"str
, 是编译器错误。通过在宏中执行 "" s
,他们确保宏仅在 s
是字符串文字而不是变量或其他东西时才有效。
我最近在学习lua源代码。我对 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;
}
}
在 C 中,只需将字符串文字并排放置即可将它们连接起来,因此 "foo" "bar" "baz"
等同于 "foobarbaz"
,但将字符串文字放在其他任何内容旁边,例如 "foo"str
, 是编译器错误。通过在宏中执行 "" s
,他们确保宏仅在 s
是字符串文字而不是变量或其他东西时才有效。