What Is the “Length” of a String?
blog.etleap.com
blog.etleap.com
'length' is subjective. It makes sense to want to know how many storage units are required.
Knowing the number of printed 'graphemes' (do we count non-printing 'characters'?) might also be useful.
The display length (at a given scaling size, taking in to consideration font kerning/etc) can also be useful.
That's where the linked article really goes off the rails. Characters don't have printing widths, not outside of extremely specific circumstances. You have to ask the layout engine in use what dimensions are occupied after it solves the very complex problem.
I never understood the rationale for using encodings such as UTF-16. They seem to be the worst of both worlds: strings for which ASCII would be adequate take 2x the space, and the encoding is still multi-byte. I once worked with a Windows developer who swore blind that UTF-16 was not a multi-byte encoding. When I provided evidence to the contrary, they responded something along the lines of "ok, but who would ever need more than 16 bits worth of characters?". ¯\_(ツ)_/¯
It wouldn't be so bad if people just understood that there are (almost) no valid uses-cases for measuring the size of a string. As someone else mentioned, it's a binary blob for all intents and purposes, and if processing of its content needs to be done (such as displaying it on the screen, or performing, say, word-wrap) then it should be handed over to libraries that have been designed for this purpose, because these things are very complicated.
Simply by having a single non-ASCII character in my name, I'm seeing software fail on this on a regular basis.
The actual reason utf16 is used is because it was easier to port to utf16 from ucs16. Much the way it's often acceptable to assume utf8 is ASCII it's often acceptable to assume utf16 is utf8. Even today, I imagine there are a lot of applications which assume utf16 is actually ucs16.
Here's Dazai Osamu's Hashire Melos: https://www.aozora.gr.jp/cards/000035/files/1567_14913.html. Despite having an unusually large amount of markup (most Japanese text wouldn't add the ruby characters), it's smaller as UTF-16 than UTF-8.
But I will say that the concern you have does not take into account the concerns other people have who make this argument.
I wonder if they're aware of the `measureText` method used in canvas: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRende...
Seems a lot simpler than trying to split strings up by graphemes, and probably more reliable. Pairs of letters such as "fi" are rendered as ligatures (single glyphs) in some fonts, and Unicode no longer standardises them (since the combinations are completely arbitrary). Also, grapheme clustering is going to change according to Unicode version, and not everyone's browser is going to be using the same Unicode version.
If you want to know the "string length", you basically either want the number of code units to store it (in languages that handle strings sensibly, this is just the number of bytes, since UTF-8 should always be used, except for historical reasons), or you want to pass it to a font rendering library to tell you something about pixels.