TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

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

189 pointsby da12over 3 years ago

58 comments

coreyp_1over 3 years ago
If you think the Unicode flag emoji take a lot of bytes, then consider the family emoji! (<a href="https:&#x2F;&#x2F;unicode.org&#x2F;emoji&#x2F;charts&#x2F;full-emoji-list.html#family" rel="nofollow">https:&#x2F;&#x2F;unicode.org&#x2F;emoji&#x2F;charts&#x2F;full-emoji-list.html#family</a>)<p>I&#x27;m in the process of designing a scripting language and implementing it in C++. I plan to put together a YouTube series about it. (Doesn&#x27;t everyone want to see Bison and Flex mixed with proper unit tests and C++20 code?)<p>Due to my future intended use case, I needed good support for Unicode. I thought that I could write it myself, and I was wrong. I wasted two weeks (in my spare time, mostly evenings) trying to cobble together things that should work, identifying patterns, figuring out how to update it as Unicode itself is updated, thinking about edge cases, i18n, zalgo text, etc. And then I finally reached the point where I knew enough to know that I was making the wrong choice.<p>I&#x27;m now using ICU. (<a href="https:&#x2F;&#x2F;icu.unicode.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;icu.unicode.org&#x2F;</a>) It&#x27;s huge, it was hard to get it working in my environment, and there are very few examples of it&#x27;s usage online, but after the initial setup dues are paid, it WORKS.<p>Aside: Yes, I know I&#x27;m crazy for implementing a programming language that I intend for serious usage. Yes, I have good reasons for doing it, and yes I have considered alternatives. But it&#x27;s fun, so I&#x27;m doing it anyways.<p>Moral of the story: Dealing with Unicode is hard, and if you think it shouldn&#x27;t be that hard, then you probably don&#x27;t know enough about the problem!
评论 #30106201 未加载
评论 #30106046 未加载
评论 #30113328 未加载
评论 #30109212 未加载
评论 #30112058 未加载
评论 #30108854 未加载
WA9ACEover 3 years ago
I feel like I&#x27;m obligated to share this almost 20 year old Spolsky post that gave me my understanding of characters.<p><a href="https:&#x2F;&#x2F;www.joelonsoftware.com&#x2F;2003&#x2F;10&#x2F;08&#x2F;the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.joelonsoftware.com&#x2F;2003&#x2F;10&#x2F;08&#x2F;the-absolute-minim...</a>
评论 #30105444 未加载
zarzavatover 3 years ago
This reminds me of an interesting bug I saw where I was seeing a strange flag in some Arabic text. However when I copied the string and pasted it into a text editor, the flag of Saudi Arabia appeared instead (which made much more sense). After some vexillologic research on Wikipedia I identified the original flag as American Samoa and it suddenly all made sense. Turns out some broken RTL support was flipping the SA into AS at presentation.
评论 #30112732 未加载
yoyohello13over 3 years ago
Maybe I&#x27;m missing some prerequisite knowledge here, but why would I assume `flag=&quot;us&quot;` is an emoji? Looking at that first block of code, there is no reason for me to think &quot;us&quot; is a single character.<p>Edit: Turns out my browser wasn&#x27;t rendering the flags.
评论 #30105407 未加载
评论 #30105327 未加载
评论 #30107912 未加载
评论 #30105554 未加载
emodendroketover 3 years ago
What I&#x27;d like to know is, given the explosion of the character set for emoji, does the rationale for Han unification still make sense? The case for not allowing national variants seems less and less compelling with every emoji they add.<p>This is a bit of a hobby horse, but imagine if every time you read an article in English on your phone some of the letters were replaced with &quot;equivalent&quot; Greek or Cyrillic one and you can get an idea of the annoyance. Yeah, you can still read it with a bit of thought, but who wants to read that way?
评论 #30107645 未加载
评论 #30109743 未加载
评论 #30108478 未加载
评论 #30109106 未加载
treeskneesover 3 years ago
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.<p>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?
评论 #30105861 未加载
评论 #30106435 未加载
jerfover 3 years ago
So, in terms of acing interviews, increasingly one of the best answers to the question &quot;Write some code that reverses a string&quot; is that in a world of unicode, &quot;reversing a string&quot; is no longer possible or meaningful.<p>You&#x27;ll probably be told &quot;oh, assume US ASCII&quot; or something, but in the meantime, if you can back that up when they dig into it, you&#x27;ll look really smart.
评论 #30107502 未加载
评论 #30108184 未加载
评论 #30105392 未加载
评论 #30105891 未加载
评论 #30105674 未加载
评论 #30105625 未加载
评论 #30105379 未加载
评论 #30108843 未加载
dhosekover 3 years ago
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.
评论 #30105851 未加载
评论 #30107962 未加载
otagekkiover 3 years ago
If flag emojis are really a combination of 2 special characters, the reversal of the U.S. flag should result in having the Soviet Union flag.
评论 #30106446 未加载
评论 #30106836 未加载
评论 #30106410 未加载
评论 #30107599 未加载
chrismorganover 3 years ago
UTF-8 does not represent Unicode code points, but rather Unicode <i>scalar values</i>. The difference between the two is surrogates, the way that UTF-16 ruined Unicode: code points are 0₁₆ to 10FFFF₁₆, scalar values are 0₁₆ to D7FF₁₆ and E000₁₆ to 10FFFF₁₆. Yes, the author quoted Wikipedia, but Wikipedia is wrong on this point; surprisingly comprehensively wrong: the UTF-8 page completely ignores the distinction, and even the page on <i>code points</i> doesn’t mention scalar values! This error propagates to other places, too: for example, “and there are a total of 1,112,064 possible code points”: no, that’s how many scalar values there are; code points also include the 2,048 surrogates, so there are 1,114,112 code points.
Mesopropithecusover 3 years ago
Unfortunately the HN text input won&#x27;t let me do this, but a funny starter for the article would have been this:<p>&#x27;(Spanish flag)&#x27;[::-1]<p>basically &#x27;&#x27;.join([chr(127466), chr(127480)]) vs. &#x27;&#x27;.join([chr(127466), chr(127480)])[::-1]<p>I&#x27;ll add this to my collection of party tricks and show myself out.<p>Cool article!
Crazyontapover 3 years ago
This section on the linked Wikipedia article(1) is quite amazing on how the family emoji is rendered using a zero-width joiner<p>(1) <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Emoji#Joining" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Emoji#Joining</a><p>edit: forgot HN doesn&#x27;t render emojis. Better read it directly on Wikipedia i guess.
tlover 3 years ago
This is a nice dive into limitations in Python&#x27;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).
评论 #30106020 未加载
happytoexplainover 3 years ago
I guessed that it would become the USSR flag (US -&gt; SU), but apparently Unicode doesn&#x27;t define that one! I wonder why. That would have been humorous.
评论 #30105494 未加载
评论 #30105691 未加载
评论 #30105503 未加载
progbitsover 3 years ago
Semi-related (about length of emoji &quot;characters&quot;, not reversing): <a href="https:&#x2F;&#x2F;hsivonen.fi&#x2F;string-length&#x2F;" rel="nofollow">https:&#x2F;&#x2F;hsivonen.fi&#x2F;string-length&#x2F;</a><p>Previously discussed:<p><a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=20914184" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=20914184</a><p><a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=26591373" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=26591373</a><p>As for this article &amp; Python - as usual it is biasing towards convenience and implicit behavior rather than properly handling all edge cases.<p>Compare with Rust where you can&#x27;t &quot;reverse&quot; a string - that is not a defined operation. But you can either break it into a sequence of characters or graphemes and then reverse that, with expected results: <a href="https:&#x2F;&#x2F;play.rust-lang.org&#x2F;?version=stable&amp;mode=debug&amp;edition=2021&amp;gist=6335bb2c5cacf308ed5190deaba0121f" rel="nofollow">https:&#x2F;&#x2F;play.rust-lang.org&#x2F;?version=stable&amp;mode=debug&amp;editio...</a><p>(Sadly the grapheme segmentation is not part of standard library, at least yet)
评论 #30113421 未加载
jiveturkeyover 3 years ago
Interesting article. Written for beginners, conversationally. Has excessive amounts of whitespace, for &quot;readability&quot; I guess. But at the same time, it dives quite deep, which I don&#x27;t think this &quot;style&quot; of presentation matches up with the amount of time a more novice reader is going to devote to a single long form article.<p>As to the content, for all the deep dive, a simple link to <a href="https:&#x2F;&#x2F;unicode.org&#x2F;reports&#x2F;tr51&#x2F;#Flags" rel="nofollow">https:&#x2F;&#x2F;unicode.org&#x2F;reports&#x2F;tr51&#x2F;#Flags</a> and what an emoji is, would have saved so much exposition. I also wish he&#x27;d touched on normalization. With the amount of time he&#x27;s demanding from readers he could have mentioned this important subject. Because then he could discuss why (starting from his emoji example) a-grave (à) might or might not be reversible, depending how the character is composed.<p>Also wish he&#x27;d pointed to some libraries that can do such reversals.
utopcellover 3 years ago
There are unicode characters that reverse parsing order themselves. This has been the basis of a code injection attack, analyzed in [1].<p>[1] ``Trojan Source: Invisible Vulnerabilities&#x27;&#x27;: <a href="https:&#x2F;&#x2F;trojansource.codes&#x2F;trojan-source.pdf" rel="nofollow">https:&#x2F;&#x2F;trojansource.codes&#x2F;trojan-source.pdf</a>
uniqueuidover 3 years ago
Upper and lower codepoints are really way too obscure and can create issues you didn&#x27;t even know you had.<p>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.<p>Not fun.
techwiz137over 3 years ago
It&#x27;s pretty funny that reversing the American flag yields Soviet Union(SU).
cmyrover 3 years ago
Something I haven&#x27;t seen mentioned yet is one of the most annoying things about regional indicator symbols, which is that interpreting them correctly requires arbitrary backtracking, and handling this correctly is very annoying for things like text fields.<p>Basically: A single, unpaired RIS counts as a single grapheme. Similarly, a pair of RIS count as a single grapheme. Now imagine if your cursor position is after an RTS, and you arrow backwards (assuming LTR text, imagine your cursor is to the right of an RIS, and you press the left arrow.) Your textbox should now move the cursor to the left by one grapheme. How do you figure out where this is, in code units? You basically have to scan backwards until you find the first non-RIS codepoint, and then you have to match them up into pairs to figure out if your left-arrow movement should correspond to a movement of one or two codepoints.<p>This is a longstanding source of bugs, and if you&#x27;re bored you can play around with pasting a huge sequence of flags into a textfield and then trying to navigate around it with the arrow keys&#x2F;mouse. There are some broken implementations out there.<p>edit: while I&#x27;m thinking about this I will point out that an alternative design, which would have solved this problem (and which was first pointed out to me by @raphlinus) would have been to have two separate sets of RI symbols, one for &#x27;first position&#x27; and one for &#x27;second position&#x27;; then you could always determine the appropriate cursor position without needing context. Isn&#x27;t hindsight a wonderful thing?
评论 #30113469 未加载
sundarurfriendover 3 years ago
Julia docs do a (surprisingly) good job of being clear and explicit about this: the docstring for `reverse(AbstractString)` says:<p>&gt; Reverses a string. Technically, this function reverses the codepoints in a string and its main utility is for reversed-order string processing [...]. See also [...] `graphemes` from module Unicode to operate on user-visible &quot;characters&quot; (graphemes) rather than codepoints.<p>Properly reversing a string of flags (or any other grapheme clusters) is just a `using Unicode: grapheme` away.
mappuover 3 years ago
If you like this, you may also like why len(emoji) is still not 1 in Python 3 despite all the unicode breakage: <a href="https:&#x2F;&#x2F;storytime.ivysaur.me&#x2F;posts&#x2F;grapheme-clusters&#x2F;" rel="nofollow">https:&#x2F;&#x2F;storytime.ivysaur.me&#x2F;posts&#x2F;grapheme-clusters&#x2F;</a><p>I do feel like these are all &#x27;gotcha&#x27; questions - I haven&#x27;t seen any real-world requirement to reverse a string and then have it be displayed in a useful way.
qqiiover 3 years ago
&gt; 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?<p>So are there any good libraries that can deal with code points that are merged together into a single pictographic and reverse them &quot;as expected&quot;?
评论 #30107939 未加载
kevin_thibedeauover 3 years ago
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&#x27;t need surrounding context to parse them out like flags need.
评论 #30106075 未加载
评论 #30105508 未加载
a_cover 3 years ago
Understanding unicode would make the question more obvious<p><a href="https:&#x2F;&#x2F;www.joelonsoftware.com&#x2F;2003&#x2F;10&#x2F;08&#x2F;the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.joelonsoftware.com&#x2F;2003&#x2F;10&#x2F;08&#x2F;the-absolute-minim...</a>
Waterluvianover 3 years ago
I wish languages did a far better job clearly distinguishing between their operations:<p>1. You are acting in byte space and it’s pretty unambiguous what should happen. We are not acknowledging the semantics of language and alphabets.<p>2. You’re acting in language space and these operations will behave the way you probably think they should (depending on your cultural expectations, probably)
Beldinover 3 years ago
Interestingly, on my phone the so-called flag is not a flag at all, but &quot;US&quot; in outline.<p>So python behaves as expected: the 2 character string, when reversed, becomes &quot;SU&quot;. Similar stuff happens with the other &quot;flag&quot; strings.<p>I&#x27;m sure emojis in my phone are outdated. I&#x27;m not sure how that affects whether I see a flag or letters.
评论 #30105997 未加载
评论 #30108356 未加载
codingkevover 3 years ago
Yes, this allows for easy building of flag emojis as long as you know the ISO 3166 two-letter country code.<p>Example: <a href="https:&#x2F;&#x2F;github.com&#x2F;kennell&#x2F;flagz&#x2F;blob&#x2F;master&#x2F;flagz.py" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;kennell&#x2F;flagz&#x2F;blob&#x2F;master&#x2F;flagz.py</a>
michaelsbradleyover 3 years ago
See chapter 7 in <i>Hacking the Planet (with Notcurses)</i> for a short treatment of encodings, extended grapheme clusters, etc.<p><a href="https:&#x2F;&#x2F;nick-black.com&#x2F;htp-notcurses.pdf#page53" rel="nofollow">https:&#x2F;&#x2F;nick-black.com&#x2F;htp-notcurses.pdf#page53</a>
xmprtover 3 years ago
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&#x27;t see why computers can&#x27;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&#x27;re maintaining the ordering of those runes in the reversed string. Of course that means that naive string reversing doesn&#x27;t work anymore but naive string reversing wouldn&#x27;t work in the world of UTF-8 if we just went byte by byte.
评论 #30105533 未加载
评论 #30111255 未加载
raffyover 3 years ago
Kinda related: I am developing a library for ENS (Ethereum Name Service) name normalization: <a href="https:&#x2F;&#x2F;github.com&#x2F;adraffy&#x2F;ens-normalize.js" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;adraffy&#x2F;ens-normalize.js</a><p>I&#x27;m trying to find the best combination of UTS-46, UTS-51, UTS-39, and prior work on IDN resolution w&#x2F;r&#x2F;t confusables: <a href="https:&#x2F;&#x2F;adraffy.github.io&#x2F;ens-normalize.js&#x2F;test&#x2F;report-confusables.html" rel="nofollow">https:&#x2F;&#x2F;adraffy.github.io&#x2F;ens-normalize.js&#x2F;test&#x2F;report-confu...</a><p>Personally, I found the Unicode spec very messy. Critical information is all over the place. You can see the direct effect of this when you compare various packages across different languages and discover that every library disagrees in multiple places. Even JS String.normalize() isn&#x27;t consistent in the latest version of most browsers: <a href="https:&#x2F;&#x2F;adraffy.github.io&#x2F;ens-normalize.js&#x2F;test&#x2F;report-nf.html" rel="nofollow">https:&#x2F;&#x2F;adraffy.github.io&#x2F;ens-normalize.js&#x2F;test&#x2F;report-nf.ht...</a> (fails in Chrome, Safari)<p>The major difference between ENS and DNS is emoji are front and center. ENS resolves by computing a hash of a name in a canonicalized form. Since resolution must happen decentralized, simply punting to punycode and relying custom logic for Unicode-handling isn&#x27;t possible. On-chain records are 1:1, so there&#x27;s no fuzzy matching either. Additionally, ENS is actively registering names, so any improvement to the system must preserve as many names as possible.<p>At the moment, I&#x27;m attempting to improve upon the confusables in the Common&#x2F;Greek&#x2F;Latin&#x2F;Cyrillic scripts, and will combine these new grouping with the mixed-script limitations similar to IDN handling in Chromium.<p>Interactive Demo: <a href="https:&#x2F;&#x2F;adraffy.github.io&#x2F;ens-normalize.js&#x2F;test&#x2F;resolver.html" rel="nofollow">https:&#x2F;&#x2F;adraffy.github.io&#x2F;ens-normalize.js&#x2F;test&#x2F;resolver.htm...</a><p>Also this emoji report is pretty cool: <a href="https:&#x2F;&#x2F;adraffy.github.io&#x2F;ens-normalize.js&#x2F;test&#x2F;report-emoji.html" rel="nofollow">https:&#x2F;&#x2F;adraffy.github.io&#x2F;ens-normalize.js&#x2F;test&#x2F;report-emoji...</a>
jupp0rover 3 years ago
With all the criticism I normally have for Rust, I must say that its type safe handling of UTF-8 and its unambiguous distinction between byte strings and UTF-8 strings are extremely helpful in handling situations mentioned in the article correctly (and also efficiently).<p>Yes it&#x27;s a pain, but the way the standard library designed its types force you to handle conversions correctly, for example when byte arrays are converted to UTF-8 strings and may contain invalid UTF-8 sequences.
ineedasernameover 3 years ago
It&#x27;s an emoji... Are there any emojis with only one character? My assumption going in would be that any emoji is &gt; 1 character. Admittedly, despite lots of string processing, I never have to deal with emojis so I guess I&#x27;m not sure.<p>An interesting exercise would be emoji detection during string reversal to preserve the original emoji. I though something like that would be the crux of the article.<p>Am I wrong about single character emojis?
评论 #30111598 未加载
faebiover 3 years ago
Why reverse them if one barely can implement, display and edit them correctly. I never could make them work perfectly in VIM. Also I had to open a bug in Firefox recently:<p><i>Flag emojis and others are displayed in double the size on Windows 10 using Firefox Nightly</i> <a href="https:&#x2F;&#x2F;bugzilla.mozilla.org&#x2F;show_bug.cgi?id=1746795" rel="nofollow">https:&#x2F;&#x2F;bugzilla.mozilla.org&#x2F;show_bug.cgi?id=1746795</a>
评论 #30111616 未加载
aidenn0over 3 years ago
&gt; The answer is: it depends. There isn&#x27;t a canonical way to reverse a string, at least that I&#x27;m aware of.<p>Unicode defines grapheme clusters[1] that represent &quot;user-perceived characters&quot; separating a string into those and reversing seems like a pretty good way to go about it.<p>1: <a href="http:&#x2F;&#x2F;www.unicode.org&#x2F;reports&#x2F;tr29&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.unicode.org&#x2F;reports&#x2F;tr29&#x2F;</a>
jugover 3 years ago
I&#x27;m not surprised the flag had two components, but I _was_ surprised the US flag was made by literally U and S, haha!<p>I definitely thought it&#x27;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 &quot;component&quot; and instead rendered as the individual two nonsense characters.
评论 #30106301 未加载
nextstepover 3 years ago
Compare all of this nonsense to how it’s done in Swift. String APIs in Swift are great: intuitive and do what you expect.
alfredxingover 3 years ago
Related — I did a deep dive a couple years ago on emoji codepoints and how they&#x27;re encoded in the Apple emoji font file, with the end goal of extracting the embedded images — <a href="https:&#x2F;&#x2F;github.com&#x2F;alfredxing&#x2F;emoji" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;alfredxing&#x2F;emoji</a>
sltkrover 3 years ago
So what was the deal with the Scottish flag?
评论 #30105664 未加载
评论 #30105734 未加载
zwerdldsover 3 years ago
In normal conditions you can check for a ZWJ, but with regional coding chars, you would have to consider the regional chars block as a single char in the reversal. Given that is isn&#x27;t necessarily locale dependant but presentation layer dependant, there might not be anough info to decide how to act.
codezeroover 3 years ago
You also can&#x27;t URL Encode a string (In JS at least) if you truncate an emoji at the beginning or end of it.
评论 #30113594 未加载
zanzibar735over 3 years ago
Of course you can reverse a string with a flag emoji. You just need to treat a &quot;string&quot; as a collected of Extended Grapheme Clusters, and then you reverse the order of the EGCs. So if the string is `a&lt;flag unicode bytes&gt;b`, the output should be `b&lt;flag unicode bytes&gt;a`.
bandyabootover 3 years ago
Would be interesting to see the list of flag emojis that, when reversed, become a different flag emoji.
评论 #30106427 未加载
demetriusover 3 years ago
It&#x27;s sad that Unicode doesn&#x27;t include flags for dissolved countries. If it did, reversing an US flag would make a Soviet Union flag (code SU). This would make the text much more fun
评论 #30112456 未加载
heystefanover 3 years ago
Oooh I know this one, I&#x27;ve read it here last year: <a href="https:&#x2F;&#x2F;tonsky.me&#x2F;blog&#x2F;emoji&#x2F;" rel="nofollow">https:&#x2F;&#x2F;tonsky.me&#x2F;blog&#x2F;emoji&#x2F;</a>
ezfeover 3 years ago
Works in Swift, which is the benefit of Swift having the most painful String API possible:<p>let v = &quot;Flag: &quot; String(v.reversed()) &#x2F;&#x2F; Output: :galF v.count &#x2F;&#x2F; Output: 7
nitelyover 3 years ago
You can, but you need to break the string into graphemes first.
kart23over 3 years ago
Google also interprets emojis funny. Google the Estonian and South Sudan flag (f&quot;{chr(127466)*2+chr(127480)*2}&quot;) and you get results for Spain.
architectdroneover 3 years ago
humorously, on my local machine, I only see the string &quot;us&quot;, and was rather confused when he was asserting that it was a single character :D
评论 #30108346 未加载
randpxover 3 years ago
Try reversing the Canadian flag (CA) and you get the Ascension Island Flag (AC). Great article, but completely misses the point.
qwerty456127over 3 years ago
If the US flag is 2 special symbols saying US, why doesn&#x27;t reversing it just produce the flag of the Soviet Union?
评论 #30112161 未加载
ts4zover 3 years ago
Let me cheat a bit and say Unicode comes in three flavors: UTF-8, UCS-2 aka UTF-16, and UTF-32. UTF-8 is byte-oriented, UTF-16 is double-byte oriented, and UTF-32 nobody uses because you waste half the word almost all of the time.<p>You can&#x27;t reduce the <i>bytes</i> in UTF-8 or UTF-16, because you&#x27;ll scramble the encoding. But you could parsing the string, codepoint-at-a-time, handling the specifics of UTF-8, or UTF-16 with its surrogate pairs, and reversing those. This sounds equivalent to reversing UTF-32, and I believe is what the original poster was imagining.<p>Except you can&#x27;t do that, because Unicode has composing characters. Now, I&#x27;m American and too stupid to type anything other than ASCII, but I know about n+~ = ñ. If you have the pre-composed version of ñ, you can reverse the codepoint (it&#x27;s one codepoint). If you don&#x27;t have it, and you have n+dead ~, you can&#x27;t reverse it, or in the word &quot;año&quot; you might put the ~ on the &quot;o&quot;. (Even crazier things happen when you get to the ligatures in Arabic; IIRC one of those is about 20 codepoints.)<p>So we can&#x27;t just reverse codepoints, even ancient versions of Unicode. Other posters have talked about the even more exotic stuff like Emoji + skin tone. It&#x27;s necessary to be very careful.<p>Now, the old fart in me says that ASCII never had this problem. But the old fart in me knows about CRLF in text protocols, and that&#x27;s never LFCR; and that if you want to make a ñ in ASCII you must send n ^H ~. I guess you can reverse that, but if you want to do more exotic things it becomes less obvious.<p>(IIRC UCS-2 is the deadname, now we call it UTF-16 to remind us to always handle surrogate pairs correctly, which we don&#x27;t.)<p>TLDR: Strings are hard.
smegsicleover 3 years ago
did they think all those skintone emojis are individual codepoints?
评论 #30105534 未加载
评论 #30105425 未加载
mlindnerover 3 years ago
The person tries to define character when there isn&#x27;t actually any definition of what that even means. Character is a term limited to languages that actually use them and not all text is made up of characters.
nottorpover 3 years ago
So basically unicode along with c++ are great job security if you do bother to learn them.<p>There&#x27;s another word that comes to mind when thinking about those two: metastasis.
hougaardover 3 years ago
In other news, water is wet :)
exdsqover 3 years ago
Am I missing something or is this Day 1 of a programming course in C?
midjjiover 3 years ago
And this is why char should have been byte from the start.