Live data from Hacker News

It’s not wrong that "🤦🏼‍♂️".length == 7 (2019)

hsivonen.fi

191–200 of 315 posts

Re: It’s not wrong that "🤦🏼‍♂️".length == 7 (2019)

#191

I'm not a fan of "everything you know about X is wrong" articles. Very often they try to present some little tidbit of knowledge as a revelation and mislead the reader in the process. In this case, the tidbit is: "grapheme clusters exist and they are useful". The misleading part is that the article draws a false equivalence between what the author calls "UTF-32 code units" and UTF-16 code units. UTF-32 code units are…

> UTF-16 code units, on the other hand, are an implementation detail of UTF-16. Would that it were only so. Instead, UTF-16 ruined Unicode for everyone with the abomination that is surrogates , and almost nothing that deals with UTF-16 actually asserts well-formedness, and ill-formed UTF-16 cannot be represented in UTF-8 or UTF-32. UTF-32 and UTF-8 code units are truly implementation details of their encodings, as ot…

I don't see what's inherently wrong with UTF-16 surrogates. If I am not wrong, a given UTF-16 codeunit is unambigously either a complete code point, a first surrogate, or a second surrogate.

Why should we expect invalid utf-16 strings to be representable in utf-8 or 32? I don't see anyone trying to represent invalid utf-8 in utf-16 or 32.

Re: It’s not wrong that "🤦🏼‍♂️".length == 7 (2019)

#192
post #81

Earlier quoted context omitted.

HN does, in fact, handle it, and chose to discard it.

You need to use the latest and greatest: Awww dang they updated the filter, booo

You can remove the word "they" from your comment and it'll still work for this site.

Re: It’s not wrong that "🤦🏼‍♂️".length == 7 (2019)

#193

Interesting; emojis also make SMS really weird. according to twilio including a emoji will change the character limit from 160 to 70

That’s because SMS has a 1120 bit message capacity and uses one of two encoding mechanisms.

GSM-7 uses 7 bits per character and a correspondingly limited character set that allows for 1120/7 = 160 characters.

UCS-2/UTF-16 uses 16 bits per character for a total of 1120/16 = 70 characters per message.

Re: It’s not wrong that "🤦🏼‍♂️".length == 7 (2019)

#194

Interesting; emojis also make SMS really weird. according to twilio including a emoji will change the character limit from 160 to 70

It's not weird because it's not a 160 limit in the sense of characters. Non-English language people are I guess more aware of this -- using a character not in the standard English a-z often counts as two characters.

Re: It’s not wrong that "🤦🏼‍♂️".length == 7 (2019)

#195
post #179
post #174

Earlier quoted context omitted.

Showing unintuitive results in a small number of cases always beats showing unintuitive results for all the cases. The Swift approach can also asymptotically reach perfection. The other approach will be broken forever. These are categorical improvements in design

The Swift approach can't reach perfection in isolation because data from the future can always break it. That's why in the article you see Swift running on Ubuntu 14.04 returning len==2 while the same code on Ubuntu 18.04 returns len==1 for the same emoji string. IMO that's a big philosophical question here: do we accept that "string length" means something you can't compute for arbitrary strings unless your code is…

Your example shows an improvement, which proves my point (also don't drop the word asymptotically, nothing can ever be perfect, that's not the issue, being closer to perfect is a positive)

And you can compute it, you can pin a Unicode version and ship it in the language if those platform differences are unbearable (so, you can actually isolate it and simply ignore the future :))

The bigger philosophical question: how much longer do we accept that "string length" does not measure the most intuitive measure of string length and call a byte a char?

Re: It’s not wrong that "🤦🏼‍♂️".length == 7 (2019)

#196

Why should length in programming be devoid of units? Why can't we have length be (8, UTF32_CODEPOINTS) ?

We can and do - they're called types. However it's always going to be up to the library developers as to whether two things are the same type.

Re: It’s not wrong that "🤦🏼‍♂️".length == 7 (2019)

#197

Really the correct way to design string APIs would be to not have an ambiguous "length" at all, but to always require specifying whether you want UTF8-bytes, memory bytes, code points, graphemes, whatever. However such an API would be pretty cumbersome because for all non-edge cases (read: a western language and a reasonable encoding that language - which when looking at world demographics is a very narrow way of say…

>Horrible horrible idea when used in conjunction with automatic type conversions. WriteLine($"The size is {3.5}"); shouldn't print "3.5" in the US and "3,5" somewhere else.

Actually why?

Re: It’s not wrong that "🤦🏼‍♂️".length == 7 (2019)

#198

Really the correct way to design string APIs would be to not have an ambiguous "length" at all, but to always require specifying whether you want UTF8-bytes, memory bytes, code points, graphemes, whatever. However such an API would be pretty cumbersome because for all non-edge cases (read: a western language and a reasonable encoding that language - which when looking at world demographics is a very narrow way of say…

Swift defaults to grapheme clusters, and allows you to request other lengths on-demand.

Re: It’s not wrong that "🤦🏼‍♂️".length == 7 (2019)

#199

Earlier quoted context omitted.

It is wrong that "{emoji}".length == 7 -- but it's wrong because there's no such thing as the 'length' of a string out of context. A string should be viewed as an opaque data type with views into it depending on what you're trying to do. You can have its length in the context of storage/retrieval/transmission (UTF-8 byte count), its length in the context of parsing (code points), its length in the context of editing…

There are three notions of length that make sense: 1. UTF-8 byte length 2. Code point count 3. Extended grapheme cluster count #3 makes sense for users but it doesn’t make sense for programs which often need to work at the code point level. I expect programming language string length to obey the law: len(a ++ b) = len(a) + len(b) For example, if I concatenate a two strings, one containing an “e” and one containing a…

> Code point length is the most useful for people who are actually writing string algorithms based upon Unicode.

What algorithms would you be writing against code points?

Re: It’s not wrong that "🤦🏼‍♂️".length == 7 (2019)

#200

Earlier quoted context omitted.

> UTF-16 code units, on the other hand, are an implementation detail of UTF-16. Would that it were only so. Instead, UTF-16 ruined Unicode for everyone with the abomination that is surrogates , and almost nothing that deals with UTF-16 actually asserts well-formedness, and ill-formed UTF-16 cannot be represented in UTF-8 or UTF-32. UTF-32 and UTF-8 code units are truly implementation details of their encodings, as ot…

WTF-8 is bridging the gap here. I don't know of a UTF-32 equivalent, maybe it's impossible.

The UTF-32 equivalent is just the original UCS-4 — simply not enforcing any restrictions on the 32-bit value. Probably most code using UTF-32 does this, at least internally. (I can understand using high bits for metadata or non-Unicode points and have done so, but I don't see any reason for testing for surrogates outside of encoding/decoding UTF-16; they are indeed an abomination.)
Post reply on HN