[Swan] streq()

D. Hugh Redelmeier hugh at mimosa.com
Sun Feb 10 09:14:38 EET 2013


Pluto has a macro, defined in include/constants.h, called streq:

#define streq(a, b) (strcmp((a), (b)) == 0)     /* clearer shorthand */

This is a clearer way of writing tests for string equality that strcmp 
directly.

Here's an example of how to make your code confusing:

programs/pluto/spdb_v1_struct.c:172:       if(!strcmp(st->sec_ctx->sec_ctx_value, sec_ctx_value)) {

Perfectly correct, and very error prone.

Explicitly comparing with 0 is better, but still inferior to streq.

Currently there are 170 calls to strcmp in the tree.  I'd be surprized if 
more than 10 of them would not be clearer if they were replaced by calls 
to streq.

Some of them might not be in files that include constants.h.
In most cases, those programs would be improved by adding the #define 
and using streq.

The only two hits in programs/rsasigkey/rsasigkey.c:

programs/rsasigkey/rsasigkey.c:406:#    define  STREQ(a, b)     (strcmp(a, b) == 0)
programs/rsasigkey/rsasigkey.c:536:     pwdata.source = password ? (strcmp(password, buf)? PW_PLAINTEXT: PW_FROMFILE) : PW_NONE;

That second would be clearer if it used the first.
I would put more parens in line 406 just to be sure.
The file does use STREQ a lot.

Note: officially the C standard has reserved identifiers that begin
with str.  So STREQ is technically safer.  I think that streq is
enough nicer that I'm willing to chance it.


More information about the Swan mailing list