lib: Add a function to split a string into substrings

Some environment variables provide a space-separated list of strings. It
is easier to process these when they are broken out into an array of
strings.

Add a utility function to handle this.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass
2023-01-17 10:47:14 -07:00
committed by Tom Rini
parent a0fb9de60d
commit 3e96ed44e8
3 changed files with 147 additions and 0 deletions

View File

@@ -328,6 +328,30 @@ char *strmhz(char *buf, unsigned long hz);
*/
void str_to_upper(const char *in, char *out, size_t len);
/**
* str_to_list() - Convert a string to a list of string pointers
*
* Splits a string containing space-delimited substrings into a number of
* separate strings, e.g. "this is" becomes {"this", "is", NULL}. If @instr is
* empty then this returns just {NULL}. The string should have only a single
* space between items, with no leading or trailing spaces.
*
* @instr: String to process (this is alloced by this function)
* Returns: List of string pointers, terminated by NULL. Each entry points to
* a string. If @instr is empty, the list consists just of a single NULL entry.
* Note that the first entry points to the alloced string.
* Returns NULL if out of memory
*/
const char **str_to_list(const char *instr);
/**
* str_free_list() - Free a string list
*
* @ptr: String list to free, as created by str_to_list(). This can also be
* NULL, in which case the function does nothing
*/
void str_free_list(const char **ptr);
/**
* vsscanf - Unformat a buffer into a list of arguments
* @inp: input buffer