2 ms·
There’s a lot of commentary in this post about storage size, but outside of serialisation (for file storage or network reasons) the actual byte count is typical
by olliej 7y ago
There’s a lot of commentary in this post about storage size, but outside of serialisation (for file storage or network reasons) the actual byte count is typically not what you want.
What you want in your UI is the number of glyphs that will be presented to the user. Which means it is correct for it to change across OS or library versions, as new emoji will result in a different number of rendered glyphs.
There are very few places in program UI that want anything at all other than the actual glyph count - emoji aren’t even the first example of this, there are numerous “characters” the are made up of letters+accents that are not single code units/points/whatever (I can never recall), for which grapheme count is what you want.
The only time you care about the underlying codepoints is really if you yourself are rendering the characters, or you’re implementing complex text entry (input managers for many non-English characters). The latter of which you would pretty much never want to do yourself - I say this having done exactly that, and making it work correctly (especially on windows) was an utter nightmare.
Then once you get past code point/units to the level of bytes your developer set divides neatly in two:
* people who think the world is fixed # of bytes per character
* people who know to use the API function to get the number of bytes
But for any actual use of a string in a UI you want the number of glyphs that will actually be displayed.
My assumption is that that logic is why Swift gives you the count it does.
- slavik81 7y agoWhat do you do with the glyph count? I've never needed it. For storage I need to know the size in bytes. For the GUI I need to know the dimensions of the rendered result.
- olliej 7y agoCaret positioning, selection, etc /if/ you’re implementing text entry yourself - though doing so means accepting the huge amount of work required to support input managers. In every other case you are 100% correct: you want either the graphical bounds or the number of bytes. The other measures seem of questionable utility to anyone outside of the Unicode library implementation :)
- hsivonen 7y agoThe article acknowledges that the Swift design makes sense for the UI domain when the Unicode data actually stays up-to-date: "It’s easy to believe that the Swift approach nudges programmers to write more extended grapheme cluster-correct code and that the design makes sense to a language meant primarily for UI programming on a largely evergreen platform (iOS)."
- olliej 7y agothe problem i had was your framing as being bad because the “size” of a string changes as library/OS versions change, whereas I believe that that is a desirable behavior. My point is generally that the only measurements a developer should ever care about are the visible glyph count and the byte/storage size.