|
|
|
|
|
by nitrogen
6024 days ago
|
|
Other than strlen, string operations can be faster with null-terminated strings. Plus, there are other benefits: If you want to turn some arbitrary data into a string, just put a 0 byte where you want it to end. Instead of having to increment and compare both a counter and a pointer (or store a final pointer for comparison) in string-manipulation operations, you just increment the pointer. It makes for very concise loops in strchr, etc. Tokenizing a string is just a matter of throwing down 0 bytes where the tokens are (as strtok does). Passing a tailing subset of a string to a function is as easy as adding an integer to the string pointer, rather than requiring a memcpy and length calculation. |
|
Strtok is just as easily handled by handling strings as a 2-item struct: a size_t for length and a pointer (separating the size data from the character array). In fact, that would kick ass: you can non-destructively tokenize, pass around substring arguments, etc.