public: add Q_strnlen with fallback to memchr

This commit is contained in:
Alibek Omarov 2024-12-17 20:00:36 +03:00
parent b4afe390d6
commit 339aebb08c
2 changed files with 15 additions and 0 deletions

View file

@ -169,6 +169,16 @@ static inline char *Q_strstr( const char *s1, const char *s2 )
} }
// libc extensions, be careful what to enable or what not // libc extensions, be careful what to enable or what not
static inline size_t Q_strnlen( const char *str, size_t size )
{
#if HAVE_STRNLEN
return strnlen( str, size );
#else
const char *p = (const char *)memchr( str, 0, size );
return p ? p - str : size;
#endif
}
static inline size_t Q_strncpy( char *dst, const char *src, size_t size ) static inline size_t Q_strncpy( char *dst, const char *src, size_t size )
{ {
if( unlikely( !dst || !src || !size )) if( unlikely( !dst || !src || !size ))

View file

@ -29,6 +29,10 @@ int main(int argc, char **argv) { return strlcpy(argv[1], argv[2], 10); }'''
STRLCAT_TEST = '''#include <string.h> STRLCAT_TEST = '''#include <string.h>
int main(int argc, char **argv) { return strlcat(argv[1], argv[2], 10); }''' int main(int argc, char **argv) { return strlcat(argv[1], argv[2], 10); }'''
STRNLEN_TEST = '''#include <string.h>
int main(int argc, char **argv) { return (int)strnlen(argv[0], 10); }
'''
ALLOCA_TEST = '''#include <%s> ALLOCA_TEST = '''#include <%s>
int main(void) { alloca(1); return 0; }''' int main(void) { alloca(1); return 0; }'''
@ -106,6 +110,7 @@ def configure(conf):
check_libc_extension(STRCHRNUL_TEST, 'strchrnul', 'HAVE_STRCHRNUL') check_libc_extension(STRCHRNUL_TEST, 'strchrnul', 'HAVE_STRCHRNUL')
check_libc_extension(STRLCPY_TEST, 'strlcpy', 'HAVE_STRLCPY') check_libc_extension(STRLCPY_TEST, 'strlcpy', 'HAVE_STRLCPY')
check_libc_extension(STRLCAT_TEST, 'strlcat', 'HAVE_STRLCAT') check_libc_extension(STRLCAT_TEST, 'strlcat', 'HAVE_STRLCAT')
check_libc_extension(STRNLEN_TEST, 'strnlen', 'HAVE_STRNLEN')
# kill temporary uselib # kill temporary uselib
del conf.env.DEFINES_export del conf.env.DEFINES_export