Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

std::tolower example from website not giving expected result

I found an example of std::tolower, here: https://en.cppreference.com/w/cpp/string/byte/islower

There’s an example which, according to the website, should return false and true for this bit of code:

#include <iostream>
#include <cctype>
#include <clocale>
 
int main()
{
    unsigned char c = '\xe5'; // letter å in ISO-8859-1
 
    std::cout << "islower(\'\\xe5\', default C locale) returned "
               << std::boolalpha << (bool)std::islower(c) << '\n';
 
    std::setlocale(LC_ALL, "en_GB.iso88591");
    std::cout << "islower(\'\\xe5\', ISO-8859-1 locale) returned "
              << std::boolalpha << (bool)std::islower(c) << '\n';
 
}

But copy-pasting this bit in my own IDE gives me false and false, and so does the run this code button on the website itself.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

EDIT:
So the locale is not being set properly. Using windows 10 with latest Jetbrains Rider.

This works:

    assert(std::setlocale(LC_ALL, "en_US.UTF-8"));
    //assert(std::setlocale(LC_ALL, "en_GB.iso88591"));

    printf ("Locale is: %s\n", setlocale(LC_ALL,NULL) );

But uncommenting the other locale will throw error.

>Solution :

Ok so problem is that on Windows locale names are not same as on Linux.

On Windows iso88591 is represented by codepage 1252 so one of possible locale name is:.1252:

std::setlocale(LC_ALL, ".1252");

Not sure, but it is possible also .Windows-1252 will do the job too.

You can also try boost.locale to try unify locale names (so it could work same on all platforms). Since this is C++ you need to use std::tolower(std::locale).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading