Re: [PATCH] Refactor "if(strspn(str, ...) == strlen(str)" code

Поиск
Список
Период
Сортировка
От Geoff Winkless
Тема Re: [PATCH] Refactor "if(strspn(str, ...) == strlen(str)" code
Дата
Msg-id CAEzk6ffaryoC=REYR=9fyGq9WEN-bqyDfzzVrY1xzeyZT5o2LA@mail.gmail.com
обсуждение исходный текст
Ответ на [PATCH] Refactor "if(strspn(str, ...) == strlen(str)" code  (Aleksander Alekseev <a.alekseev@postgrespro.ru>)
Список pgsql-hackers
On 8 December 2016 at 15:54, Aleksander Alekseev
<a.alekseev@postgrespro.ru> wrote:
> Hi.
>
> I noticed that there is a lot of repeating code like this:
>
> ```
> if (strspn(str, " \t\n\r\f") == strlen(str))
> ```
>
> I personally don't find it particularly readable, not mentioning that
> traversing a string twice doesn't look as a good idea (you can check
> using objdump that latest GCC 6.2 doesn't optimize this code).

You could just change it to

if (str[strspn(str, " \t\n\r\f")] == '\0')

to mitigate calling strlen. It's safe to do so because strspn will
only return values from 0 to strlen(str).

Geoff



В списке pgsql-hackers по дате отправления:

Предыдущее
От: Heikki Linnakangas
Дата:
Сообщение: Re: Use EVP API pgcrypto encryption, dropping support for OpenSSL 0.9.6 and older
Следующее
От: Asif Naeem
Дата:
Сообщение: Re: pgcrypto compilation error due to stack-allocated EVP_CIPHER_CTX