There is no Standard function in C to take a string, break it up at whitespace
or other delimiters, and create an array of pointers to char
, in one step.
If you want to do that sort of thing, you have to do it yourself, either
completely by hand, or by calling e.g. strspn
and strpbrk
in a loop,
or by calling strtok
in a loop, or by calling strsep
in a loop.
I am not asking how to do this. I know how to do this, and there are plenty of other questions on Stackoverflow about how to do it. What I'm asking is if there are any good reasons why there's no such function.
I know the two main reasons, of course: "Because no mainstream compiler/library ever had one" and "Because the C Standard didn't specify one, either (because it likes to standardize existing practice)." But are there any other reasons? (Are there arguments that such a function is an actively bad idea?)
This is usually a lame and pointless sort of question, I know. In this case I'm fixated on it because convenient splitting is such a massively useful operation. I wrote my own string splitter within my first year as a C programmer, I think, and it's been a huge productivity enhancer for me ever since. There are dozens of questions here on SO every day that could be answered easily (or that wouldn't even have to be asked) if there were a standard split function that everyone could use and refer to.
To be clear, the function I'm imagining would have a signature like
int split(char *string, char **argv, int maxargs, const char *delim)
It would break up string
into at most maxargs
substrings, splitting on one or more characters from delim
, placing pointers to the substrings into argv
, and modifying string
in the process.
And to head off an argument I'm sure someone will make: although it's standard, I do not consider
strtok
to be an effective solution. strtok
, frankly, sucks. Saying "you don't need a split function,
because strtok
exists" is a lot like saying "You don't need printf
,
because puts
exists." This is not a question about what's theoretically
possible with a given toolset; it's about what's useful and convenient. The more
fundamental issue here, I guess, concerns the ineffable tradeoffs involved
in picking tools that are leverageable and productivity-enhancing and that
"pay their way". (I think it's clear that a nicely encapsulated
string-splitting function would pay its way handsomely, but perhaps
that's just me.)