News for hackers. Stuff that matters.
Re:String length functions for single emoji characters evalu... by throwawayffffas on Friday March 26, 2021 @10:33AM (#26592612)
Python 3's approach is clearly the best. Because it focuses on the problem at hand, unicode codepoints. A string in python is a sequence of unicode codepoints, it's length should be the number of codepoints in the sequence, it has nothing to do with bytes.

To draw an absurd parallel "[emoji]".len() == 17 is equivalent to [1,2].len() == 8 (2 32 bit integers)

In my opinion the most useful result in the case the article describes is 5. There should of course be a way to get 1 (the number of extended graphemes), but it should not be the strings "length".

Re:String length functions for single emoji characters evalu... by nerdponx on Friday March 26, 2021 @10:43AM (#26592738)
Don't Swift and Go support iterating over graphemes? Edit: yes, Swift is mentioned at the bottom of the article.

It'd be great to have a function for that in other scripting languages like Python, Ruby, etc.

There was an interesting sub-thread here on HN a while ago about how a string-of-codepoints is just as bad as a string-of-bytes (subject to truncation, incorrect iteration, incorrect length calculation) and that we should just have string-of-bytes if we can't have string-of-graphemes. I don't agree, but some people felt very strongly about it.

Re:String length functions for single emoji characters evalu... by banthar on Friday March 26, 2021 @10:48AM (#26592808)
Defining string as a sequence of unicode codepoints is the mistake.

Nobody ever cares about unicode codepoints. You either want the number of bytes or the width of the string on screen.

UTF-32 codepoints waste space and give you neither.

Re:String length functions for single emoji characters evalu... by stkdump on Friday March 26, 2021 @02:25PM (#26595258)
The width on the screen is in pixels. Yes, I find monospace fonts increasingly pointless.
Re:String length functions for single emoji characters evalu... by anaerobicover on Friday March 26, 2021 @11:59AM (#26593637)
But what do you do when you're processing a string with codepoints that compose into one user-visible glyph?

    >>> len("🇨🇦")
    2
Re:String length functions for single emoji characters evalu... by jxy on Friday March 26, 2021 @03:42PM (#26596098)
yeah, and what do you do when you got a nonexistent country: "🇩🇧".
Re:String length functions for single emoji characters evalu... by Symbiote on Friday March 26, 2021 @05:12PM (#26597002)
Your example changes length depending on the font. (If a new country gets the code DB, fonts will gradually be updated to include the flag.)

I'll bet there are some systems in the PRC that don't include 🇹🇼.