149 points · by luu · 8y ago · 134 comments
[1]: https://dev.mysql.com/doc/refman/8.0/en/charset-unicode-utf8...
[2]: https://medium.com/@adamhooper/in-mysql-never-use-utf8-use-u...
[3]: https://stackoverflow.com/questions/202205/how-to-make-mysql...
[4]: https://www.eversql.com/mysql-utf8-vs-utf8mb4-whats-the-diff...
[1]: https://stackoverflow.com/questions/5567249/what-are-the-mos...
Did we use to work together? Because I've been one of those poor DBAs to inherit a system that was not using utf8mb4 charsets, and sure damn enough an emoji took down said system for hours affecting every client on the books.
They recovered, but limped along for another two years before eventually getting bought out and effectively soft-shutting down.
I was hired post-Armageddon to keep the lights on and eventually left once the acquisition completed.
When I did a web project in university in 2014, the assistant professor warned me at the beginning that this will happen with MySQL and Emoji, because he had the problem the last semester.
And then in 2017, years after I learned that this was a huge problem, I got a freelance app project where they already had a back-end developer. The app crashed multiple times after release when I finally realized the back-end dev didn't know anything about this and simply clicked together the DB in phpmyadmin with all the default settings...
Did we use to work together? Because I've been one of those poor DBAs to inherit a system that was not using utf8mb4 charsets, and sure damn enough an emoji took down said system for hours affecting every client on the books.
Perhaps ;) My first major project out of current job was spending two months figure out why our system doesn't support emojis. And MySQL was the blame, I want my two months back.Hello JIRA Servicedesk, which at least half a year ago still preferred "old" utf8. Over the winter holidays I'll have to, basically, reinstall the entire fucking thing.
Came across this the last time I used mysql, fortunately before it was too far into the project. In any case, every time I've ever used mysql, there's something that pisses me off about it. Like indexing on a BINARY field is using case-insensitive comparisons, like it's actually text.
https://bugzilla.mozilla.org/show_bug.cgi?id=776048
The tl;dr is that MDN is a multilingual site, and supports categorization in a few ways, including by tagging. An example of the bug: the English MDN's CSS reference tagged articles with "CSS Reference". The French MDN's CSS reference tagged them "CSS Référence". Sometimes, the French tag appeared on English articles.
The source of the issue was MySQL's utf8_general_ci collation, which did not see "e" and "é" as distinct, so it was a toss-up as to which tag you'd get back from the database. The solution was to build and install a custom collation to teach MySQL to see accented and unaccented characters as distinct.
I'm curious, what are their arguments? UTF-16 is pretty objectively bad at everything, it's not compatible with ascii and C-strings like UTF-8 and it's not a decent constant-width encoding for codepoints like UTF-32 (and even that is not necessarily useful anyway since you often care more about characters than codepoints). AFAIK the only advantage of UTF-16 is that it's a little more efficient when encoding text written in certain scripts.
Even UTF-32 is not a fixed-sized character encoding!! It's a fixed-sized codepoint encoding. Characters can be composed of more than one codepoint. Even if you think "hey, I'll use pre-composed codepoints", you'll fail because not every legitimate, canonically decomposed character has a single-codepoint pre-composition.
The disadvantages to UTF-16 far outweigh its one advantage (the one you mentioned).
(There is one more advantage to UTF-16, and it's that Win32 uses it all over the place. But that's not much of an advantage, and Windows does seem to be making progress towards putting UTF-8 on an equal footing (if not better).)
That turned out not to be true but binary compatibility made switching to UTF-8 impossible.
WTF-16 "standard" is just UTF-16 that allows invalid surrogate pairs. Windows kernel style handling.
It's a shame you were voted down for a perfectly legitimate (if misleading) question.
This is a very common gotcha most developers ignore until they encounter their first invalid surrogate pair file path... UTF-16 can't represent all possible Windows file names or paths.
I have to imagine that you're just jesting :0
Even if you you do require chars(str) for large strings outside the BMP those characters were so rare before emojis that you could waste a single bit on "contains any non-BMP?" and almost always do the work in O(1) time as opposed to O(n) for UTF-8.
It’s often faster. When you process non-English text in UTF8, branch prediction fails all the time because all characters have random size from 1 to 3 bytes. When you process non-English text in UTF16, branch prediction predicts all the time: surrogate pairs are extremely rare so the 99.9% of the stream is 2 bytes/character.
> it's not compatible with ascii and C-strings like UTF-8
In modern C++, wchar_t is a built-in fundamental type: http://www.cplusplus.com/reference/cwchar/wchar_t/
But with the slight problem that it actually means UTF-32 on everything on non-Windows (there's char16_t (C++11) and char32_t (C++20), but somewhat obnoxiously, they're new types, so you need explicit casts for pointer conversion)
Now, there's __STDC_ISO_10646__, which, if it is defined by an implementation, means that wchar_t is guaranteed to be a Unicode codepoint. So e.g. glibc defines that, and on Linux you can therefore assume wchar_t as UTF-32 regardless of locale. But BSDs don't. And on Windows, nothing that uses 16-bit wchar_t can use that define, since 16 bits aren't wide enough to be conforming.
UTF-16 can be faster if you're iterating through characters, but for parsing iterating through bytes is usually sufficient. It's fine to use something like plain old strchr to search for a { or a < in a UTF-8 string. Characters only get really important when you want to print the string.
In simple terms, you can't just treat it as an array to index to any given character.
There's also all the detriments that still apply. (Including UTF-16LE / UTF-16BE, BOM, not being able to concatenate two valid string sequences (blind) and always have a valid result, etc.)
You're also incorrect, or at least not correctly describing the search operation.
The assumption about finding specific characters (for example, command flags) in a string or array of strings is also... complicated.
As an example: https://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms
For /command/ flags the half and full width equivalent characters might want to be 'folded' back over the traditional ASCII namespace.
Sometimes a specific loss of precision can be desirable.
Correct.
> only get really important when you want to print the string.
Print, split into fixed-length pieces, render, layout, typeset — GUI apps do that a lot with their strings.
Even web browser do, probably that’s why JavaScript strings are UTF-16 despite web in general is mostly UTF-8, see section 6.1.4 “The String Type” on page 67: http://www.ecma-international.org/publications/files/ECMA-ST...
Perl6 decodes to NFG form which provides a 1:1 mapping between characters and scalar values.[1] That makes Unicode string munging almost as easy as ASCII.[2] But it requires always preprocessing and intern'ing your strings.
[1] Multi-codepoint graphemes are dynamically assigned a unique internal codepoint.
[2] I say almost because some Unicode text rules, like those for word and paragraph breaks, can never be implemented as simple as in ASCII.
For plain text, yes. UTF-16 covers the Basic Multilingual Plane in 2 bytes per character, UTF-8 covers the BMP in at most 3 bytes per character. However, since most (web) content is dominated by markup in ASCII, which is 1 byte per character in UTF-8 and 2 bytes per character in UTF-16, UTF-8 typically wins even with Asian scripts.
Taking some random zh Wikipedia page in UTF-8:
curl "https://zh.wikipedia.org/wiki/威蘭運河" | wc -c
78543
curl "https://zh.wikipedia.org/wiki/威蘭運河" | wc -m
71003
So, that's 1.11 bytes per character on average, compared to at least 2 bytes per character if it was encoded in UTF-16.(Of course, adding compression is going to diminish the differences anyway. A very primitive compression algorithm in such cases would be recoding your UTF-16 to UTF-8 :p.)
curl -Ss "https://zh.wikipedia.org/wiki/威蘭運河" | iconv -t utf-16 | wc -c
142008
There are only 3799 non-ASCII characters on that page: curl -Ss "https://zh.wikipedia.org/wiki/威蘭運河" | tr -d '\1-\176' | wc -m
3799
Compared with 67204 ASCII ones (tr -cd).Better default for what? I agree UTF-8 is better default for HTML sent over network. For different context, the better default can be different.
> side from it being the default on Windows
Also default in Java (including Android userland), C#, iOS & OSX, python 3 (unless compiled with UTF32 support but the default is UTF16), Symbian, OpenOffice, QT, most XML parsers (e.g. Xerces), and many other languages, libraries and frameworks. Even JavaScript uses UTF16 strings.
Not the case since PEP 393 / Python 3.3.
Also pretty much all Linux distros used wide builds.
> Also default in Java (including Android userland), C#, iOS & OSX, python 3 (unless compiled with UTF32 support but the default is UTF16), Symbian, OpenOffice, QT, most XML parsers (e.g. Xerces), and many other languages, libraries and frameworks. Even JavaScript uses UTF16 strings.
Most of these don't actually use UTF16 but UCS2 with surrogates (sometimes nicknamed WTF16).
Not true since Python 3.3. It now uses latin1/ucs-2/ucs-4 based on string contents (smallest encoding that can represent all codepoints).
The default is UTF-32 on Linux, and UTF-16 on Windows and macOS - simply so as to match the platform convention.
and IIRC east Asian languages (which benefit the most) almost only use UTF-8
(I visited Japan for 2 weeks for the first time about a month ago; had a great time)
China, a major user of East Asian languages, uses GB2312, certainly not UTF-8.
UTF-8 assigns 3 bytes to Chinese characters so it can use the 2-byte space for European characters; that's not exactly a big selling point when you're using East Asian languages.
Back on Python 2.7.5, the UTF-7 decoder didn't do range checking, so this script [1] produced a "Unicode string" containing the codepoint U+DEADBEEF. (The maximum valid codepoint is U+10FFFF.) This string would crash regexes, corrupt databases, etc., so that allowed denial-of-service attacks against any function that let you specify an arbitrary encoding.
(This is fixed in all extant versions of Python.)
At $work, we run a heavily patched OTRS for keeping track of our tickets, and have lots of systems automatically sending cron and other status mails to it (bad, I know).
We got a bug report that the recipient email was displayed as Mojibake, something like blabla+⻧⻯⻱@ourdomain.net
After digging into the source email and the OTRS code base, I found the problem:
Some shitty MUA years ago failed to properly encode email headers, and sent 8-bit "Subject: " headers. To deal with that, OTRS had a workaround that tried to decode all (!) headers with the encoding specified in the Content-Encoding header, with a fallback to ASCII or Latin-1 if the decoding failed.
Now, a pretty old Windows system or application sent UTF-7 encoded emails to something like blabla+autoreply=no+more=stuff-here@ourdomain.net, and OTRS successfully decoded the 'To:' header as UTF-7. And since it's not ASCII compatible, it turned it into gibberish.
The "fix" was to only do the header decoding if the declared encoding is ASCII compatible.
The code still seems to be in OTRS today: https://github.com/OTRS/otrs/blob/rel-6_0_14/Kernel/System/E...
The unicode codespace is composed of 17 16-bit planes, and so has room for 1114112 codepoints. As of Unicode 11, 137439 are allocated.
And while UTF-8 has been restricted to only support 21 bits (which already allows almost 1 million more USV than actually allowed), the encoding scheme (and pre-2003 UTF-8) supports 31 bits of payload.
The PDP-10 had 36-bit words which were byte addressable -- and bytes could range from 1-63 bits in length.
So beyond the humorous language (nonets, septets) and inline PDP-10 assembly examples this was a absurdist joke intended to appeal on several levels.
That sounds like padding to a "character boundary" to me. I can't find anywhere that defines the term as being an entire block of 3/4.
Missing characters imho:
fixed width space, degree symbol, copyright/trademark symbols, the opposite direction of `, maybe a few more like pilcrow, section symbol, dagger, generic currency symbol, card suits, arrows and a few mathematical ones like +-, roughly equal, not equal, ...
Why I care about ASCII here? Because programming language source code is still written in that, and it's also those symbols that appear on standard US keyboard layout.
I think those extra symbols would have made life easier in many cases, more than some of the more obscure control characters it has (such as the useless line break vs newline we are still suffering from). They could have gotten away with just 16 instead of 32 control characters imho, even in the times when they had mechanical machines with bells :p
None of the programming languages I've used in the last ten years, save perhaps brainfuck, used ASCII. It was all unicode, either through explicit configuration atop the source code file, or e.g. UTF-8 by default.
Did you know in C++ you can have a variables named as emoji? It compiles with clang++ with -std=c++11 :)
APL is a famous non-ASCII based programming language using arrows and such, but very difficult to type on a modern computer of course. Other than esoteric languages, I don't know any modern programming languages using non-ASCII symbols in core keywords and operators.
It is however, especially today, a completely separate issue from encoding. Having more characters in ASCII won’t make a dent without having them on most keyboards, and having them on a keyboard is enough to encourage adoption without having them in the ascii code plane.
(Though they also have ASCII equivalents of those you can use instead.)
In fact due to the teletype legacy on modern terminals, we still use ASCII control codes now. eg killing an application via ctrl+c - that's sent as an ASCII control code and the receiving pesudo-TTY then interprets that as an interrupt signal. Backspace is also an ASCII control code.
The point about cartridge return (CR) and line feed (LF) is an interesting one. From a purely text markup perspective it does seem insane to have two separate characters. However from the perspective of printing data on a teletype it makes total sense: you want one control character to bring the head back to the start of the line, and a second control character to feed the paper. In fact even on modern pseudo-TTYs (eg on Linux), you can still define whether you want CHR(10) to do both a CRLF or just LF - and in fact when programs do put the TTY into raw mode (eg readline, ncurses, etc) then preferred behaviour is to disable the auto-CR on CHR(10).
The interesting thing is your point seems focused on keyboard layouts - and there's nothing stopping a keyboard from having characters that are not part of ASCII. eg UK keyboards have a pound sign (GBP) and that's not included in ASCII. Neither is the Euro currency character and that appears on some keyboards as well. I have no idea what ¬ (next to the backtick button on my keyboard) is called but that's not an ASCII character either. Navigation buttons on keyboards (left, right, home, page up, etc) are not ASCII characters. Nor are the function keys (F1 et al). Escape is though - and has proven to be quite an important control character too as it's used heavily for inlining data into TTYs via ANSI escape sequences (eg colours, cursor movement, etc).
But getting back to the GBP and Euro point: there is no reason why you couldn't configure your own keyboard to map different keys to different code points outside of the ASCII range if you wanted to.
Honestly though, some of your examples of missing symbols don't really make a whole lot of sense. How would card suits having an ASCII code help developers (or anyone for that matter)? Arrows I could see a benefit of and they do appear on some of the competing character encodings of the 70s and 80s. The opposite direction of quotation marks is a valid point to as they might help make it easier to write nested strings. However in terms of written text I'm more than happy having editors auto-fix straight quotation marks with their angled counterparts.
> Escape is though - and has proven to be quite
> an important control character too
Making the ASCII escape character the same as the keyboard escape key is one of the most annoying things in the history of terminals. It means terminal apps can't just react to your escape key directly; in theory they have to wait for the next character, to see if it's part of an escape sequence (because eg. arrow keys are escape sequences).This is why so many curses-based terminal apps don't use escape to go back a level or close a window, even though GUI apps do it all the time. Too hard to make it work predictably.
In fact in the terminal stuff I've written in Go, escape sequences end up being picked up as one array even though the TTY is set to be unbuffered just because of the time the code takes to itterate through the STDIN read loop. So didn't even need to write any fancy timing logic for the terminal stuff I've written in Go.
Sure, in some rare edge cases you might have a situation when connecting to a remote TTY over a laggy network connection will cause the escape sequence to fail because of timing issues, but that's less of an issue these days and I think the terminal would be pretty unusable by that stage anyway.
> This is why so many curses-based terminal apps don't use escape to go back a level or close a window, even though GUI apps do it all the time. Too hard to make it work predictably.
Escape is used all the time in terminal apps though. Readline uses it when you have vim mode enabled (which means Bash also supports it), Vi obviously uses it heavily. Tmux, top, cfdisk all support escape. But most importantly ncurses stuff (eg dialog) does too.
So I think what you're talking about there is more of a UI design decision rather than terminal programs not being able to do it. Thinking back, I don't really recall escape being a common navigation idiom until it became popular on the web. Your typical 90s WIMP UI (eg Windows 9x, MacOS 7+) wasn't escape heavy either (or at least it wasn't a common way to navigate as I recall). Sure you'd use it on model dialogs (eg file->open) but really very little else. So terminal software - being much older, generally speaking - never had that convention either. In fact it wasn't even that common on the web until model dialogs became all the rage so people used to use the back button heavily (and hence why GUI file managers have forwards and backwards buttons in them - but that's a whole other tangent).
To be honest though, I much prefer the terminal approach anyway. Escaping out of screens feels very crude and the terminal approach for multiple "screens" of data was to use function keys or control sequences to precisely navigate from one screen to another. Or in the case of ncurses UIs, optionally using arrow keys to highlight the option you want. Personally I find the function key kind of workflow far easier to navigate than escaping stuff all the time but I guess it's what you're used to.
In any case, terminal apps don't need to wait for the next character to determine whether the user is pressing [esc] or [-->] (with the terminal emulator transmitting CSI+n+C). You just look at the timings instead.
That's the logical not symbol, companion to ∧ (and) and ∨ (or).
It's in ISO-8859-1 (and presumably the British keyboard layout) while the others aren't, since they can be made with /\ and \/.
It's perhaps an oddity now, but these control characters were most definitely necessary when ASCII was formalized in the 1960s from teletype machines.
How would it make life any easier? The fact that they aren’t in ASCII doesn’t mean they can’t be on the keyboard. That’s an entirely separate issue. European keyboards have lots of non-ASCII characters on them.
Also as others have noted, programming languages are almost universally not ASCII. Though again I don’t know why you think that matters.
https://www.gs1.org/standards/barcodes
I would extrapolate that RFID uses similar encoding, but I have no experience there.
The fact that scanners often encode FNC1 as some arbitrary ASCII character, with GS and ^ being common choices or even reformat the data to something resembling the human readable representation (ie. "(01)..." instead of FNC1 0 1 ...) is only about the scanner interface.
But IIRC QR code does not have any special out-of-ASCII control symbols and GS1 structures are encoded with ASCII GS instead of FNC1.