From ac7617990a05ecc35ba7198e60174518f76c5d80 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Sun, 17 Nov 2024 05:34:29 +0300 Subject: [PATCH] public: add Q_splitstr function --- public/crtlib.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/public/crtlib.h b/public/crtlib.h index 2356d195..3fdda4a1 100644 --- a/public/crtlib.h +++ b/public/crtlib.h @@ -284,6 +284,34 @@ static inline const char *Q_strchrnul( const char *s, int c ) } #endif // !HAVE_STRCHRNUL +/* +=========== +Q_splitstr + +splits strings by a character +if handler returns nonzero value, exists with that value +=========== +*/ +static inline int Q_splitstr( char *str, int delim, void *userdata, + int (*handler)( char *prev, char *next, void *userdata )) +{ + char *prev = str; + char *next = Q_strchrnul( prev, delim ); + int ret = 0; + + for( ; ; prev = next + 1, next = Q_strchrnul( prev, delim )) + { + int ch = *next; // save next value if it's modified by handler + + ret = handler( prev, next, userdata ); + + if( !ch || ret != 0 ) + break; + } + + return ret; +} + #ifdef __cplusplus } #endif