5 ms·
I'm sorry, but if a language forced me to write str.len_utf8_bytes() every time I wanted the length of a string, I'd just stop using that language.
by adtac 5y ago
I'm sorry, but if a language forced me to write str.len_utf8_bytes() every time I wanted the length of a string, I'd just stop using that language.
- mgdlbp 5y agoThough it might encourage some to consider str.len_extended_grapheme_clusters()
- jeffparsons 5y agoOut of interest, which length did you want?
- jhasse 5y agoNumber of characters
- int_19h 5y agoWhat's a "character"? A codepoint? A glyph? Is "fi" a single character? How about "ß"? "Ȣ"? "IJ"? When programmers get answers to these questions wrong, the code that they write ends up being broken for someone out there. And they do get it wrong if their native culture instills a wrong kind of "common sense" about scripts. Which is precisely why we need more stuff like str.len_utf8_bytes() - because it forces the person writing it to consider their assumptions.
- josephg 5y agoYep. I think about this stuff a lot and I still mess this up all the time. I’ve come to realise that string.length in javascript is a footgun. Basically every time I use it, my code is wrong when I test with non-ascii characters. I’ve spent the last decade writing javascript and I’ve never once actually cared to know what half the UTF16 byte length of a string is. The only legitimate uses are internal in javascript (since other parts of JS make that quantity meaningful). Like using it in slice() or iterating - though for the latter we now have Unicode string iteration anyway.
- jhasse 5y ago> Is "fi" a single character? [...] "Ȣ"? "IJ"? I don't know these, someone who does could properly tell you. The implementation could fallback to "glyph" or "codepoint" (don't know what those are exactly) if it is unsure. Mostly right is better than nearly always wrong (= returning number of bytes) IMHO. > How about "ß"? That's one character. > When programmers get answers to these questions wrong, the code that they write ends up being broken for someone out there. So? Returning the number of bytes would also be wrong. Renaming len to len_glyphs will just result in most programmers typing more and some accidentally using len_utf8_bytes (returns the number of bytes?) where len_glyphs would be less wrong.
- deleted 5y ago[deleted]