4 ms·
> In gamedev there is simple rule: don't try to do any of that. I am not in gamedev, but I frequently have to develop middleware that takes in user entered dat
by cheema33 2y ago
> In gamedev there is simple rule: don't try to do any of that.
I am not in gamedev, but I frequently have to develop middleware that takes in user entered data and formats it in a way that will import into a 3rd party system without errors. And that sometimes means changing the case on strings.
In my experience as a developer, this is very very common requirement.
Luckily I am not forced to use a low level language for any of my work. In C# I can simply do this: "hello world".ToUpper();
- Smaug123 2y agoIf you're putting data into a third-party system, you might want `ToUpperInvariant`, not `ToUpper`. (Just checking that you know the difference, because most people don't!)
- pjmlp 2y agoNote that the correct way to do that in C# would be to pass an instance of CultureInfo.
- crote 2y agoThe problem is that such third-party requirements are usually wrong. Two decades ago some developer probably went "Yeah, obviously all names start with capital letters!", not realizing that there are in fact plenty of names which start with a lowercase letter. So they added an input validation test which checks for capitals, which meant everyone feeding that system had to format their data. A whole ecosystem grew around the format of the output of that system, and now you're suddenly rewriting the system and you run into weird and plain wrong capitalization requirements for no technical reason whatsoever. Alternatively, the same but start with punch cards which predate ASCII and don't distinguish between uppercase and lowercase letters. > In C# I can simply do this: "hello world".ToUpper() ... which does not work. Take a look at the German word "straße" (street), for example. Until very recently the "ß" character did not have an uppercase variant, so a ToUpper would convert it to "STRASSE". This is a lossy operation, as the reverse isn't true: the lowercase variant of "KONGRESSSTRASSE" (congress street) is not "kongreßstraße" - it's supposed to be "Kongressstraße". It can get even worse: the phrase "in Maßen" (in moderate amounts) naively has the uppercase variant "IN MASSEN" - but that means "in huge amounts"! In that case it is probably better to stick to "IN MASZEN". And then there's Turkish, where the uppercase variant of the letter "i" is of course "İ" rather than "I" - note the dot. So no, you cannot "simply" use ToUpper() / ToLower(). They might work well enough of basic ASCII for languages like English, but they have a habit of making a mess out of everything else. You're supposed to use CultureInfo.TextInfo.ToUpperCase() and explicitly specify what locale the text is in so that it can use the right converter. Which is of course essentially impossible in general-purpose text fields. In practice that means your options are a) giving up on the concept of uppercase/lowercase conversion and just passing it as-is, or b) accepting that you are inevitably going to be silently corrupting your data.
- wruza 2y agoIt’s a lossy operation, but it does work. By this logic jpeg and mpeg don’t work either. But were watching them videos daily. Yes we can simply ToUpper(). We just can’t ToUpper().ToLower(), but that’s useless cause we have the original string if we need it and fine if we don’t need it.
- account42 2y agoThe point is that what ToUpper does depends on locale AND Unicode version. This for many applications it only appears to work until it will fail spectacularly in production.
- neonsunset 2y ago> So no, you cannot "simply" use ToUpper() / ToLower(). They might work well enough of basic ASCII for languages like English, but they have a habit of making a mess out of everything else. You're supposed to use CultureInfo.TextInfo.ToUpperCase() and explicitly specify what locale the text is in so that it can use the right converter. Which is of course essentially impossible in general-purpose text fields. Have you ever read the documentation? https://learn.microsoft.com/en-us/dotnet/fundamentals/runtime-libraries/system-globalization-cultureinfo#cultureinfo-and-cultural-data https://learn.microsoft.com/en-us/dotnet/fundamentals/runtim...
- crote 2y agoYes. Now try applying it to something like this very HN comment section, which is mixing words belonging to different cultures inside a single comment - and in some cases even inside the same word. Sure, you can now do case conversion for a specific culture, but which one?
- Netch 2y ago> In C# I can simply do this: "hello world".ToUpper(); Hmm still actual: https://www.moserware.com/2008/02/does-your-code-pass-turkey-test.html https://www.moserware.com/2008/02/does-your-code-pass-turkey...
- neonsunset 2y ago> 2008 This is completely irrelevant because culture-sensitive case conversion relies on ICU/NLS.
- Netch 2y agoBut at least a programmer shall be aware to call it (whatever API is used).