[Qt-interest] [BUG?]QLocale::to* functions and trailing whitespace

Mihail Naydenov mlists at ymail.com
Sun Mar 29 12:07:44 CEST 2009


Thank You for your response.
You are right, my explanation (and understanding!) of the problem is totally wrong.
There is however a problem however, and this time I will try to present it in code;

qDebug()<<"--"<<input<<"--";

bool ok = false; 
qlonglong val = locale().toLongLong(input, &ok);

qDebug()<<"locale:"<<ok;

if(!ok)
{
QLocale lc(QLocale::C);
val = lc.toLongLong(input, &ok);

qDebug()<<"lc:"<<ok;
}

Result:
-- "11" --   <- no spaces
locale: true    <- conv. success, as expected

-- " 11" --   <- leading space

locale: true   <- ignored, as expected

-- "1 1" --   <- in-between space
locale: false   <- conv. fails, as expected
lc: false   <- conv. fails, as expected
-- "11 " --  <- trailng space
locale: false   <- conv. fails, NOT expected
lc: true   <- conv. success, as expected
The problem is not that 'C' locale "leaves trailing whitespace", the problem is local() does not ignore them, as the docs say!
Also, I have tested the return value, it is indeed 0 when ok==false, so the problem is not in error reporting.
And, as I said the result is the same for ALL to*() functions, not just the above used toLongLong().

MihailNaydenov

 



----- Original Message ----
From: Denis Dzyubenko <denis.dzyubenko at trolltech.com>
To: qt qt <qt-interest at trolltech.com>
Sent: Friday, March 27, 2009 5:46:58 PM
Subject: Re: [Qt-interest] [BUG?]QLocale::to* functions and trailing whitespace

Hi Mihail,

Mihail Naydenov wrote:
> The documentation states that all QLocale::to* functions (these are QLocale::toShort, QLocale::toLongLong, etc)
> "ignores leading and trailing whitespace."
> 
> It turned out however that 'C' locale ( QLocale cl(QLocale::C); ) does leave trailing withespace!
> It handles all other - leading, and in-between spaces, but leaves trialing. 
> Using the default local on my machine works fine and leaves no witespace.

What do you mean by "leaving a whitespace"? The listed functions are not 
supposed to modify the provided string.

QLocale locale(QLocale::C);
int value = locale.toInt("   1234   "); // value equals 1234

> Also using QString::to* functions acts as local 'C' leaving only tailing.
> 
> I have two questions:
> 
> Is this intended or a bug?
> 
> What is The Right Way to convert strings to numbers - using string::to* or with local()->to*?
> Looking in the code, String calls QLocale in the end, so is there a difference? 

Both ways are correct. If you want to convert data using the current 
system locale, you can just use conversion functions from QString. 
However if you want to convert double to a string, save it somewhere and 
do the string to double conversion later on another machine on another 
locale, then you might want to explicitely say which locale should be 
used for the conversion.

-- 
Denis Dzyubenko
Software Engineer
Nokia, Qt Software
_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest



      



More information about the Qt-interest-old mailing list