The Most Expensive One-Byte Mistake: Was Nul-Terminated Text Strings Wrong?
queue.acm.org
queue.acm.org
What we need are strings prefixed with a length of the length of the string and then the length as a variable length integer.
Null termination was easy to implement quickly. It’s bad everywhere else.
Another benefit is that you don't have to decide on a maximum string length. Initially, they probably would have chosen a uint16 for the length, but over time we would have had to add uint32 and uint64. Old code doesn't completely fade away, so we would have had three similar but not-quite-compatible string types for all time.
A small further benefit is that strings with the same suffix can share the same storage.
I suspect there's more that I'm missing.
My 100% perfect concept ( :D ) would be [length]<here's the pointer>[data]where length would be read high to low. Because of the functionally upper capped length (maximum address space) we can treat accessing that as O(1) vs. O(N) for null termination, etc.
void accept_string(const char *a, size_t a_length);
is good enough. It has the additional advantage that you can give it a substring out of a larger string without copying it.