lua-users home
lua-l archive

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


Em seg., 16 de nov. de 2020 às 07:34, Julien Cugnière <julien.cugniere@gmail.com> escreveu:
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) {

* strncmp compares chars one by one, and stops when a difference is
encountered, or n chars have been compared.
Exactly. That's what strncmp was created for ("n chars have been compared").
I usually use sizeof, but it seems to me that Lua Team likes numerals more.

I usually use it like this:
+  if (strncmp(ar.namewhat, "method", sizeof("method")) == 0) { /* to compare exactly "method" */
or
+  if (strncmp(ar.namewhat, "method", sizeof("method") - 1) == 0) { /* to compare any "method" */


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).
sizeof makes it more readable. Nginx, uses a lot.

regards,
Ranier Vilela