From a59971fdbda08b12e34464c05978461393afdafb Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Wed, 8 Mar 2023 11:16:39 +0100 Subject: [PATCH] Add strnchr() and crlf2nl() Signed-off-by: Simon Rozman --- include/stdex/sal.h | 6 ++++ include/stdex/string.h | 71 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 include/stdex/string.h diff --git a/include/stdex/sal.h b/include/stdex/sal.h index bf34c163f..b1ae7d724 100644 --- a/include/stdex/sal.h +++ b/include/stdex/sal.h @@ -33,6 +33,9 @@ #ifndef _In_z_count_ #define _In_z_count_(p) #endif +#ifndef _In_reads_or_z_ +#define _In_reads_or_z_(p) +#endif #ifndef _Inout_ #define _Inout_ @@ -48,6 +51,9 @@ #ifndef _Out_opt_ #define _Out_opt_ #endif +#ifndef _Out_writes_z_ +#define _Out_writes_z_(p) +#endif #ifndef _Success_ #define _Success_(p) diff --git a/include/stdex/string.h b/include/stdex/string.h new file mode 100644 index 000000000..3cb8d6e96 --- /dev/null +++ b/include/stdex/string.h @@ -0,0 +1,71 @@ +/* + SPDX-License-Identifier: MIT + Copyright © 2016-2022 Amebis +*/ + +#pragma once + +#include "sal.h" + +namespace stdex +{ + /// + /// Calculate zero-terminated string length. + /// + /// \param[in] str String + /// + /// \return Number of characters excluding zero terminator in the string. + /// + template + inline size_t strlen(_In_z_ const T* str) + { + size_t i; + for (i = 0; str[i]; i++); + return i; + } + + /// + /// Finds a character in a string. + /// + /// \param[in] str String + /// \param[in] chr Character to search for + /// \param[in] count Maximum number of characters in string str to search for + /// + /// \return Pointer to the first occurence of chr character or NULL if not found. + /// + template + inline const T* strnchr( + _In_reads_or_z_(count) const T* str, + _In_ T chr, + _In_ size_t count) + { + for (size_t i = 0; i < count && str[i]; i++) + if (str[i] == chr) return str + i; + return NULL; + } + + /// + /// Convert CRLF to LF + /// Source and destination strings may point to the same buffer for inline conversion. + /// + /// \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. + /// + template + inline size_t crlf2nl(_Out_writes_z_(strlen(src)) T* dst, _In_z_ const T* src) + { + size_t i, j; + for (i = j = 0; src[j];) { + if (src[j] != (T)'\r' || src[j + 1] != (T)'\n') + dst[i++] = src[j++]; + else { + dst[i++] = (T)'\n'; + j += 2; + } + } + dst[i] = (T)0; + return i; + } +} \ No newline at end of file