473,387 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problem using locales

Can anyone explain way the transformation to upper case doesn't work
correctly in the following code if PROBLEM is defined but works correctly if
it's not defined? I'm using VC 7.1
#include <locale>
#include <string>
#include <algorithm>

#define PROBLEM

struct ToUpper

{
ToUpper(std::locale const& locale) : mLocale(locale) {}

char operator() (char c) const { return std::toupper(c,mLocale); }

std::locale const& mLocale;
};
#ifdef PROBLEM
std::locale my_locale( "Swedish" );
#endif

int main ()
{
#ifndef PROBLEM
std::locale my_locale( "Swedish" );
#endif

std::string s("abc едц");

std::transform( s.begin(), s.end(), s.begin(), ToUpper(my_locale) );

return 0;
}
Feb 11 '06 #1
4 2538
* B.D.:
Can anyone explain way the transformation to upper case doesn't work
correctly in the following code if PROBLEM is defined but works correctly if
it's not defined? I'm using VC 7.1

#include <locale>
#include <string>
#include <algorithm>
#define PROBLEM

struct ToUpper
{
ToUpper(std::locale const& locale) : mLocale(locale) {}
char operator() (char c) const { return std::toupper(c,mLocale); }
std::locale const& mLocale;
};

#ifdef PROBLEM
std::locale my_locale( "Swedish" );
#endif

int main ()
{
#ifndef PROBLEM
std::locale my_locale( "Swedish" );
#endif
std::string s("abc едц");
std::transform( s.begin(), s.end(), s.begin(), ToUpper(my_locale) );
return 0;
}


I could reproduce your problem with Visual C++ 7.1, where if PROBLEM is
defined (global instance of 'my_local') the local name is reported as
"Swedish_Sweden.1252", but the transformed string as "ABC едц".

The problem could not be reproduced using g++ 3.4.4, since that compiler
doesn't support the "Swedish" locale, at least not without doing
something special.

Note: the standard only mandates support for the "C" and "" locales, and
is self-contradictory on what other locale names can be (only
standard C locale names, whatever they are, or implementation defined
names).

So really the code should have a try-catch.

The problem with Visual C++ 7.1 likely to be a library implementation
bug due to order-of-initialization of globals.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 11 '06 #2
Thanks the responses and suggestions.

I apologize if I was a bit vague in my initial posting. The program transforms the characters in the
string s to upper case. The last three strange characters in the string are national characters
that are part of the Swedish alphabet. What I don't understand is way the program behaves
differently if PROBLEM is defined. What I real would like to know if this is a:

a) programmer issue (more education is needed, perhaps by reading a
good book that cover this topic)

b) compiler issue

c) C++ language issue

Another thing that would be interesting to know is if this behavior is more general than it first
appears? Should I expected this type of behavior when I instantiate other classes as well? If the
problem is related to how or global objects are initializing, as suggested by Jim Langston
(alt.comp.langlearn.c-c++) and Alf P. Steinbach (comp.lang.c++) then perhaps the problem is a bit
more general than I first expected?

"B.D." <bd@algonet.se> wrote in message news:ki*******************@newsb.telia.net...
Can anyone explain way the transformation to upper case doesn't work correctly in the following
code if PROBLEM is defined but works correctly if it's not defined? I'm using VC 7.1
#include <locale>
#include <string>
#include <algorithm>

#define PROBLEM

struct ToUpper

{
ToUpper(std::locale const& locale) : mLocale(locale) {}

char operator() (char c) const { return std::toupper(c,mLocale); }

std::locale const& mLocale;
};
#ifdef PROBLEM
std::locale my_locale( "Swedish" );
#endif

int main ()
{
#ifndef PROBLEM
std::locale my_locale( "Swedish" );
#endif

std::string s("abc едц");

std::transform( s.begin(), s.end(), s.begin(), ToUpper(my_locale) );

return 0;
}


