4 ms·
> "strlen" will always iterate through a string searching for a NULL byte whether it's in C, Rust, Assembly Not all languages use NULL terminated strings. I th
by wkjagt 5mo ago
> "strlen" will always iterate through a string searching for a NULL byte whether it's in C, Rust, Assembly
Not all languages use NULL terminated strings. I think Rust actually stores the string length alongside a pointer to the start of the string data. You can do the same in C, but you'd have to do it manually using a struct. In assembly you could do the same thing since you get to decide basically everything.
https://www.youtube.com/watch?v=y8PLpDgZc0E https://www.youtube.com/watch?v=y8PLpDgZc0E
- fragmede 5mo agoPascal stores the length of the string next to the string, but the other thing of note is with the advent of Unicode, strlen is strlen, but often not actually the number you need/want.
- jchw 5mo agoI argue that strlen is actually still quite often the number you need+want, because possibly the main use case for it is determining how many bytes long a string is.
- fragmede 5mo agoIt is, it just isn't always any more. When printing the string to the screen, the multibyte sequences need to be accounted for, like an emoji that's 2 bytes but only renders as one character too the screen.
- tgma 5mo agoArguably a POSIX assembly programmer would also naturally keep strings along with their size, either explicitly or known implicitly, more similar to Pascal, not null-terminated, as that's how syscalls expect it. Null-terminated would chiefly be a libc-ism.