lua-users home
lua-l archive

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


Le lun. 16 nov. 2020 à 10:38, Ranier Vilela <ranier.vf@gmail.com> a écrit :
> I still want to hear the Lua Team.
> But if that is really the case, it is simple to resolve and continue to avoid calling strlen over and over again.
> +if (strncmp(ar.namewhat, "method\0", 7) == 0) {

I'm not the Lua Team, but : you seem to assume strcmp calls strlen,
and that strncmp is an optimization of strcmp. That is not the case :
* strcmp compares chars one by one, and stops when a difference is
encountered, or \0 is encountered. strlen is *not* called.
* strncmp compares chars one by one, and stops when a difference is
encountered, or n chars have been compared.

So using strncmp won't reduce the number of chars compared. It will
however make the code less readable, and more brittle (you have to
make sure the magic number 7 is correct, and stays in sync with the
string literal in case of changes).

-- 
Julien Cugnière