Thread overview
toInt() to critical of string input?
Apr 01, 2005
AEon
Apr 01, 2005
Regan Heath
Apr 01, 2005
AEon
April 01, 2005
I noted that after using toInt() for a while, that there are quite a few cases it will fail. Even ANSI C is more generous, e.g.

toInt(" 4")	-> Error since the space is invalid
toInt("")	-> Error since empty string is not "promoted" to 0

Sure you can fix the above, by adding in the first case trim(" 4"), and in the second case do a "".length!=0 check before you call toInt().

But does this really need to be so "unpractical"?

I will admit this will catch a few possibly inadvertant bugs, should your code actually come accross the above cases. But just now I tripped over toInt("").

AEon
April 01, 2005
On Fri, 01 Apr 2005 07:41:30 +0200, AEon <aeon2001@lycos.de> wrote:
> I noted that after using toInt() for a while, that there are quite a few cases it will fail. Even ANSI C is more generous, e.g.

You mean "atoi".. from MSDN:

"The function stops reading the input string at the first character that it cannot recognize as part of a number."

"The string argument to atof has the following form:

[whitespace] [sign] [digits] [.digits] [ {d | D | e | E }[sign]digits]

A whitespace consists of space and/or tab characters, which are ignored; sign is either plus (+) or minus ( – ); and digits are one or more decimal digits. If no digits appear before the decimal point, at least one must appear after the decimal point. The decimal digits may be followed by an exponent, which consists of an introductory letter ( d, D, e, or E) and an optionally signed decimal integer.

atoi, _atoi64, and atol do not recognize decimal points or exponents. The string argument for these functions has the form:

[whitespace] [sign]digits

where whitespace, sign, and digits are exactly as described above for atof."

> toInt(" 4")	-> Error since the space is invalid
> toInt("")	-> Error since empty string is not "promoted" to 0

> Sure you can fix the above, by adding in the first case trim(" 4"), and in the second case do a "".length!=0 check before you call toInt().

or:

//you will need to write this function
i = toIntDef("",0);

or:

//this is effectively how you write the toIntDef function
try i = toInt("");
catch (ConvError e) i = 0;

> But does this really need to be so "unpractical"?

It's probably because I'm used to atoi, but I agree with you.

> I will admit this will catch a few possibly inadvertant bugs, should your code actually come accross the above cases. But just now I tripped over toInt("").

I can't see too much harm to skipping whitespace.

Regan
April 01, 2005
Regan Heath wrote:
> On Fri, 01 Apr 2005 07:41:30 +0200, AEon <aeon2001@lycos.de> wrote:
> 
>> I noted that after using toInt() for a while, that there are quite a few  cases it will fail. Even ANSI C is more generous, e.g.
> 
> You mean "atoi".. from MSDN:

Right, should have mentioned that.

> //you will need to write this function
> i = toIntDef("",0);

Somehow writing a function for the "low down" code cases never occurs to me. For me functions are the large ungainly blocks.

Good idea, will write that function.

AEon