2 ms·
Null terminated strings make writing parsers really clean. The null byte becomes another character for you to check against in your parser code, so you don't ne
by nofriend 1y ago
Null terminated strings make writing parsers really clean. The null byte becomes another character for you to check against in your parser code, so you don't need a separate check for the length (usually checks are inclusive: it is character 'a'? if not, defer to caller. so checking for null byte can happen in a single location, whereas checking for length would need to happen in every function). And it means you have lots of *ptr++ spread around your code, rather than having to pass around a struct and modify it, or call methods on it.
- IshKebab 1y agoIt's really not hard to check the length. Checking null bytes also adds an awkward memory data dependency that can make SIMD more awkward. It also makes strlen O(n) which is kinda shit - for example it led to that famous GTA5 accidental O(n^2) bug. For situations where a null terminator really is better it's easy to add them to a length-prefixed string, whereas the reverse is not true. They clearly got this wrong.