From 1c54745b3bf903d93432441b889d61d1650ab007 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Mon, 3 Jul 2023 13:56:48 +0200 Subject: [PATCH] string: Add strncpy Signed-off-by: Simon Rozman --- include/stdex/string.hpp | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/include/stdex/string.hpp b/include/stdex/string.hpp index c92eb247f..db67e8b6a 100644 --- a/include/stdex/string.hpp +++ b/include/stdex/string.hpp @@ -336,6 +336,50 @@ namespace stdex } } + /// + /// Copy zero-terminated string + /// + /// \param[in] dst Destination string + /// \param[in] src Source string + /// \param[in] count String code unit count limit + /// + template + inline void 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); + } + + /// + /// Copy zero-terminated string + /// + /// \param[in] dst Destination string + /// \param[in] count_dst Destination string code unit count limit + /// \param[in] src Source string + /// \param[in] count_src Source string code unit count limit + /// + template + inline void 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) + { + assert(dst || !count_dst); + assert(src || !count_src); + for (size_t i = 0; ; ++i) + { + if (i > count_dst) + break; + if (i > count_src) { + dst[i] = 0; + break; + } + if ((dst[i] = src[i]) == 0) + break; + } + } + /// /// Convert CRLF to LF /// Source and destination strings may point to the same buffer for inline conversion.