SRL Archive - Bugs

StrToInt('hi') not throwing error?


Gu3st...

2011-04-01 21:46:51

begin
WriteLn(StrToInt('hi'));
end.

Compiled succesfully in 15 ms.
-1
Successfully executed.

AFAIK, "hi" is not a number, but Simba says it is the same as "-1"… Why doesn't it result in an error???

Sex

2011-04-01 21:49:28

No reason….if there is anything in the string that is not a number it returns -1…

Well I don't know. Whatever :p. I think it doesn't matter anyways.

Gu3st...

2011-04-01 21:53:39

It should not return a number when the input is not a number (of course)

-1 doesn't mean it failed, -1 is just a number.

StrToInt('anystring'); should crash simba

Sex

2011-04-01 21:57:10

-1 sometimes means false. As does 0. This is a different situation as you are expecting a number, any number (which could, of course, be -1 itself). I see what you mean. But I don't think I'd want it to crash. You can create your own handler for this if you really want to.

Gu3st...

2011-04-01 21:58:12

If you don't want it to crash, use StrToIntDef().

StrToInt() should imo crash if the input is not a number.

Dgby714

2011-04-01 22:00:29

Its just a wrapper around FreePascals StrToInt, Tho it should throw an exception.

Sex

2011-04-01 22:01:41

Ah, I'd find myself to agree with you now.
 StrToBool('') -> Error: Exception: "" is not a valid boolean. at line 3
Therefore StrToInt('hi') should throw Error: Exception: "hi" is not a valid integer. at line 3

Coh3n

2011-04-02 01:37:35

I agree with this. The same should be done for all the conversion methods.

Gu3st...

2011-04-02 06:21:40

It did throw an error before iirc… Wonder what changed :confused:

Coh3n

2011-04-02 14:48:21

From: Zyt3x
It did throw an error before iirc… Wonder what changed :confused:

It threw an error in Simba? As long as I can remember, Simba always returned -1.

Smartzkid

2011-04-02 15:22:58

function StrToInt(str: string; var value): bool

Perhaps this would be a better format?

Coh3n

2011-04-02 15:27:45

Ew, I don't like that. =\

Most conversion methods are used to Writeln and are already a pain. Throwing an exception just makes more sense IMO.

Smartzkid

2011-04-02 15:30:18

In those cases, you would use StrToIntDef

Coh3n

2011-04-02 15:38:09

From: Smartzkid
In those cases, you would use StrToIntDef

Touche.

Wizzup?

2011-04-02 17:05:25

I think it should raise an exception. Please file a bug. :)

Gu3st...

2011-04-02 18:30:41

From: Wizzup?
I think it should raise an exception. Please file a bug. :)
Thanks, and done.

Wizzup?

2011-04-07 15:33:09

Well, apparently PascalScript changed this. From their source code:

function StrToInt(const S: TbtString): LongInt;
var
e: Integer;
Res: LongInt;
begin
Val(string(S), Res, e);
if e <> 0 then
StrToInt := -1
else
StrToInt := Res;
end;

Coh3n

2011-04-08 06:12:42

Well tell them to change it back!

But if not.. couldn't some form of wrapper function be made to overwrite the PS version? If absolutely necessary of course.

Wizzup?

2011-04-08 06:18:23

From: Coh3n
Well tell them to change it back!

But if not.. couldn't some form of wrapper function be made to overwrite the PS version? If absolutely necessary of course.


Yeah, we can surely just change it. I was just trying to find out why it didn't work in Simba at first. Then we'll have to make a decision.

Gu3st...

2011-04-08 10:40:12

I already added a note to bugs.villavu.com, but I'll post here too..

We can change it to
if e <> 0 then
ThrowError(); //
else
StrToInt := Res;

Coh3n

2011-04-08 12:21:08

From: Zyt3x
I already added a note to bugs.villavu.com, but I'll post here too..

We can change it to
if e <> 0 then
ThrowError(); //
else
StrToInt := Res;

<> -1, you mean? What if the string is actually '-1'?

Dgby714

2011-04-08 12:28:11

raise Exception.Create('Some Error….');

nielsie95

2011-04-08 13:23:49

We should just use the StrToInt from FPC/Lazarus.

Wizzup?

2011-04-08 13:28:56

From: nielsie95
We should just use the StrToInt from FPC/Lazarus.


That was what I was going suggest. :)

Gu3st...

2011-04-08 15:06:46

From: Coh3n
<> -1, you mean? What if the string is actually '-1'?
nope, e is <> 0 when there is an error :)

From: nielsie95
We should just use the StrToInt from FPC/Lazarus.
:D

http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/strtoint.html

Wizzup?

2011-04-09 17:50:30

Resolved in master. Will be in 0.97 rc2.

Gu3st...

2011-04-09 19:07:51

Great! Thanks :)