From 7f1bb9b4a62ebdfaefb8e72a5c73aec90c89c69b Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Thu, 25 Aug 2022 19:14:52 +0300 Subject: [PATCH] public: introduce Q_strnicmpext function The goal is to provide both string compare with fixed length and simple pattern match --- public/crtlib.c | 12 ++++++++++-- public/crtlib.h | 3 ++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/public/crtlib.c b/public/crtlib.c index 652ae917..6dba1ec4 100644 --- a/public/crtlib.c +++ b/public/crtlib.c @@ -338,12 +338,15 @@ static qboolean Q_starcmp( const char *pattern, const char *text ) } } -qboolean Q_stricmpext( const char *pattern, const char *text ) +qboolean Q_strnicmpext( const char *pattern, const char *text, size_t minimumlength ) { + size_t i = 0; char c; while(( c = *pattern++ ) != '\0' ) { + i++; + switch( c ) { case '?': @@ -361,7 +364,12 @@ qboolean Q_stricmpext( const char *pattern, const char *text ) return false; } } - return ( *text == '\0' ); + return ( *text == '\0' ) || i == minimumlength; +} + +qboolean Q_stricmpext( const char *pattern, const char *text ) +{ + return Q_strnicmpext( pattern, text, ~((size_t)0) ); } const char* Q_timestamp( int format ) diff --git a/public/crtlib.h b/public/crtlib.h index 84027ea6..f3013a8f 100644 --- a/public/crtlib.h +++ b/public/crtlib.h @@ -76,7 +76,8 @@ float Q_atof( const char *str ); void Q_atov( float *vec, const char *str, size_t siz ); #define Q_strchr strchr #define Q_strrchr strrchr -qboolean Q_stricmpext( const char *s1, const char *s2 ); +qboolean Q_stricmpext( const char *pattern, const char *text ); +qboolean Q_strnicmpext( const char *pattern, const char *text, size_t minimumlen ); const char *Q_timestamp( int format ); #define Q_vsprintf( buffer, format, args ) Q_vsnprintf( buffer, 99999, format, args ) int Q_vsnprintf( char *buffer, size_t buffersize, const char *format, va_list args );