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

From: Aleksander Alekseev <a(dot)alekseev(at)postgrespro(dot)ru>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: [PATCH] Refactor "if(strspn(str, ...) == strlen(str)" code
Date: 2016-12-08 15:54:08
Message-ID: 20161208155408.GA27729@e733.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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 Content-Type Size
pg_str_containsonly-v1.patch text/x-diff 12.8 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Heikki Linnakangas 2016-12-08 16:04:37 Re: Use EVP API pgcrypto encryption, dropping support for OpenSSL 0.9.6 and older
Previous Message Christoph Berg 2016-12-08 15:51:34 Re: Use EVP API pgcrypto encryption, dropping support for OpenSSL 0.9.6 and older