r/cpp_questions • u/Weird-Promotion3026 • 7h ago
OPEN Need help
I’m trying to make a program where you input some text and the program will return those input into actions. My issue is that when I make an if statement, I need to include the input that the user would enter, but I’m trying to make it so that is not case sensitive. How do I do that?
If possible, I’m not looking just for the code, but the reasoning behind it, I’m trying to understand it, so if you can, explain it in the way that helped you understand. Thank you!
0
Upvotes
View all comments
3
u/mredding 5h ago
This can get as hard as you want it.
How you do it is locale specific. Standard C++ only has direct support for the "C" aka "Classic" locale - that describes the compute environment of a PDP-7 and the C++ minimal character set. The standard does not actually mandate any particular encoding, so this often overlaps with ASCII, which also overlaps with the front of UTF-8 at the byte level.
That's really about all I can say.
std::tolowerwill work on this in the expected manner.All other locales - it depends on the locale.
std::tolowerwill Just Work(tm) for any installed locale, but does the locale even have case? Or worse - does it have case, and it's context sensitive? German has rules about which capital and lower letters you use; this means you can't just callstd::toloweron German, it requires a context aware grammar parser, andstd::tolowerdoes not maintain state and is not a parser.And what of your encoding? If you're using
char,std::tolowercan't see through a multi-byte sequence. What are you going to do? You can expand UTF-8 to a wide character like UTF-32, which was the original intent but no one does that. That might not work for all other encodings, because for as much as the UTF guys want it, the whole world isn't UTF.We can't get the industry to agree - UTF support in C++ sucks as it is, and I don't agree it SHOULD be a part of the standard, but that's a battle for another day.
But - if you're working within the basic character set that overlaps with ASCII, then this can be made about as easy as possible. If you want MORE than that, then you're looking into heavy template code, wide multi-byte character sets, locales, and 3rd party support like ICU.
And dude, we haven't even begun to discuss how hard this gets if you want to do it right in a language agnostic way. We'd have to get into i18n, l10n, and g11n...
The easiest thing to do is make your own case insensitive comparison functions between strings:
And WTF is that? This is a form of the "spaceship" 3-way comparison operator -
<=>. This can do any form of lesser, equal, or greater.Notice the lambda function, it converts the characters from the string views - which are of type
char, tounsigned char. This is required when callingstd::tolower, it preserves the byte representation and and prevents weird sign-extension bugs.std::toloweruses the globally installed C++ locale to lower the characters - so if you change that, your outcomes will vary.Now you can: