I was reading the source code of the Lua programming language lately and found some of the conventions unclear or confusing. For example, the str_reverse function in this file. Is it considered as clean code and understandable code in C ?
static int str_reverse (lua_State *L) {
size_t l, i;
luaL_Buffer b;
const char *s = luaL_checklstring(L, 1, &l);
char *p = luaL_buffinitsize(L, &b, l);
for (i = 0; i < l; i++)
p[i] = s[l - i - 1];
luaL_pushresultsize(&b, l);
return 1;
}
In these lines:
char *p = luaL_buffinitsize(L, &b, l);
for (i = 0; i < l; i++)
p[i] = s[l - i - 1];
*p is created to store the value of string after being reversed. After that, i don't see it is used anywhere else. Then looking into char *p = luaL_buffinitsize(L, &b, l);
, it shows that *p is a property of luaL_Buffer b;
, which is now making sense why *p is not used anymore, because in the next line, b
is in use
Also, in this line const char *s = luaL_checklstring(L, 1, &l);
, this luaL_checklstring
function return *s
, but also, assign length of string(got from L) to size_t l
variable. So this function not only return 1 variable and but also change 1 variable along its way.