Testing if a String is a number
ncellier at ifrance.com
ncellier at ifrance.com
Wed Jul 5 11:25:41 UTC 2006
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:
> ncellier at ifrance.com 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
More information about the Squeak-dev
mailing list
|