diff options
author | Alejandro Colomar <alx@kernel.org> | 2022-12-24 20:33:43 +0100 |
---|---|---|
committer | Alejandro Colomar <alx@kernel.org> | 2022-12-24 22:48:25 +0100 |
commit | 0af6dbb3d4cbade1add07de00567bea0e3f5f99a (patch) | |
tree | ce563b2c0339cd12e9c5703f5d494f141c8b2f98 | |
parent | e10172137816c6b79e2b95bdcf490274666f3c5d (diff) |
stpecpy.h, stpecpy.c: stpecpy(): Make the function inline
This function has several operations that can be skipped in normal
conditions. Inlining will allow the compiler see those, and hopefully
transform some calls into mempcpy(3) or stpcpy(3).
Signed-off-by: Alejandro Colomar <alx@kernel.org>
-rw-r--r-- | include/stp/stpe/stpecpy.h | 29 | ||||
-rw-r--r-- | src/stp/stpe/stpecpy.c | 27 |
2 files changed, 30 insertions, 26 deletions
diff --git a/include/stp/stpe/stpecpy.h b/include/stp/stpe/stpecpy.h index 7dc318a..ca6a23c 100644 --- a/include/stp/stpe/stpecpy.h +++ b/include/stp/stpe/stpecpy.h @@ -6,12 +6,39 @@ #define INCLUDE_STP_STPE_STPECPY_H_ +#include <stdbool.h> +#include <stddef.h> +#include <string.h> + #include <stp/_compiler.h> #pragma clang assume_nonnull begin -char *stp_nullable stpecpy(char *stp_nullable dst, char *end, +inline char *stp_nullable stpecpy(char *stp_nullable dst, char *end, const char *restrict src); + + +inline char *stp_nullable +stpecpy(char *stp_nullable dst, char *end, const char *restrict src) +{ + bool trunc; + size_t dsize, dlen, slen; + + slen = strlen(src); + + if (dst == end) + return end; + if (stp_unlikely(dst == NULL)) // Allow chaining with stpeprintf(). + return NULL; + stp_impossible(dst > end); + + dsize = end - dst; + trunc = (slen >= dsize); + dlen = trunc ? dsize - 1 : slen; + dst[dlen] = '\0'; + + return mempcpy(dst, src, dlen) + trunc; +} #pragma clang assume_nonnull end diff --git a/src/stp/stpe/stpecpy.c b/src/stp/stpe/stpecpy.c index 0fbc014..3c61da0 100644 --- a/src/stp/stpe/stpecpy.c +++ b/src/stp/stpe/stpecpy.c @@ -4,33 +4,10 @@ #include <stp/stpe/stpecpy.h> -#include <stdbool.h> -#include <stddef.h> -#include <string.h> - #include <stp/_compiler.h> #pragma clang assume_nonnull begin -char *stp_nullable -stpecpy(char *stp_nullable dst, char end[0], const char *restrict src) -{ - bool trunc; - size_t dsize, dlen, slen; - - slen = strlen(src); - - if (dst == end) - return end; - if (stp_unlikely(dst == NULL)) // Allow chaining with stpeprintf(). - return NULL; - stp_impossible(dst > end); - - dsize = end - dst; - trunc = (slen >= dsize); - dlen = trunc ? dsize - 1 : slen; - dst[dlen] = '\0'; - - return mempcpy(dst, src, dlen) + trunc; -} +extern inline char *stp_nullable stpecpy(char *stp_nullable dst, char end[0], + const char *restrict src); #pragma clang assume_nonnull end |