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

From Aleksander Alekseev
Subject [PATCH] Refactor "if(strspn(str, ...) == strlen(str)" code
Date
Msg-id 20161208155408.GA27729@e733.localdomain
Whole thread Raw
Responses Re: [PATCH] Refactor "if(strspn(str, ...) == strlen(str)" code  (Geoff Winkless <pgsqladmin@geoff.dj>)
Re: [PATCH] Refactor "if(strspn(str, ...) == strlen(str)" code  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
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).

How about rewriting such a code like this?

```
if (pg_str_containsonly(str, " \t\n\r\f"))
```

Corresponding patch is attached. I don't claim that my implementation of
pg_str_containsonly procedure is faster that strspn + strlen, but at
least such refactoring makes code a little more readable.

--
Best regards,
Aleksander Alekseev

Attachment

pgsql-hackers by date:

Previous
From: Christoph Berg
Date:
Subject: Re: Use EVP API pgcrypto encryption, dropping support for OpenSSL 0.9.6 and older
Next
From: Heikki Linnakangas
Date:
Subject: Re: Use EVP API pgcrypto encryption, dropping support for OpenSSL 0.9.6 and older