Live data from Hacker News

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

davidamos.dev

31–40 of 247 posts

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

#31
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 no longer possible or meaningful.

If you really wanted to, you could write a string reversal algorithm that treated two-character emojis as an indivisible element of the string and preserved its order (just as you'd need to preserve the order of the bytes in a single multi-byte UTF-8 character). You'd just need to carefully specify what you mean by the terms "string", "character" and "reverse" in a way that includes ordered, multi-character sequences like flag emojis.

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

#32

Earlier quoted context omitted.

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.

A whole lesson in Unicode in itself right there with your experience, haha!

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

#33

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.

Nope, because the repurposing is independent of how the Unicode is represented. There's absolutely no advantage to having a string in UTF-32 over UTF-8 since you'll still need to examine every character and the added overhead for converting byte strings in UTF-8 to 32-bit code points is by far offset by the huge memory increase necessary to store UTF-32.

What's more, it's really not that difficult to start at the end of a valid UTF-8 string and get the characters in reverse order. UTF-8 is well-designed that way in that there's never ambiguity about whether you're looking at the beginning byte of a code point.

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

#35
post #28

So what was the deal with the Scottish flag?

From Wikipedia:

> A separate mechanism (emoji tag sequences) is used for regional flags, such as England 󠁧󠁢󠁥󠁮󠁧󠁿, Scotland 󠁧󠁢󠁳󠁣󠁴󠁿, Wales 󠁧󠁢󠁷󠁬󠁳󠁿, Texas 󠁵󠁳󠁴󠁸󠁿 or California 󠁵󠁳󠁣󠁡󠁿. It uses U+1F3F4 WAVING BLACK FLAG and formatting tag characters instead of regional indicator symbols. It is based on ISO 3166-2 regions with hyphen removed and lowercase, e.g. GB-ENG → gbeng, terminating with U+E007F CANCEL TAG. Flag of England is therefore represented by a sequence U+1F3F4, U+E0067, U+E0062, U+E0065, U+E006E, U+E0067, U+E007F.

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

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

UTF-8 reverse string has been a thing for a long time in most/all programming languages. It may not work perfectly in 100% of the cases, but that doesn't mean reversing a string is no longer possible.

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

#38

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.

As I understand it, there is no two-letter ISO code for the USSR because when they update the standard they remove countries that no longer exist. In at least one case they have reused a code point: CS has been both "Czechoslovakia" and "Serbia and Montenegro", neither of which currently exist.

As a result, two-letter ISO codes are useless for many potential applications, such as, for example, recording which country a book was published in, unless you supplement them with a reference to a particular version of the standard.

Is there a way of getting the Czechoslovakian flag as an emoji? And did Serbia and Montenegro get round to making a flag?

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

#39
> Challenge: How would you go about writing a function that reverses a string while leaving symbols encoded as sequences of code points intact? Can you do it from scratch? Is there a package available in your language that can do it for you? How did that package solve the problem?

So are there any good libraries that can deal with code points that are merged together into a single pictographic and reverse them "as expected"?

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

#40
post #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.

True, but Unicode explicitly defines "SU" as a deprecated combination, regardless of flags. Seems like they omit everything from the list of "no longer used" country codes, with some exceptions. I would think they would have no reason not to allow historical regions.
Post reply on HN