Add strnchr() and crlf2nl()

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2023-03-08 11:16:39 +01:00
parent dda761a692
commit a59971fdbd
2 changed files with 77 additions and 0 deletions

View File

@ -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)

71
include/stdex/string.h Normal file
View File

@ -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 <class T>
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 <class T>
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 <class T>
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;
}
}