From 4a24b57035df2be5a982a293419cd3e517cb4803 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 8 Sep 2017 01:58:07 -0700 Subject: [PATCH] ircd: Add string_view modern overloads to classic BSD strl*() iface. --- include/ircd/lexical.h | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/include/ircd/lexical.h b/include/ircd/lexical.h index 32691059a..68092dfeb 100644 --- a/include/ircd/lexical.h +++ b/include/ircd/lexical.h @@ -157,6 +157,8 @@ namespace ircd // Vintage size_t strlcpy(char *const &dest, const char *const &src, const size_t &bufmax); size_t strlcat(char *const &dest, const char *const &src, const size_t &bufmax); + size_t strlcpy(char *const &dest, const string_view &src, const size_t &bufmax); + size_t strlcat(char *const &dest, const string_view &src, const size_t &bufmax); // Legacy char *strip_colour(char *string); @@ -532,6 +534,25 @@ ircd::tokens(A allocator, return ret; } +inline size_t +ircd::strlcpy(char *const &dest, + const string_view &src, + const size_t &max) +{ + if(!max) + return 0; + + const auto &len + { + src.size() >= max? max - 1 : src.size() + }; + + assert(len < max); + memcpy(dest, src.data(), len); + dest[len] = '\0'; + return len; +} + inline size_t #ifndef HAVE_STRLCPY ircd::strlcpy(char *const &dest, @@ -541,12 +562,8 @@ ircd::strlcpy(char *const &dest, if(!max) return 0; - const size_t ret(strnlen(src, max)); - const size_t len(ret >= max? max - 1 : ret); - memcpy(dest, src, len); - dest[len] = '\0'; - - return ret; + const auto len{strnlen(src, max)}; + return strlcpy(dest, {src, len}, max); } #else ircd::strlcpy(char *const &dest,