Comment by JdeBP

Comment by JdeBP 7 days ago

0 replies

Much has been said about Daniel J. Bernstein eschewing the Standard C library in publicfile and other softwares. But Bernstein's str_rchr() function was designed to expressly avoid this well-known gotcha of the Standard C string functions.

Here's str_rchr() which uses the offset of the terminating NUL as the returned sentinel value:

* https://github.com/jdebp/djbwares/blob/trunk/source/str_rchr...

And here's it being used (by publicfile's httpd and indeed other programs) to find the basename's extension in order to infer a content type:

* https://github.com/jdebp/djbwares/blob/trunk/source/filetype...

The extension is always a non-NULL string, that can always be passed to str_equal(). It is just sometimes a zero-length string.

It's possible, but a bit clunky, to achieve the same effect with two successive calls to Standard C/C++ strrchr(), or strchr(), the second being:

        if (!result) result = std::strchr(s, '\0');
Here's me doing that in my own code:

* https://github.com/jdebp/nosh/blob/c8d635c284b41b483067d5f58...

One can get very lost in the weeds on the comparative merits on different instruction architectures of compiler intrinsics, explicit loop unrolling, whole program optimization, and whatnot. (-: