4 ms·
Am I wrong for assuming the .length should return a length in bytes? If you want to use 32bit units, then multiply your output by 4. If you want to do Unicode
by ars 3y ago
Am I wrong for assuming the .length should return a length in bytes? If you want to use 32bit units, then multiply your output by 4.
If you want to do Unicode string manipulation and length counting, then use specific functions for that - but the base internal .length function should just output bytes.
- Arnavion 3y ago>then multiply your output by 4. That is not how UTF-32 works. >but the base internal .length function should just output bytes. Do you think the length of an `int64_t[3]` array should be 3 or 24?
- ars 3y ago> Do you think the length of an `int64_t[3]` array should be 3 or 24? There should be functions to do both: sizeof(int64_t[3]) * sizeof(int64_t) for example to get bytes. In this example, the base function should do bytes, and there should be a unicode function to count it in other ways. I could be sizing to fit in a database, or send over the wire, or I might want visible space on the screen, or I might want to know how to move the cursor. Each of those types of length should be supported.
- tialaramex 3y ago> There should be functions to do both: sizeof(int64_t[3]) * sizeof(int64_t) for example to get bytes. That's not what that does, the answer to the code you wrote is 192 because you're multiplying the same size factor twice.
- ars 3y agoNo I'm not, read it again. The first one has size 3, because there are 3 elements in the array. The second one has size 8 because an int64 is 8 bytes.
- tialaramex 3y agoI'm just reporting facts, C's sizeof operator measures bytes, so sizeof(int64_t[3]) * sizeof(int64_t) is 192 You don't have to like it
- ars 3y agoI apologize, you are correct. It's been too long since I used c. I should write instead: sizeof(int64_t[3]) / sizeof(int64_t) for length of array. This does help my argument that Javascript length on a Unicode string should return bytes though :)
- tgv 3y agoThe most obvious use case for length is iterating over the string and indexing it. In JS (or Go, Rust, Python) indexing and iteration is not byte based. As has been said elsewhere, length depends on the context/way you use it.
- tialaramex 3y agoIn Rust you need to specify what it is you think you're going to "iterate over" in a string. You can't just "iterate over a string" because that's not a thing. You can get an iterator over the bytes in the string, with "foo".bytes() or you can get an iterator over the Unicode scalar values in the string with "foo".chars(), or you can iterate over a UTF-16 encoding of the string with "foo".encode_utf16() You can index into Rust's strings, but you need to specify slice indexes, you can't just treat this like it's a array because that's not what it is. If you wanted a slice of bytes you can have one cheaply, it's as_bytes() which is a [u8] and you can index directly into that slice as with any array of bytes, but you can't mutate that and those aren't characters, they're just bytes.