Timeline for answer to str_split() function, not present in standard C library by Toby Speight
Current License: CC BY-SA 4.0
Post Revisions
16 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Feb 6, 2022 at 19:48 | comment | added | Davislor |
Note that this is very similar to malloc() + strncpy() + a strtok() loop.
|
|
| Jan 24, 2022 at 7:51 | comment | added | Toby Speight | In any case, the "allocate a single block" approach ameliorates the problem - you can freely reassign the pointers without leaking memory or freeing non-allocated memory. And that's the version I recommend. | |
| Jan 24, 2022 at 6:46 | comment | added | user245050 | Yes, characters can be changed with char *const * but the point is why to put any restriction? Let the user do what he/she wants to do. strcat(), strcpy(), etc. also return a char * (without any const qualifier) which the user can change (contents as well). And there is no documentation in their man pages that the pointers that are returned can be used safely or not. | |
| Jan 23, 2022 at 11:35 | comment | added | Toby Speight |
I said that you have a choice - either provide sufficient documentation that it can be used safely, or provide a safer but less flexible interface that doesn't need such documentation. If you want users to be able to change the pointers, you need to help them to do so safely. BTW, changing characters within the strings is still possible with char *const* (unlike char const **, for instance).
|
|
| Jan 23, 2022 at 10:52 | comment | added | user245050 | The user might have a valid reason for changing resultant strings - like replacing some character with another character in the resultant strings - and the user couldn't do it in the original string because the user got the original string as ""const"" and so the user may want to do it in the resultant strings. | |
| Jan 23, 2022 at 10:52 | comment | added | user245050 |
Toby, about these: The return type (char**) could cause surprise, as a user may expect to be able to overwrite any of the pointers. and or return char *const * instead: If the user wants to change the return values then it is his/her decision. Why should I restrict him/her from doing so by returning char *const *?
|
|
| Jan 23, 2022 at 9:10 | comment | added | user245050 |
Even PHP's explode function (php.net/manual/en/function.explode.php) gives an error on empty delimiter: PHP Warning: explode(): Empty delimiter in /home/cg/root/5206135/main.php on line 10.
|
|
| Jan 23, 2022 at 9:05 | comment | added | user245050 |
I checked Python's string split function and if you pass in an empty delimiter, it gives an error: Traceback (most recent call last): File "./prog.py", line 3, in <module> ValueError: empty separator. So, can you tell me in which language did you find this: null or empty delimiter means "split between each pair of characters".?
|
|
| Jan 23, 2022 at 9:00 | comment | added | user245050 | I actually thought about memcpy() and memmove() when I was writing the code. I decided to use memmove() all the time so that I don't have to figure out whether source and destination are overlapping or not. | |
| Jan 22, 2022 at 9:09 | history | edited | Toby Speight | CC BY-SA 4.0 |
Improved code to verify we don't over-allocate
|
| Jan 22, 2022 at 8:49 | comment | added | Toby Speight |
Using strtok() as comparison isn't helpful, since that function takes a set of characters, any of which act as delimiter. Passing an empty set makes no sense.
|
|
| Jan 22, 2022 at 8:35 | history | edited | Toby Speight | CC BY-SA 4.0 |
added 2062 characters in body
|
| Jan 21, 2022 at 14:30 | history | edited | Toby Speight | CC BY-SA 4.0 |
Negative max_split is documented mean unlimited
|
| Jan 21, 2022 at 14:29 | comment | added | Toby Speight |
I missed the sentence saying that negative max_splits can be used to mean "unlimited" - sorry for my error! At the risk of repeating a different point, a test suite helps reviewers see what is confirmed to work!
|
|
| Jan 21, 2022 at 14:12 | comment | added | user245050 |
I find it surprising that an empty string as delimiter doesn't split the string into 1-character chunks. That's what experience with other libraries leads me to expect, so consider changing that behaviour.: I think I am doing what strtok() does with the delimiter being NULL or empty string. But I will check again and post the results.
|
|
| Jan 21, 2022 at 13:22 | history | answered | Toby Speight | CC BY-SA 4.0 |