Live data from Hacker News

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

davidamos.dev

11–20 of 247 posts

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

#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/

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

#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 naive string reversing doesn't work anymore but naive string reversing wouldn't work in the world of UTF-8 if we just went byte by byte.

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

#13

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.

In Windows Chrome, it doesn't render the emoji for me. In Android Chrome, it renders a flag emoji - not the raw region indicators (which look like the letters "u" and "s").

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

#14
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.

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

#16
post #2

I feel like I'm obligated to share this almost 20 year old Spolsky post that gave me my understanding of characters. https://www.joelonsoftware.com/2003/10/08/the-absolute-minim...

In that same vein, here's my introduction to Unicode about 10 years ago from Tom Scott.

https://www.youtube.com/watch?v=MijmeoH9LT4

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

#17

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.

In my browser (Firefox on Windows), the thing between the quotes in the first block of code looks like a picture of the US flag cropped to a circle, not like the characters "us".

Ah I see, I just opened it in firefox. It looks like some JS library is not getting loaded in Edge. The author was talking about "us", "so", etc. looking like one character and I thought I was going crazy, lol.

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

#18
Upper and lower codepoints are really way too obscure and can create issues you didn't even know you had.

I once had the very unpleasant experience of debugging a case where data saved with R on windows and loaded on macOS ended up with individually double-encoded codepoints.

Not fun.

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

#19
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/

> 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.

I really highly doubt it.

How do you reverse this?: مرحبًا ، هذه سلسلة.

Can you do it without any knowledge about whether what looks like one character is actually a special case joiner between two adjacent codepoints that only happens in one direction? Can you do it without knowing that this string appears wrongly in the HN textbbox due to an apparent RTL issue?

It's just not well-defined to reverse a string, and the reason we say it's not meaningful is that no User Story ever starts "as a visitor to this website I want to be able to see this string in opposite order, no not just that all the bytes are reversed, but you know what I mean."

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

#20
On the challenge front, there are things like á which might be a single code point or two code points (a+´). Then there are the really challenging things like ᾷ where if the components are individual characters, the order of ͺ and ῀ are not guaranteed to be consistent.
Post reply on HN