This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: Localization: example from document page doesn't work for latin characters ?
- From: Jan Pfeifer <pfjan at yahoo dot com dot br>
- To: Paolo Carlini <pcarlini at suse dot de>
- Cc: libstdc++ at gcc dot gnu dot org
- Date: Sun, 11 Dec 2005 12:38:40 -0200
- Subject: Re: Localization: example from document page doesn't work for latin characters ?
- References: <439C24BD.5060405@yahoo.com.br> <439C3393.7030903@suse.de>
Thanks for the answer :). I tried the C version as well, and it displays
the same problem. I didn't know that the stdc++ used the libc
implementation for these localization issues.
I'll ask about this in gcc-help.
- jan
ps.: attached is the edited file with the C version. Btw, I'm compiling
with gcc (GCC) 4.0.2 20050808 (prerelease).
Paolo Carlini wrote:
>Jan Pfeifer wrote:
>
>
>
>>Am I missing something, or is there something missing in the library ?
>>
>>
>>
>>
>Well, I don't have ready at hand the locale at issue, sorry, but in such
>cases, for debugging purposes I would recommend preparing first a simple
>pure C equivalent (that is, using just setlocale & toupper / tolower)
>and see what happens. Consider that for such facilities the C++ library
>is mostly calling into the underlying libc facilities and skipping some
>steps often makes the issue much simpler to understand.
>
>Paolo.
>
>
#include <iostream>
#include <iterator> // for back_inserter
#include <locale>
#include <string>
#include <algorithm>
#include <cctype> // old <ctype.h>
using namespace std;
struct MyToUpper
{
MyToUpper(std::locale const& l) : loc(l) {;}
char operator() (char c) const { return std::toupper(c,loc); }
private:
std::locale const& loc;
};
struct MyToLower
{
MyToLower(std::locale const& l) : loc(l) {;}
char operator() (char c) const { return std::tolower(c,loc); }
private:
std::locale const& loc;
};
int main(int argc, char** argv)
{
// locale loc( "" );
// locale loc( "en_US.UTF-8" );
locale loc( "pt_BR.UTF-8" );
MyToUpper up( loc );
MyToLower down( loc );
cout << "C++ version: " << endl;
const char *reference = "�rfão";
string normal = reference;
cout << normal << endl;
transform( normal.begin(), normal.end(), normal.begin(), up );
cout << normal << endl;
transform( normal.begin(), normal.end(), normal.begin(), down );
cout << normal << endl;
// C version
//setlocale(LC_ALL, "");
//setlocale(LC_ALL, "pt_BR.UTF-8");
setlocale(LC_ALL, "en_US.UTF-8");
cout << endl << "C version: " << endl;
char buffer[256];
strcpy( buffer, reference );
char *buffer_end = buffer + strlen(buffer);
cout << buffer << endl;
transform( buffer, buffer_end, buffer, ::toupper );
cout << buffer << endl;
transform( buffer, buffer_end, buffer, ::tolower );
cout << buffer << endl;
return 0;
return 0;
}