Feb 12 '06 #3
TB
B.D. sade:
Thanks the responses and suggestions.

I apologize if I was a bit vague in my initial posting. The program
transforms the characters in the string s to upper case. The last three
strange characters in the string are national characters that are part
of the Swedish alphabet. What I don't understand is way the program
behaves differently if PROBLEM is defined. What I real would like to
know if this is a:

a) programmer issue (more education is needed, perhaps by reading a
good book that cover this topic)

b) compiler issue

c) C++ language issue

Another thing that would be interesting to know is if this behavior is
more general than it first appears? Should I expected this type of
behavior when I instantiate other classes as well? If the problem is
related to how or global objects are initializing, as suggested by Jim
Langston (alt.comp.langlearn.c-c++) and Alf P. Steinbach (comp.lang.c++)
then perhaps the problem is a bit more general than I first expected?


The problem is most likely with VC++. There are no problems with the code's
expected behaviour using Borland's C++ Compiler with RW or STLport.

--
TB @ SWEDEN
Feb 12 '06 #4
Ok, many thanks to you for helping clarify and verify this.

- B.D
"TB" <TB@SWEDEN> wrote in message news:43**********************@taz.nntpserver.com.. .
B.D. sade:
Thanks the responses and suggestions.

I apologize if I was a bit vague in my initial posting. The program
transforms the characters in the string s to upper case. The last three
strange characters in the string are national characters that are part
of the Swedish alphabet. What I don't understand is way the program
behaves differently if PROBLEM is defined. What I real would like to
know if this is a:

a) programmer issue (more education is needed, perhaps by reading a
good book that cover this topic)

b) compiler issue

c) C++ language issue

Another thing that would be interesting to know is if this behavior is
more general than it first appears? Should I expected this type of
behavior when I instantiate other classes as well? If the problem is
related to how or global objects are initializing, as suggested by Jim
Langston (alt.comp.langlearn.c-c++) and Alf P. Steinbach (comp.lang.c++)
then perhaps the problem is a bit more general than I first expected?


The problem is most likely with VC++. There are no problems with the code's
expected behaviour using Borland's C++ Compiler with RW or STLport.

--
TB @ SWEDEN

Feb 12 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: garykpdx | last post by:
Is there some sort of tutorial on locales or the locale module? I can't seem to find a list showing all possible locales. I made 'en' work alright, but when I tried 'de' or 'de_DE' or 'es_ES',...
0
by: David List | last post by:
I seem to have a problem installing facets into locales explicitly. It is not clear to me whether the problem is my compiler, my environment or my code. I am trying out some code from...
6
by: Gustav Lead | last post by:
Hi all, I have trouble reading back floating point data from a file. It appears as if the tokens for positive and negative infinity (1.#INF and -1.#INF) that my implementation writes get...
1
by: John Smith | last post by:
Hi, I hope somebody can help here. I have an app. that uses both MS Access 2000 and SQL Server databases running under XP and 2003 server. The problem I have encountered during testing was...
2
by: corley | last post by:
Hello world, I am having a problem with the decimal symbol. The result of writing a floating-point-number to an xml document (using libxml2 from xmlsoft.org) is <number>10,1234</number> ...
0
by: | last post by:
Hi all, running the following code as an console application: #include <locale> #include <iostream> #include <sstream> #include <tchar.h> using namespace std;
8
by: Rik | last post by:
Hi all, is there a in PHP to get the available locales without shellacces (i.e. locale -a)? Grtz, -- Rik Wasmus
2
by: cris | last post by:
Hi, I got trouble when I try to change the locale used in a program foo.cc // ----------------- begin #include <iostream> #include <locale> #include <exception> using namespace std; int...
4
by: Gilles Ganault | last post by:
Hello What does it take to support locales? The following test code found on the PHP site doesn't work as planned: ======= setlocale(LC_TIME, "C"); echo strftime("%A"); setlocale(LC_TIME,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.