public: add Q_strnlen with fallback to memchr
This commit is contained in:
parent
b4afe390d6
commit
339aebb08c
2 changed files with 15 additions and 0 deletions
|
@ -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
|
||||
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 )
|
||||
{
|
||||
if( unlikely( !dst || !src || !size ))
|
||||
|
|
|
@ -29,6 +29,10 @@ int main(int argc, char **argv) { return strlcpy(argv[1], argv[2], 10); }'''
|
|||
STRLCAT_TEST = '''#include <string.h>
|
||||
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>
|
||||
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(STRLCPY_TEST, 'strlcpy', 'HAVE_STRLCPY')
|
||||
check_libc_extension(STRLCAT_TEST, 'strlcat', 'HAVE_STRLCAT')
|
||||
check_libc_extension(STRNLEN_TEST, 'strnlen', 'HAVE_STRNLEN')
|
||||
|
||||
# kill temporary uselib
|
||||
del conf.env.DEFINES_export
|
||||
|
|
Loading…
Add table
Reference in a new issue