News for hackers. Stuff that matters.
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.