Oli
Oli

Reputation: 2464

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.

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.

Upvotes: 1

Views: 85

Answers (1)

Marek R
Marek R

Reputation: 38072

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).

Upvotes: 1

Related Questions