Re: mutt_buffer_concat_path is broken [PATCH: 1/1] Version 2

Alejandro Colomar via Mutt-dev <[email protected]> Fri, 8 May 2026 15:16:24 +0200
Newsgroups gmane.mail.mutt.devel
Message-ID <af3ghoHALl7ayee_@devuan>
Hi Rene, Derek,

On 2026-05-08T07:47:15-0400, Derek Martin wrote:
> On Fri, May 08, 2026 at 08:08:13AM +0200, Rene Kita wrote:
> > strspn is one of those function I can't get into my head, that's why I
> > always end with a loop. :-) I should really try to remember it.
> 
> I prefer the loop:  They're functionally equivalent, nearly as
> succinct, and far more explicit.  you're not the only one who has
> trouble remembering it.  In professional production code I've seen
> loops far more often than strspn.  Its name doesn't really lend to one
> remembering what it does, like a lot of the less-commonly-used C
> library functions.  =8^)

The name means "string span".  It is the span (length) of the substring
composed exclusively of characters in the second parameter.

I believe it's not often used because few people know it, and thus few
people spread the knowledge of when it's useful and how.

There are mainly two cases where it's useful:

-  Skip leading characters (often, white space, or other delimiters).

	s += strspn(s, " \t");  // skip white space
	s += strspn(s, "/");   // skip path delimiters

-  A boolean indicator for whether a string starts with some character.

	if (strspn(s, "/"))
		handle_absolute_path(s);
	else
		handle_relative_path(s);

Then there are a few other cases, but these two cover the most common
ones.  Once you learn this function, it crams a lot of logic into a
single function, making the code more compact and readable.  I find it
to be one of the most useful string functions.

Another very useful and rarely used one is strpbrk(3).  That is
strchr(3) on steroids.  (And a better name for it might have been
strchrs(), as Plan9 called it.)


Have a lovely day!
Alex

-- 
<https://www.alejandro-colomar.es>
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEES7Jt9u9GbmlWADAi64mZXMKQwqkFAmn94iIACgkQ64mZXMKQ
wql2jQ/+P2RwORgSfJFktt6tFvTjDHInrDsSdyqwOVIG2k8F+qAzElC+rxx9Psbs
CS/o4eR1l7uQ1Xp9rmo4UD7fZOUP2klnpJixnjhsMKoyLYsDyvHn6v2boHLKGtj8
Ce23/xNoJtYjPRGqI5ahsNfgnlx2WDHfTPXeg/5Ep97z3+Qx+Db+tPMUMC1nmroT
JgtuBJ+tJ6ExR80vX7H2i26wbpGMKfngpG7JyBZPkK15czLw1Y7opAKb3IcFD+5t
7Y+rfFYp3lxfruzalRIn83Q8sOofA8dPcxNR+dA4UQeiBFqU0Fp9d3dUWTRLWJAN
O4x59/ie5WjGSux6suTkTXxo6LIXNPJ59PQKNvaP+x3RP4clpeAfnw0NvuKMPae3
k9/N2XMlaCD389m+stxq+SDvQcVTTzSW2i2UOODhJlr94nE+KMd9pMdE2+c/SIY8
5Jzh3M/zdrYNzz4BpLWrU6RRmk7wgTpOc9yOhvrL4rOvWVdsOtdYnYuXumKCsUvV
GAdZcIjx2DJ0wdI6c2Jh8zyf0Hw/OitAOkxRkxxc2rDOydX8kVeb6auqGYE4qC74
NqJD+NZgb+tXrzvQupKADktGk/OGjAv8DooNYZL6GPlUyKBDKjoZbfP0XHtIGobP
kEO7Dscf+pMGgPMC4KPbApSejqNm0MV+bLvUO3+zONtbQ+he6zs=
=2rNJ
-----END PGP SIGNATURE-----