Testing if a String is a number

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Testing if a String is a number

Damien Cassou-3
Hi,

I'm surprised there is no #isNumber selector for strings. If I want to
detect if a string is a number or not, it's not so easy. One of the
possible implementation would be:

String>>isNumber
   value := self asNumber.
   value isZero
        ifFalse: [^ true]
        ifTrue:  [^ self first = $0]


But this is buggy and incomplete:

'dfdfd' isNumber false     <- correct
'12' isNumber  true        <- correct
'0' isNumber true          <- correct
'0ef' isNumber true        <- Wrong !
'2r0' isNumber false       <- Wrong !


Should I open a bug report on Mantis ?

Reply | Threaded
Open this post in threaded view
|

Re: Testing if a String is a number

Nicolas Cellier-3
Hi Damien

I suggest you use my Squeak NumberParser (See http://bugs.impara.de/view.php?id=3512 ) which has error handling a little bit more developped than just answering zero...

You could simply test is aNumber is read without error from (self readStream), and assert the stream atEnd.

Nicolas

Damien Cassou:

> Hi,
>
> I'm surprised there is no #isNumber selector for strings. If I want to
> detect if a string is a number or not, it's not so easy. One of the
> possible implementation would be:
>
> String>>isNumber
>    value := self asNumber.
>    value isZero
>         ifFalse: [^ true]
>         ifTrue:  [^ self first = $0]
>
>
> But this is buggy and incomplete:
>
> 'dfdfd' isNumber false     <- correct
> '12' isNumber  true        <- correct
> '0' isNumber true          <- correct
> '0ef' isNumber true        <- Wrong !
> '2r0' isNumber false       <- Wrong !
>
>
> Should I open a bug report on Mantis ?
>
>

________________________________________________________________________
iFRANCE, exprimez-vous !
http://web.ifrance.com


Reply | Threaded
Open this post in threaded view
|

Re: Testing if a String is a number

Damien Cassou-3
[hidden email] wrote:
> I suggest you use my Squeak NumberParser (See http://bugs.impara.de/view.php?id=3512 ) which has error handling a little bit more developped than just answering zero...
>
> You could simply test is aNumber is read without error from (self readStream), and assert the stream atEnd.

Hi Nicolas,

I thought about your parser but did not tried it yet. Does somebody plan
to include NumberParser into main stream ? I think it corrects some
bugs, is a good refactoring and brings additional possibilities.

Thank you Nicolas

Bye

Reply | Threaded
Open this post in threaded view
|

Re: Testing if a String is a number

Damien Cassou-3
In reply to this post by Nicolas Cellier-3
[hidden email] wrote:
> I suggest you use my Squeak NumberParser (See http://bugs.impara.de/view.php?id=3512 ) which has error handling a little bit more developped than just answering zero...
>
> You could simply test is aNumber is read without error from (self readStream), and assert the stream atEnd.

Your parser works perfectly. Now, #stringIsNumber should be implemented as:

stringIsNumber
   | stream |
   stream _ self readStream.
   SqNumberParser parse: stream onError: [^ false].
   ^ stream atEnd


Next line is a subliminal message, don't read it please !
It would be cool to have it in the base image, please include it


Thank you

Reply | Threaded
Open this post in threaded view
|

Re: Testing if a String is a number

Nicolas Cellier-3
In reply to this post by Damien Cassou-3

However, there are two details:

1) maybe you have to care about separators
for example '   10' is Number will be true, while '10   ' isNumber will be false
think of changing ^stream skipSeparators; atEnd

2) the #isNumber method name is questionnable
Current implementation in virgin images is a short cut of (isKindOf: Number),
Most senders of isNumber will really expect a Number, not a String
try (2 * '4') or ('3.14' sin) in your image, and you will see what i mean.

If you want strings to become unevaled (differed evaluation) objects, then it makes me think of a lisp approach, String quotes being equivalent somehow to lisp uneval quote...

Nicolas


Damien Cassou:

> [hidden email] wrote:
> > I suggest you use my Squeak NumberParser (See http://bugs.impara.de/view.php?id=3512 ) which has error handling a little bit more developped than just answering zero...
> >
> > You could simply test is aNumber is read without error from (self readStream), and assert the stream atEnd.
>
> Your parser works perfectly. Now, #stringIsNumber should be implemented as:
>
> stringIsNumber
>    | stream |
>    stream _ self readStream.
>    SqNumberParser parse: stream onError: [^ false].
>    ^ stream atEnd
>
>
> Next line is a subliminal message, don't read it please !
> It would be cool to have it in the base image, please include it
>
>
> Thank you
>

________________________________________________________________________
iFRANCE, exprimez-vous !
http://web.ifrance.com


Reply | Threaded
Open this post in threaded view
|

Re: Testing if a String is a number

Damien Cassou-3
[hidden email] wrote:
> However, there are two details:
>
> 1) maybe you have to care about separators
> for example '   10' is Number will be true, while '10   ' isNumber will be false
> think of changing ^stream skipSeparators; atEnd


Thank you for this precision. I can strip the string too.


> 2) the #isNumber method name is questionnable
> Current implementation in virgin images is a short cut of (isKindOf: Number),
> Most senders of isNumber will really expect a Number, not a String
> try (2 * '4') or ('3.14' sin) in your image, and you will see what i mean.

I've noticed this and change the name in my previous mail to
#stringIsNumber.


Thank you

Reply | Threaded
Open this post in threaded view
|

RE: Testing if a String is a number

Peter Crowther-2
In reply to this post by Damien Cassou-3
> From: Damien Cassou
> I'm surprised there is no #isNumber selector for strings.

Do you mean "responds to arithmetic messages" or "could be converted
into something that responds to arithmetic messages"?  #isNumber means
the first (to me); your use seems to be the second.  Given that I have
code that tests for #isNumber before sending (eg) #+ to an object, I'd
be unhappy about the second use!

                - Peter

Reply | Threaded
Open this post in threaded view
|

Re: Testing if a String is a number

tblanchard
Agree - isThing usually tests if the object is an instance of Thing.

A message name like isNumberAsString would be more "standard".

On Jul 5, 2006, at 12:09 PM, Peter Crowther wrote:

>> From: Damien Cassou
>> I'm surprised there is no #isNumber selector for strings.
>
> Do you mean "responds to arithmetic messages" or "could be converted
> into something that responds to arithmetic messages"?  #isNumber means
> the first (to me); your use seems to be the second.  Given that I have
> code that tests for #isNumber before sending (eg) #+ to an object, I'd
> be unhappy about the second use!
>
> - Peter
>


Reply | Threaded
Open this post in threaded view
|

Re: Testing if a String is a number

Damien Cassou-3
In reply to this post by Peter Crowther-2
> Do you mean "responds to arithmetic messages" or "could be converted
> into something that responds to arithmetic messages"?  #isNumber means
> the first (to me); your use seems to be the second.  Given that I have
> code that tests for #isNumber before sending (eg) #+ to an object, I'd
> be unhappy about the second use!

Ok, I'm sorry. I meant #stringIsNumber that test if a string is a
representation of a number: '123' for example.