Live data from Hacker News

A popular but wrong way to convert a string to uppercase or lowercase

devblogs.microsoft.com

111–120 of 272 posts

Re: A popular but wrong way to convert a string to uppercase or lowercase

#112
post #82

Earlier quoted context omitted.

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 worr…

But that's wrong. The upper case for ß is ẞ.

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 or lowercase

#113

He gave 4 examples of how it's done incorrectly, but zero actual examples of doing it correctly.

> Okay, so those are the problems. What’s the solution?

> If you need to perform a case mapping on a string, you can use LCMap­String­Ex with LCMAP_LOWERCASE or LCMAP_UPPERCASE, possibly with other flags like LCMAP_LINGUISTIC_CASING. If you use the International Components for Unicode (ICU) library, you can use u_strToUpper and u_strToLower.

Re: A popular but wrong way to convert a string to uppercase or lowercase

#114

He gave 4 examples of how it's done incorrectly, but zero actual examples of doing it correctly.

for (int i = 0; i

Thank you for this universal approach. I can now toggle capitalization on/off for any character, instead of just being limited to alphabetic ones!

Jokes aside, I was kinda hoping for a good answer that doesn't rely on a Windows API or an external library, but I'm not sure there is one. It's a rather complex problem when you account for more than just ASCII and the English language.

Re: A popular but wrong way to convert a string to uppercase or lowercase

#115

Earlier quoted context omitted.

I'm referring to the people who call case conversion in general "a simple text operation". Say you have an std::string and you want to make it lower case. If you assume it contains just ASCII that's a simpler operation than if you assume it contains UTF-8, but C++ doesn't provide a single function that does either of them. A person can rightly complain that the former is a basic functionality that the language should…

For ascii in C++ isn't there std::tolower / std::toupper? If you're not dealing with unsigned char types there isn't a simple case conversion function, but that's for a good reason as the article lays out.

Those functions take and return single characters. What's missing is functions that operate on strings. You can use them in combination with std::transform(), but as the article points out, even if you're just dealing with ASCII you can easily do it wrong. I've been using C++ for over 20 years and I didn't know tolower() and toupper() were non-addressable. There's really no excuse for the library not having simple case conversion functions that operate on strings in-place.

Re: A popular but wrong way to convert a string to uppercase or lowercase

#116
post #2

As always, Raymond is right. (And as usually, I could guess it's him before even clicking the link.) That said, 99% time when doing upper- or lowercase operation you're interested just in the 7-bit ASCII range of characters. For the remaining 1%, there's ICU library. Just like Raymond Chen mentioned.

> That said, 99% time when doing upper- or lowercase operation you're interested just in the 7-bit ASCII range of characters.

I think it's more the exact opposite.

The only times I'm dealing with 7-bit ASCII is for internal identifiers like variable names or API endpoints. Which is a lot of the time, but I can't ever think of when I've needed my code to change their case. It might literally be never.

On the other hand, needing to switch between upper, lower, and title case happens all the time, always with people's names and article titles and product names and whatnot. Which are never in ASCII because this isn't 1990.

Re: A popular but wrong way to convert a string to uppercase or lowercase

#118
post #59

Earlier quoted context omitted.

Ah, i see, we disagree on what is "human language". An abbreviation like HTML and it's different capitalisations to me sound a lot like a feature of human language.

Is this a serious argument? Humans don't directly use HTML to communicate with each other. It's a document markup language rendered by user agents, developed against a specification.

Markup languages and SGML in particular absolutely are designed for digital text communication by humans and to be written using plain text editors; it's kindof the entire point of avoiding binary data constructs.

And to GP, SGML/HTML actually has a facility to define uppercasing rules beyond ASCII, namely the LCNMSTRT, UCNMSTRT, LCNMCHAR, UCNMCHAR options in the SYNTAX NAMING section in the SGML declaration introduced in the "Extended Naming Rules" revision of ISO 8879 (SGML std, cf. https://sgmljs.net/docs/sgmlrefman.html). Like basically everything else on this level, these rules are still used by HTML 5 to this date, and in particular, that while elements names can contain arbitrary characters, only those in the IRV (ASCII) get case-folded for canonization.

Re: A popular but wrong way to convert a string to uppercase or lowercase

#119

Earlier quoted context omitted.

But that's wrong. The upper case for ß is ẞ.

That was only adopted in Germany like 7 years ago!

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 or lowercase

#120

Earlier quoted context omitted.

Incorrect. ẞ is still a thing.

Going by what you and the grandparent wrote, it's not just a thing, but two different things: ẞ ß It is probably time for an Esperanto advocate to show up and set us all straight.

Pri kio vi parolas? En Esperanto, unu letero egalas unu sonon.

What are you talking about? In Esperanto, one letter equals one sound.

Post reply on HN