Add trim and trim_empty support to strsplit and update all callers

This commit is contained in:
Andrea Mazzoleni 2026-09-19 11:12:49 +02:00
parent 929ba5cebe
commit 8e66b42ceb
5 changed files with 163 additions and 23 deletions

View file

@ -26,8 +26,9 @@ char* strpolish(char* s);
* null-terminates each token by overwriting its trailing delimiter,
* and stores a pointer to each token's start in @split_map.
*
* Leading and consecutive delimiters are silently skipped, so the
* output never contains empty tokens.
* If @trim_empty is non-zero, leading and consecutive delimiters are
* silently skipped, and tokens trimmed to empty are discarded.
* If @trim_empty is zero, empty tokens are preserved.
*
* The input string is modified in-place; @split_map entries point
* directly into @str and are only valid for its lifetime.
@ -36,15 +37,17 @@ char* strpolish(char* s);
* \param split_max Capacity of @split_map; at most this many tokens are stored.
* \param str Input string to tokenise (modified in-place).
* \param delimiters Null-terminated set of delimiter characters.
* \param trim Null-terminated set of characters to trim from the start and end of each token.
* \param trim_empty Whether to trim empty tokens and consecutive delimiters.
* \return Number of tokens stored in @split_map.
*
* Example:
* char buf[] = " one,two,,three ";
* char buf[] = " one, two,, , three ";
* char* map[8];
* unsigned n = strsplit(map, 8, buf, " ,");
* unsigned n = strsplit(map, 8, buf, ",", " ", 1);
* // n == 3, map[] == { "one", "two", "three" }
*/
unsigned strsplit(char** split_map, unsigned split_max, char* str, const char* delimiters);
unsigned strsplit(char** split_map, unsigned split_max, char* str, const char* delimiters, const char* trim, int trim_empty);
/**
* Split a command-line string into an argument vector,