Live data from Hacker News

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

davidamos.dev

61–70 of 247 posts

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

#61
> The answer is: it depends. There isn't a canonical way to reverse a string, at least that I'm aware of.

Unicode defines grapheme clusters[1] that represent "user-perceived characters" separating a string into those and reversing seems like a pretty good way to go about it.

1: http://www.unicode.org/reports/tr29/

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

#62
This is a nice dive into limitations in Python's unicode handling and at the end, how to work around some problems. But you could use languages with proper unicode support like Swift or Elixir (weirdly HN is fighting flags in comment code which makes examples header to demonstrate).

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

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

Even the most basic ASCII string is still a data structure.

Is it a PASCAL string (length byte followed by data) or a C string (arbitrary run of bytes terminated by a null character)?

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

#64
I'm not surprised the flag had two components, but I _was_ surprised the US flag was made by literally U and S, haha!

I definitely thought it'd be something like [I am a Flag] and [The flag ID between 0 and 65535]. And reversing it would be [Flag ID] + [I am a Flag] which would not be a defined "component" and instead rendered as the individual two nonsense characters.

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

#67
post #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

poor man gave me and many others something like half of our introduction to computer science, but has gotten far more fame as the "emoji guy" for his repeated bouts with this particular part of unicode :)

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

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

Thankfully, there isn't an assigned ISO 3166-1 2-letter country code for SU currently; people may have interesting reactions seeing what happens when reversing a US flag emoji if there were.

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

#69
post #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

That's more about the UTF-8 encoding than Unicode itself.

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

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

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

>what looks like one character is actually a special case joiner between two adjacent codepoints

Are you referring to a grouping not covered by the definition of grapheme clusters (which I am only passingly familiar with)? If so, then I don't think it's any more non-meaningful to reverse it than to reverse an English string. The result is gibberish to humans either way - it sounds more like you're saying that there is no universally "meaningful to humans" way to reverse some text in potentially any language, which is true regardless of what encoding or written language you're using. I was thinking of it more from the programmer side - i.e. that Unicode provides ways to reverse strings that are more "meaningful" (as opposed to arbitrary) than e.g. just reversing code points.

Post reply on HN