[PATCH 1/1] string: Add stpecpy(3)
Alejandro Colomar
alx.manpages@gmail.com
Fri Dec 23 17:27:05 GMT 2022
On 12/23/22 18:03, Alejandro Colomar wrote:
> alx@asus5775:~/src/alx/libstp/src$ grepc -tfd stpecpy
> ./stp/stpe/stpecpy.c:15:
> char *stp_nullable
> stpecpy(char *stp_nullable dst, char end[0], const char *restrict src)
> {
> char *stp_nullable p;
>
> if (dst == end)
> return end;
> if (stp_unlikely(dst == NULL)) // Allow chaining with stpeprintf().
> return NULL;
Oh, and the two branches above can be optimized into a branch that returns dst.
I wrote them expanded, for added readability, and allow the compiler reorganize
it however it pleases. So, in reality, there are 2 actual branches, plus one
inside memccpy(3).
> if (dst > end)
> stp_unreachable();
>
> p = memccpy(dst, src, '\0', end - dst);
> if (p != NULL)
> return p - 1;
>
> /* Truncation detected. */
> end[-1] = '\0';
> return end;
> }
>
> alx@asus5775:~/src/gnu/glibc$ grepc -tfd __memccpy
> ./string/memccpy.c:30:
> void *
> __memccpy (void *dest, const void *src, int c, size_t n)
> {
> void *p = memchr (src, c, n);
>
> if (p != NULL)
> return __mempcpy (dest, src, p - src + 1);
>
> memcpy (dest, src, n);
> return NULL;
> }
>
--
<http://www.alejandro-colomar.es/>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <https://sourceware.org/pipermail/libc-alpha/attachments/20221223/d49dfdc3/attachment.sig>
More information about the Libc-alpha
mailing list