News for hackers. Stuff that matters.
Re:A popular but wrong way to convert a string to uppercase ... by bayindirh on Tuesday October 08, 2024 @05:21AM (#41775308)
I don't think it's a C++ problem. You just can't transform anything developed in "ancient" times to unicode aware in a single swoop.

On the other hand, libicu is 37MB by itself, so it's not something someone can write in a weekend and ship.

Any tool which is old enough will have a thousand ways to do something. This is the inevitability of software and programming languages. In the domain of C++, which has a size mammoth now, everyone expects this huge pony to learn new tricks, but everybody has a different idea of the "new tricks", so more features are added on top of its already impressive and very long list of features and capabilities.

You want libICU built-in? There must be other folks who want that too. So you may need to find them and work with them to make your dream a reality.

So, C++ is doing fine. It's not that they omitted Unicode during the design phase. Unicode has arrived later, and it has to be integrated by other means. This is what libraries for.

Re:A popular but wrong way to convert a string to uppercase ... by pornel on Tuesday October 08, 2024 @05:33AM (#41775367)
Being developed in, and having to stay compatible with, ancient times is a real problem of C++.

The now-invalid assumptions couldn't have been avoided 50 years ago. Fixing them now in C++ is difficult or impossible, but still, the end result is a ton of brokenness baked into C++.

Languages developed in the 21st century typically have some at least half-decent Unicode support built-in. Unicode is big and complex, but there's a lot that a language can do to at least not silently destroy the encoding.

Re:A popular but wrong way to convert a string to uppercase ... by cm2187 on Tuesday October 08, 2024 @06:18AM (#41775666)
That explains why there are two functions, one for ascii and one for unicode. That doesn't explain why the unicode functions are hard to use (per the article).
Re:A popular but wrong way to convert a string to uppercase ... by BoringTimesGang on Tuesday October 08, 2024 @06:55AM (#41775925)
Because human language is hard to boil down to a simple computing model and the problem is underdefined, based on naive assumptions.

Or perhaps I should say naïve.

Re:A popular but wrong way to convert a string to uppercase ... by cm2187 on Tuesday October 08, 2024 @02:11PM (#41780129)
Well pretty much every other more recent language solved that problem.
Re:A popular but wrong way to convert a string to uppercase ... by kccqzy on Tuesday October 08, 2024 @02:24PM (#41780263)
Almost no programming language, perhaps other than Swift, solved that problem. Just use the article's examples as test cases. It's just as wrong as the C++ version in the article, except it's wrong with nicer syntax.
Re:A popular but wrong way to convert a string to uppercase ... by zahlman on Tuesday October 08, 2024 @02:46PM (#41780453)
Python's strings have uppercase, lowercase and case-folding methods that don't choke on this. They don't use UTF-16 internally (they can use UCS-2 for strings whose code points will fit in that range; while a string might store code points from the surrogate-pair range, they're never interpreted as surrogate pairs, but instead as an error encoding so that e.g. invalid UTF-8 can be round-tripped) so they're never worried about surrogate pairs, and it knows a few things about localized text casing:

    >>> 'ß'.upper()
    'SS'
    >>> 'ß'.lower()
    'ß'
    >>> 'ß'.casefold()
    'ss'
There are a lot of really complicated tasks for Unicode strings. String casing isn't really one of them.

(No, Python can't turn 'SS' back into 'ß'. But doing that requires metadata about language that a string simply doesn't represent.)

Re:A popular but wrong way to convert a string to uppercase ... by tedunangst on Tuesday October 08, 2024 @03:18PM (#41780786)
But that's wrong. The upper case for ß is ẞ.
Re:A popular but wrong way to convert a string to uppercase ... by IncreasePosts on Tuesday October 08, 2024 @04:15PM (#41781403)
That was only adopted in Germany like 7 years ago!
Re:A popular but wrong way to convert a string to uppercase ... by kccqzy on Tuesday October 08, 2024 @05:10PM (#41781860)
Well languages and conventions change. The € sign was added not that long ago and it was somewhat painful. The Chinese language uses a single character to refer to chemical elements so when IUPAC names new elements they will invent new characters. Etc.
Re:A popular but wrong way to convert a string to uppercase ... by extraduder_ire on Tuesday October 08, 2024 @09:10PM (#41783486)
Does unicode have space set aside for those new symbols to slot into? I know it's very rare, but it could get messy.
Re:A popular but wrong way to convert a string to uppercase ... by account42 on Wednesday October 09, 2024 @09:16AM (#41787646)
Unicode is already messy. Chinese characters especially so due to han unificiation.
Re:A popular but wrong way to convert a string to uppercase ... by cm2187 on Tuesday October 08, 2024 @04:27PM (#41781531)
C#'s "ToUpper" takes an optional CultureInfo argument if you want to play around with how to treat different languages. Again, solved problem decades ago.
Re:A popular but wrong way to convert a string to uppercase ... by account42 on Wednesday October 09, 2024 @09:14AM (#41787625)
This is not a locale issue, it's a Unicode version issue. Which hightlights another problem with adding this to the base standard library.
Re:A popular but wrong way to convert a string to uppercase ... by Towaway69 on Wednesday October 09, 2024 @02:29AM (#41785090)
Isn't uppercase for ß just ß - i.e. it's its own uppercase character?
Re:A popular but wrong way to convert a string to uppercase ... by bratwurst3000 on Wednesday October 09, 2024 @08:59AM (#41787451)
there shouldn’t be an uppercase version of ß because there is no word in the german language that uses it as the first letter. the german language didnt think of allcaps. please correct me if I am wrong. If written in uppercase it should be converted to SZ or the new uppercase ß…. which my iphone doesn’t have… and converting anything to uppercase SS isn’t something germany wants …
Re:A popular but wrong way to convert a string to uppercase ... by account42 on Wednesday October 09, 2024 @09:22AM (#41787713)
> there shouldn’t be an uppercase version of ß because there is no word in the german language that uses it as the first letter. the german language didnt think of allcaps.

Allcaps (and smallcaps) has always existed in signage everywhere. Before the computing age, letters where just arbitrary metal stamps -- and just whatever you could draw before that. Historically, language was not as standardized as it is today.

Re:A popular but wrong way to convert a string to uppercase ... by Towaway69 on Wednesday October 09, 2024 @05:47PM (#41793036)
I don’t think that Germany wants a capital ß or the German language requires one rather technology needs one to dot the eyes and cross the tees.
Re:A popular but wrong way to convert a string to uppercase ... by account42 on Wednesday October 09, 2024 @09:18AM (#41787669)
Not generally no, but some applications used it that way because of ambiguity of upppercasing ß to SS - which is why ẞ was added.
Re:A popular but wrong way to convert a string to uppercase ... by Towaway69 on Wednesday October 09, 2024 @05:43PM (#41793004)
On the other hand, the German language has existed for several hundred years without having a capital ß but now it needs one?

True capitalisation has always existed but even that didn’t seem to have required a capital ß - why now?

Re:A popular but wrong way to convert a string to uppercase ... by kccqzy on Tuesday October 08, 2024 @03:04PM (#41780647)
Still breaks on, for example, Turkish i vs İ. It's impossible to do correctly without language information.

> (No, Python can't turn 'SS' back into 'ß'. But doing that requires metadata about language that a string simply doesn't represent.)

Yes that's my point. Because in typical languages strings don't store language metadata, this is impossible to do correctly in general.

Re:A popular but wrong way to convert a string to uppercase ... by zahlman on Tuesday October 08, 2024 @03:08PM (#41780685)
I'm not seeing anything in the Swift documentation about strings carrying language metadata, either, though?
Re:A popular but wrong way to convert a string to uppercase ... by kccqzy on Tuesday October 08, 2024 @03:20PM (#41780805)
This lowercase function takes a locale argument https://developer.apple.com/documentation/foundation/nsstrin...

It looks like an old NSString method that's available in both Obj-C and Swift.

The casefold function is even older than that. https://developer.apple.com/documentation/foundation/nsstrin... Its documentation specifically includes a discussion of the Turkish İ/I issue.

Re:A popular but wrong way to convert a string to uppercase ... by crote on Wednesday October 09, 2024 @04:07AM (#41785596)
But that's wrong. The uppercase for "in Maßen" ("in moderate amounts") is not "IN MASSEN" ("in Massen", meaning "in massive amounts").
Re:A popular but wrong way to convert a string to uppercase ... by tialaramex on Tuesday October 08, 2024 @06:00PM (#41782226)
Rust will cheerfully:

    assert_eq!("ὀδυσσεύς", "ὈΔΥΣΣΕΎΣ".to_lowercase());
[Notice that this is in fact entirely impossible with the naive strategy since Greek cares about position of symbols]

Some of the latter examples aren't cases where a programming language or library should just "do the right thing" but cases of ambiguity where you need locale information to decide what's appropriate, which isn't "just as wrong as the C++ version" it's a whole other problem. It isn't wrong to capitalise A-acute as a capital A-acute, it's just not always appropriate depending on the locale.

Re:A popular but wrong way to convert a string to uppercase ... by account42 on Wednesday October 09, 2024 @09:27AM (#41787748)
Is this

    assert_eq!("\u1F41δυσσεύς", "ὈΔΥΣΣΕΎΣ".to_lowercase());
or

    assert_eq!("\u03BF\u0314δυσσεύς", "ὈΔΥΣΣΕΎΣ".to_lowercase());
For display it doesn't matter but most other applications really want some kind of normalizatin which does much much more so having a convenient to_lowercase() doesn't buy you as much as you think and can be actively misleading.
Re:A popular but wrong way to convert a string to uppercase ... by MBCook on Tuesday October 08, 2024 @06:36PM (#41782489)
So what?

That doesn’t prevent adding a new function that converts an entire string to upper or lowercase in a Unicode aware way.

What would be wrong with adding new correct functions to the standard library to make this easy? There are already namespaces in C++ so you don’t even have to worry about collisions.

That’s the problem I see. It’s fine if you have a history of stuff that’s not that great in hindsight. But what’s wrong with having a better standard library going forward?

It’s not like this is an esoteric thing.

Re:A popular but wrong way to convert a string to uppercase ... by wakawaka28 on Tuesday October 08, 2024 @08:24PM (#41783259)
The reason that wasn't done is because Unicode is not really in older C++ standards. I think it may have been added to C++23 but I am not familiar with that. There are many partial solutions in older C++ but if you want to do it well then you need to get a library for it from somewhere, or else (possibly) wait for a new standard.

Unicode and character encodings are pretty esoteric. So are fonts. The stuff is technically everywhere and fundamental, but there are many encodings, technical details, etc. And most programmers only care about one language, or else only use UTF-8 with the most basic chars (the ones that agree with ASCII). That isn't terrible. You only need what you actually need. Most programs don't strictly have to be built for multiple random languages, and there is kind of a standard methodology to learn before you can do that.

Re:A popular but wrong way to convert a string to uppercase ... by account42 on Wednesday October 09, 2024 @09:11AM (#41787598)
No, strong backwards compatiblity a real strength of C++. In fact, it's probably it's main strength these days.
Re:A popular but wrong way to convert a string to uppercase ... by relaxing on Tuesday October 08, 2024 @05:44AM (#41775424)
It’s been 30 years. Unicode predates C++98. Java saw the writing on the wall. There’s no excuse.
Re:A popular but wrong way to convert a string to uppercase ... by bayindirh on Tuesday October 08, 2024 @05:52AM (#41775464)
> There’s no excuse.

I politely disagree. None of the programming languages which started integrating Unicode was targeting from bare metal to GUI, incl. embedded and OS development at the same time.

C++ has a great target area when compared to other programming languages. There are widely used libraries which compile correctly on PDP-11s, even if they are updated constantly.

You can't just say "I'll be just making everything Unicode aware, backwards compatibility be damned, eh".

Re:A popular but wrong way to convert a string to uppercase ... by blenderob on Tuesday October 08, 2024 @05:56AM (#41775488)
But we don't have to make everything Unicode aware. Backward compatibility is indeed very important in C++. Like you rightly said, it still has to work for PDP-11 without breaking anything.

But the C++ overlords could always add a new type that is Unicode-aware. Converting one Unicode string to another is a purely in-memory, in-CPU operation. It does not need any I/O and it does not need any interaction with peripherals. So one can dream that such a type along with its conversion routines could be added to an updated standard library without breaking existing code that compiles correctly on PDP-11s.

Re:A popular but wrong way to convert a string to uppercase ... by bayindirh on Tuesday October 08, 2024 @06:09AM (#41775577)
> Converting one Unicode string to another is a purely in-memory, in-CPU operation.

...but it's a complex operation. This is what libICU is mostly for. You can't just look-up a single table and convert a string to another like you work on ASCII table or any other simple encoding.

Germans have their ß to S (or capital ß depending on the year), Turkish has ı/I and i/İ pairs, and tons of other languages have other rules.

Esp, this I/ı and İ/i pairs break tons of applications in very unexpected ways. I don't remember how many bugs I reported, and how many workarounds I have implemented in my systems.

Adding a type is nice, but the surrounding machinery is so big, it brings tons of work with itself. Unicode is such a complicated system, that I read that even you need two UTF-16 characters (4 bytes in total) to encode a single character. This is insane (as in complexity, I guess they have their reasons).

Re:A popular but wrong way to convert a string to uppercase ... by blenderob on Tuesday October 08, 2024 @06:17AM (#41775645)
Thanks for the reply! Really appreciate the time you have taken to write down a thoughtful reply.
Re:A popular but wrong way to convert a string to uppercase ... by bayindirh on Tuesday October 08, 2024 @06:34AM (#41775790)
No problems! If you want a slightly longer write-up, here's a classic I constantly share with people:

https://blog.codinghorror.com/whats-wrong-with-turkey/

Re:A popular but wrong way to convert a string to uppercase ... by SAI_Peregrinus on Tuesday October 08, 2024 @09:56AM (#41777433)
> Unicode is such a complicated system, that I read that even you need two UTF-16 characters (4 bytes in total) to encode a single character. This is insane (as in complexity, I guess they have their reasons).

Because there are more than 65,535 characters. That's just writing systems, not Unicode's fault. Most of the unnecessary complexity of Unicode is legacy compatibility: UTF-16 & UTF-32 are bad ideas that increase complexity, but they predate UTF-8 which actually works decently well so they get kept around for backwards compatibility. Likewise with the need for multiple normalization forms.

Re:A popular but wrong way to convert a string to uppercase ... by bayindirh on Tuesday October 08, 2024 @10:06AM (#41777526)
I mean, I already know some Unicode internals and linguistics (since I developed a language-specific compression algorithm back in the day), but I have never seen a single character requiring four bytes (and I know Emoji chaining for skin color, etc.).

So, seeing this just moved the complexity of Unicode one notch up in my head, and I respect the guys who designed and made it work. It was not whining or complaining of any sort. :)

Re:A popular but wrong way to convert a string to uppercase ... by fluoridation on Tuesday October 08, 2024 @03:06PM (#41780662)
Cuneiform codepoints are 17 bits long. If you're using UTF-16 you'll need two code units to represent a character.
Re:A popular but wrong way to convert a string to uppercase ... by gpderetta on Wednesday October 09, 2024 @08:25AM (#41787122)
you also need two UTF16 code units for plain emojis.
Re:A popular but wrong way to convert a string to uppercase ... by TorKlingberg on Wednesday October 09, 2024 @09:53AM (#41788005)
Lots of emoji are outside the Basic Multilingual Plane and need 4 bytes in UTf-8 and UTF-16. That's without going into skin color and other modifiers and combinations.
Re:A popular but wrong way to convert a string to uppercase ... by numpad0 on Tuesday October 08, 2024 @08:07PM (#41783144)
It's because Unicode don't allow for language switching.

It takes up to eight bytes per character in Unicode if you want to support both Chinese and Japanese in a single font using IVS(and I don't think there's any font that actually supports this).

AFAICS(As far as I can search), Simplified(PRC) and Traditional(Taiwan) Chinese encoding are respectively called GB2312 and Big5, and they're both two byte encodings with good practical coverage. Same applies for Japanese Shift_JIS. If e.g. :flag_cc: were allowed to be used as start-of-language marker, one could theoretically cut that back down to two bytes per character without losing much and actually improving language supports.

Re:A popular but wrong way to convert a string to uppercase ... by account42 on Wednesday October 09, 2024 @09:55AM (#41788031)
The number of characters is not the problem, the mess due to legacy compatibility is - case folding and normaltization could be much simpler if the codepoints were laid out with that in mind. Also the fact the Unicode can't make up its mind if it wants to encode glyphs (turkish I and i, han unification) or semantic characters (e.g. cyrillic vs. latin letters) or just "ideas" (emojis).
Re:A popular but wrong way to convert a string to uppercase ... by account42 on Wednesday October 09, 2024 @09:45AM (#41787916)
> Germans have their ß to S (or capital ß depending on the year)

FYI, it's never S. If there is no better option then SS and ss are the proper capital and lowercase substitutions.

Re:A popular but wrong way to convert a string to uppercase ... by gpderetta on Tuesday October 08, 2024 @03:53PM (#41781165)
Java ended up picking UCS-2 and getting screwed.
Re:A popular but wrong way to convert a string to uppercase ... by throwaway2037 on Wednesday October 09, 2024 @05:53AM (#41786098)
Pretty much all Unicode early adopters went for 16-bit chars. Qt and Win32 API are another pair.
Re:A popular but wrong way to convert a string to uppercase ... by gpderetta on Wednesday October 09, 2024 @08:21AM (#41787089)
Indeed, ICU as well, and then they all moved to UTF-16, which, again. in the long term lost to UTF-8. My point is that committing on a specific Unicode design 30 years ago was not, in retrospect, necessarily a good idea.

By not committing to UCS-2 early C++ left the road open to UTF-8. I'll concede that UTF8 has risen as the clear winner for more than a decade and C++ is well past the point that it should have at least basic builtin support. The problem is that there is at least one important C++ platform that only very recently added full support for the encoding in their native API.

Re:A popular but wrong way to convert a string to uppercase ... by account42 on Wednesday October 09, 2024 @10:13AM (#41788200)
Qt really has no excuse for still using 16-bit characters since unlike the other two they have had multiple ABI breaks since then.
Re:A popular but wrong way to convert a string to uppercase ... by throwaway2037 on Thursday October 10, 2024 @03:37AM (#41796512)
"no excuse" -- I would respectfully disagree here. There are lots of very smart people who have worked on Qt. Really, some insanely good C++ programmers have worked on that project. I have no doubt that they have discussed changing class QString to use UTF-8 internally. To be clear, probably QChar would also need to change, or a new class (QChar8?) would be needed, in parallel to QChar. I guess they concluded the API breakage would be too severe. I assume Java and Win32/DotNet decided the same. Finally, you can Google for old mailing list discussions about QString using UTF-16. Many before have asked "can we just change to UTF-8?".
Re:A popular but wrong way to convert a string to uppercase ... by account42 on Thursday October 10, 2024 @03:48AM (#41796566)
Ah yes, appeal to authority. No better way to admit that you are talking out of your arse.
Re:A popular but wrong way to convert a string to uppercase ... by wakawaka28 on Tuesday October 08, 2024 @08:28PM (#41783278)
Java was built from scratch as a heavy language with a whole portability layer that C++ does not have. Also, libraries have been around to do this stuff in C++ but maybe some people saw it better to not require C++ to support Unicode, presumably.
Re:A popular but wrong way to convert a string to uppercase ... by account42 on Wednesday October 09, 2024 @09:43AM (#41787892)
Java has 16-bit character types. It is in no way better at modern Unicode than C++ while being needlessly less efficient for mostly-ASCII text like XML-like markup.
Re:A popular but wrong way to convert a string to uppercase ... by Netch on Wednesday October 09, 2024 @11:24AM (#41788880)
> There’s no excuse.

Until mid-2000s there was no certainty Unicode will eventually defeat competitors. In real it havenʼt fully yet - GB2312 and Tron are still locally prevailing, and IBM still jogs with EBCDIC. But at its early times nobody was reasonably sure, and Java attempt could have failed as well. (More so Java approach for UCS-2 was wrong - already commented nearby.)

Re:A popular but wrong way to convert a string to uppercase ... by nitwit005 on Thursday October 10, 2024 @02:39AM (#41796192)
Java embraced Unicode, and ended up with a mess as Unicode changed underneath it.

You can actually end up in a cleaner state in C++, as there is no obligation to use the standard library string classes, but it's pretty much required in Java.

Re:A popular but wrong way to convert a string to uppercase ... by akira2501 on Tuesday October 08, 2024 @02:34PM (#41780355)
> libicu is 37MB by itself, so it's not something someone can write in a weekend and ship.

Isn't that mostly just from tables derived from the Unicode standard?

Re:A popular but wrong way to convert a string to uppercase ... by zahlman on Tuesday October 08, 2024 @02:40PM (#41780414)
>You just can't transform anything developed in "ancient" times to unicode aware in a single swoop.

Even for Python it took well over a decade, and people still complain about the fact that they don't get to treat byte-sequences transparently as text any more - as if they want to wrestle with the `basestring` supertype, getting `UnicodeDecodeError` from an encoding operation or vice-versa, trying to guess the encoding of someone else's data instead of expecting it to be decoded on the other side....

But in C++ (and in C), you have the additional problem that the 8-bit integer type was named for the concept of a character of text, even though it clearly cannot actually represent any such thing. (Not to mention the whole bit about `char` being a separate type from both `signed char` and `unsigned char`, without defined signedness.)

Re:A popular but wrong way to convert a string to uppercase ... by ectospheno on Tuesday October 08, 2024 @02:45PM (#41780451)
> Any tool which is old enough will have a thousand ways to do something.

Only because of the strange desire of programmers to never stop. Not every program is a never ending story. Most are short stories their authors bludgeon into a novel.

Programming languages bloat into stupidity for the same reason. Nothing is ever removed. Programmers need editors.

Re:A popular but wrong way to convert a string to uppercase ... by fluoridation on Tuesday October 08, 2024 @03:01PM (#41780611)
So how do you design a language that accommodates both the people who need a codebase to be stable for decades and the people who want the bleeding edge all the time, backwards compatibility be damned?
Re:A popular but wrong way to convert a string to uppercase ... by the_gorilla on Tuesday October 08, 2024 @03:03PM (#41780639)
You don't. Any language that tries to do both turns into an unusable abomination like C++. Good languages are stable and the bleeding edge is just the "new thing" and not necessarily better than the old thing.
Re:A popular but wrong way to convert a string to uppercase ... by fluoridation on Tuesday October 08, 2024 @03:07PM (#41780673)
C++ doesn't try to do that. It aims to remain as backwards compatible as possible, which is what the GP is complaining about.