From d7a44c2929e16726cbfedcfca87cace4990068a5 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Sun, 1 Oct 2023 23:17:56 +0200 Subject: [PATCH] string: add strncmp Signed-off-by: Simon Rozman --- include/stdex/string.hpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/include/stdex/string.hpp b/include/stdex/string.hpp index c647aa5ea..9e9933d5c 100644 --- a/include/stdex/string.hpp +++ b/include/stdex/string.hpp @@ -379,6 +379,29 @@ namespace stdex return 0; } + /// + /// Binary compare two strings + /// + /// \param[in] str1 String 1 + /// \param[in] str2 String 2 + /// \param[in] count String 1 and 2 code unit count limit + /// + /// \return Negative if str1str2; zero if str1==str2 + /// + template + inline int strncmp(_In_reads_or_z_opt_(count) const T1* str1, _In_reads_or_z_opt_(count) const T2* str2, _In_ size_t count) + { + assert((str1 && str2) || !count); + size_t i; T1 a; T2 b; + for (i = 0; i < count && ((a = str1[i]) | (b = str2[i])); ++i) { + if (a > b) return +1; + if (a < b) return -1; + } + if (i < count && str1[i]) return +1; + if (i < count && str2[i]) return -1; + return 0; + } + /// /// Lexigraphically compare two strings ///