Live data from Hacker News

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

davidamos.dev

41–50 of 247 posts

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

#41
post #28

So what was the deal with the Scottish flag?

Most flags use the ISO 2-character country code to access their values. However, some flags don't map to 2-character country codes (Scotland being one example). In this case it uses the sequence black flag, GBSCT (for Great Britain-Scotland, represented using the tag latin small letter codes for the letters) then cancel tag to end the sequence. Changing the middle five to be GBENG gives the English flag and GBWLS gives the Welsh flag.

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

#42
But you can, and did, reverse a string. It seems you would need more details, such as a request to reverse the meaning or interpretation of the string, which is what the author is getting at.

If someone challenges you to reverse an image, what do you do? Do you invert the colors? Mirror horizontally? Mirror vertically? Just reverse the byte order?

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

#43
post #26
post #11

Earlier quoted context omitted.

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?

Keep it first? Like that’s not a gotcha. Your input is a string and the output is that string visually reversed. What it looks like in memory is irrelevant.

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

#44
Interestingly, on my phone the so-called flag is not a flag at all, but "US" in outline.

So python behaves as expected: the 2 character string, when reversed, becomes "SU". Similar stuff happens with the other "flag" strings.

I'm sure emojis in my phone are outdated. I'm not sure how that affects whether I see a flag or letters.

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

#47
post #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…

Ah, I didn't realize they reused codes from ISO 3166-3. I figured, because they keep these regions around in their own set, that was some implication that the codes would not be reused.

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

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

Which is why these APIs should always make normalization available: https://unicode.org/reports/tr15/

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

#50

But you can, and did, reverse a string. It seems you would need more details, such as a request to reverse the meaning or interpretation of the string, which is what the author is getting at. If someone challenges you to reverse an image, what do you do? Do you invert the colors? Mirror horizontally? Mirror vertically? Just reverse the byte order?

There's a specification problem here. I like to say that a "string" isn't a data structure, it's the absence of one. Discussing "strings" is pointless. It follows that comparing programming languages by their "string" handling is likewise pointless.

Case in point: a "struct" in languages like C and Rust is literally a specification of how to treat segments of a "string" of contiguous bytes.

Post reply on HN