3 ms·
> What were you expecting to get when doing reverse on 🇨🇦? My future child sees only one character in that string, an emoji. (If you put the cursor to the ri
by csande17 6y ago
> What were you expecting to get when doing reverse on 🇨🇦?
My future child sees only one character in that string, an emoji. (If you put the cursor to the right of 🇨🇦 and press backspace, the whole flag goes away, so that must mean it's only one character, right?) If they reverse that string, they'd expect to get the same result back.
> The flag symbol is encoded using regional indicator symbol letters: C, A. When you reverse it you get regional indicator symbol letters A, C which represents Ascension Island.
Right, this is exactly what I'll have to explain! Just like how in UTF-8, "é" is encoded using two bytes: 0xC3 0xA9. So if you reverse that, you get bytes 0xA9 0xC3, which do not represent "é". It's the exact same problem, you've just moved up one level of abstraction. (The Python 3 version of the problem isn't unique to flags -- there are all kinds of modifiers and ZWJ sequences and combining characters that get messed up if you rearrange individual codepoints.)
A few languages have tried to solve this problem in other ways, like how Swift considers each character of a string to be a "grapheme cluster", but that comes with a lot of its own drawbacks.
- takeda 6y agoIMO this isn't really a Python issue though. By unicode standard how to present it is left to the frontend. Even displaying "CA" might be enough. With combining character sequences there's at least a normalization and you can transform to the right representation based on your need. As you mentioned that Swift allows one to operate on grapheme clusters, and it comes with advantages but also some issues as you pointed out. The topic is complex but Python 3 does what unicode standard specifies, while Python 2 would output a garbage in similar scenario.