public: add Q_splitstr function
This commit is contained in:
parent
0813d2c7ce
commit
ac7617990a
1 changed files with 28 additions and 0 deletions
|
@ -284,6 +284,34 @@ static inline const char *Q_strchrnul( const char *s, int c )
|
||||||
}
|
}
|
||||||
#endif // !HAVE_STRCHRNUL
|
#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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Reference in a new issue