From ff097d4432134da499abb0b3ea3b1803ccf9ac34 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Thu, 13 Jul 2023 14:35:48 +0200 Subject: [PATCH] string: have strncpy return number of non-zero code points in dst This information is a side product of these templates and saves an extra strnlen when caller plans on appending dst string some more data. Signed-off-by: Simon Rozman --- include/stdex/string.hpp | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/include/stdex/string.hpp b/include/stdex/string.hpp index eb2932f83..cddf3e321 100644 --- a/include/stdex/string.hpp +++ b/include/stdex/string.hpp @@ -145,7 +145,7 @@ namespace stdex /// /// \param[in] str String /// - /// \return Number of characters excluding zero terminator in the string. + /// \return Number of code units excluding zero terminator in the string. /// template inline size_t strlen(_In_z_ const T* str) @@ -162,7 +162,7 @@ namespace stdex /// \param[in] str String /// \param[in] count Code unit limit /// - /// \return Number of characters excluding zero terminator in the string. + /// \return Number of code units excluding zero terminator in the string. /// template inline size_t strnlen(_In_reads_or_z_opt_(count) const T* str, _In_ size_t count) @@ -414,13 +414,20 @@ namespace stdex /// \param[in] src Source string /// \param[in] count String code unit count limit /// + /// \return Number of code units excluding zero terminator in the dst string after the operation. + /// template - inline void strncpy( + inline size_t strncpy( _Out_writes_(count) _Post_maybez_ T1* dst, _In_reads_or_z_opt_(count) const T2* src, _In_ size_t count) { assert(dst && src || !count); - for (size_t i = 0; i < count && (dst[i] = src[i]) != 0; ++i); + for (size_t i = 0; ; ++i) { + if (i >= count) + return i; + if ((dst[i] = src[i]) == 0) + return i; + } } /// @@ -431,8 +438,10 @@ namespace stdex /// \param[in] src Source string /// \param[in] count_src Source string code unit count limit /// + /// \return Number of code units excluding zero terminator in the dst string after the operation. + /// template - inline void strncpy( + inline size_t strncpy( _Out_writes_(count_dst) _Post_maybez_ T1* dst, _In_ size_t count_dst, _In_reads_or_z_opt_(count_src) const T2* src, _In_ size_t count_src) { @@ -441,13 +450,13 @@ namespace stdex for (size_t i = 0; ; ++i) { if (i > count_dst) - break; + return i; if (i > count_src) { dst[i] = 0; - break; + return i; } if ((dst[i] = src[i]) == 0) - break; + return i; } } @@ -458,7 +467,7 @@ namespace stdex /// \param[in] dst Destination string - must be same or longer than src /// \param[in] src Source string /// - /// \return Number of characters excluding zero terminator in the dst string after the operation. + /// \return Number of code units excluding zero terminator in the dst string after the operation. /// template inline size_t crlf2nl(_Out_writes_z_(strlen(src)) T* dst, _In_z_ const T* src)