[Cuis-dev] Bug or feature? #asInteger behaviour

Juan Vuletich juan at jvuletich.org
Wed Jan 6 05:27:22 PST 2021


On 1/6/2021 9:44 AM, Hernan Wilkinson via Cuis-dev wrote:
> Definitely a bug.

I think whoever wrote that method was thinking #asPositiveInteger, which 
might have been a better name. Besides, I think that something like 
#findPositiveInteger would have been much better.

> Attached is a change set that fixes it with tests to verify the 
> behavior of String>>asInteger
> I think we should think the behavior a little bit through... for 
> example '---1' signals an error but '+++1' does not, or '.1' returns 1 
> but '0.1' returns 0...
>  I thinks this are rare cases and the solution is not what we would 
> expect.
>
> Cheers!
> Hernan.

What about the below? It gives what I think are the correct answers for 
'--1', '- 1', '---1' and '-+1'. To me, the correct answer is "seach for 
the first well formed integer number, ignoring all the rest'. It breaks 
your test, though.

Anyway, looking at what the comment says, I'm more convinced that we'd 
leave as it was, but rename it as #findPositiveInteger.

asInteger
     "Answer the Integer created by interpreting the receiver as the 
string representation of an integer.  Answer nil if no digits, else find 
the first digit and then all consecutive digits after that"

     | startPosition tail endPosition answer |
     startPosition _ self findFirst: [:ch | ch isDigit].
     startPosition = 0 ifTrue: [^ nil].
     tail _ self copyFrom: startPosition to: self size.
     endPosition _ tail findFirst: [:ch | ch isDigit not].
     endPosition _ endPosition = 0
         ifFalse: [endPosition-1]
         ifTrue: [tail size].
     answer _ Number readFrom: (tail copyFrom: 1 to: endPosition) 
readStream.
     (startPosition > 1 and: [ (self at: startPosition-1) = $- ])
         ifTrue: [ answer _ answer negated].
     ^answer

"
'1796exportFixes-tkMX' asInteger
'1848recentLogFile-sw'  asInteger
'donald' asInteger
'abc234def567' asInteger
'-1' asInteger
'--1' asInteger
'---1' asInteger
"


> On Wed, Jan 6, 2021 at 6:27 AM Philip Bernhart via Cuis-dev 
> <cuis-dev at lists.cuis.st <mailto:cuis-dev at lists.cuis.st>> wrote:
>
>     Hi,
>
>     I just noticed that #asInteger has an unexpected behaviour.
>     That I thought is worth pointing out. Depending on what
>     someone could interpret as an integer (all unsigned numbers or
>     including negative numbers).
>
>     I found out while looking at some Pharo code that:
>
>     '-1' asInteger => -1 (in Pharo)
>     '-1' asInteger => 1 (in Cuis)
>
>     'Mötör-45heäd' asInteger => -45 (in Pharo)
>     'Mötör-45heäd' asInteger => 45 (in Cuis)
>
>     So the sign is lost.
>
>     Was this intentional? Bug or feature?
>
>
>     Thanks,
>     Philip
>     -- 
>     -- 
>     Cuis-dev mailing list
>     Cuis-dev at lists.cuis.st <mailto:Cuis-dev at lists.cuis.st>
>     https://lists.cuis.st/mailman/listinfo/cuis-dev
>
>
>
> -- 
> *Hernán Wilkinson
> Agile Software Development, Teaching & Coaching*
> *Phone: +54-011*-4893-2057
> *Twitter: @HernanWilkinson*
> *site: http://www.10Pines.com <http://www.10pines.com/>*
> Address: Alem 896, Floor 6, Buenos Aires, Argentina

Thanks,

-- 
Juan Vuletich
www.cuis-smalltalk.org
https://github.com/Cuis-Smalltalk/Cuis-Smalltalk-Dev
https://github.com/jvuletich
https://www.linkedin.com/in/juan-vuletich-75611b3
@JuanVuletich

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20210106/8e67d69e/attachment-0001.htm>


More information about the Cuis-dev mailing list