Re: [PATCH] Refactor "if(strspn(str, ...) == strlen(str)" code - Mailing list pgsql-hackers

From Geoff Winkless
Subject Re: [PATCH] Refactor "if(strspn(str, ...) == strlen(str)" code
Date
Msg-id CAEzk6ffaryoC=REYR=9fyGq9WEN-bqyDfzzVrY1xzeyZT5o2LA@mail.gmail.com
Whole thread Raw
In response to [PATCH] Refactor "if(strspn(str, ...) == strlen(str)" code  (Aleksander Alekseev <a.alekseev@postgrespro.ru>)
List 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 by date:

Previous
From: Heikki Linnakangas
Date:
Subject: Re: Use EVP API pgcrypto encryption, dropping support for OpenSSL 0.9.6 and older
Next
From: Asif Naeem
Date:
Subject: Re: pgcrypto compilation error due to stack-allocated EVP_CIPHER_CTX