Live data from Hacker News

Why can't you reverse a string with a flag emoji?

davidamos.dev

21–30 of 247 posts

Re: Why can't you reverse a string with a flag emoji?

#21

I guessed that it would become the USSR flag (US -> SU), but apparently Unicode doesn't define that one! I wonder why. That would have been humorous.

IIRC Unicode doesn't define country codes. It was a workaround for a political issue of which countries recognize which other countries.

It would have been difficult to get the CN delegation to sign off on a list that contained TW, although there are probably others.

Re: Why can't you reverse a string with a flag emoji?

#22

I guessed that it would become the USSR flag (US -> SU), but apparently Unicode doesn't define that one! I wonder why. That would have been humorous.

Unicode doesn't define any flags, really. That's up to the font rendering on systems/libraries.

Re: Why can't you reverse a string with a flag emoji?

#23

This misses the real problem with flag emoji in that they are composed of codepoints that can be in any order. With other emoji you get a base codepoint with potential combining characters. Using a table of combining character ranges you can skip over them and isolate the logical glyph sequences. You don't need surrounding context to parse them out like flags need.

Thanks for that interesting detail!

If such re-purposing continues, it might be easier to go straight to utf-32 for some use cases.

Re: Why can't you reverse a string with a flag emoji?

#24
post #12

This is a cool article about Unicode encoding however I still feel like it should be possible to reverse strings with Flag emojis. I don't see why computers can't handle multi rune symbols in the same way that they handle multi byte runes. We could combine all the runes that should be a single symbol and make sure that we're maintaining the ordering of those runes in the reversed string. Of course that means that nai…

Swift, for example, does what you're saying. I thought that the reason many languages don't do it that way is that part of the definition of an array (or at least expected-by-convention) is constant-time operations. If you treat a string as an array, then having to deal with variable-length units breaks that rule. That's why, when there is an API for dealing with grapheme clusters, it is usually a special case that duplicates an array-like API, instead of literally using an array.

I actually don't know how/why Python is apparently using code points, since they are variable length. That seems like a compromise between using code units and using grapheme clusters that gets you the worst of both worlds.

Edit: Maybe it uses UTF-32 under the hood when it's doing array operations on code points?

Re: Why can't you reverse a string with a flag emoji?

#25

did they think all those skintone emojis are individual codepoints?

When I first realized that the skin tone emojis were a code-point + a color code-point modifier, I tried to see what other colors there were and if I could apply those to other emojis. The immature child in me looked to see if there was a red color code point and if so, could I use it to make a "blood poop" emoji. Turns out.... no.

Re: Why can't you reverse a string with a flag emoji?

#26
post #11
post #6

So, in terms of acing interviews, increasingly one of the best answers to the question "Write some code that reverses a string" is that in a world of unicode, "reversing a string" is no longer possible or meaningful. You'll probably be told "oh, assume US ASCII" or something, but in the meantime, if you can back that up when they dig into it, you'll look really smart.

Reversing a string is still meaningful. Take a step back outside the implementation and imagine handing a Unicode string to a human. They could without any knowledge look at the characters they see and produce the correct string reversal. There is a solution to this which is to compute the list of grapheme clusters, and reverse that. https://unicode.org/reports/tr29/

Should it reverse a BOM as well or keep it first?

Re: Why can't you reverse a string with a flag emoji?

#27

Maybe I'm missing some prerequisite knowledge here, but why would I assume `flag="us"` is an emoji? Looking at that first block of code, there is no reason for me to think "us" is a single character. Edit: Turns out my browser wasn't rendering the flags.

I had the same issue when I read the article, I kept on getting stuck and asking myself what I was missing.
Post reply on HN