[Pharo-users] I need the help of a guru about squeezeOutNumber
Sven Van Caekenberghe
sven at beta9.be
Sun Jul 1 03:47:01 EDT 2012
On 01 Jul 2012, at 02:15, Benjamin wrote:
> We figured out today that
> 'blabla1230' squeezeOutNumber ==> 123
> But it should be 1230.
> So after multiple attempts to debug this expression (with strange behaviors), we discovered that it's while parsing the reversed version of the string that it fails
> Indeed, when reversed, the trailing 0 become a leading 0, and is ignored (which in our case is wrong).
> So we removed this part, and all the tests are green again.
> But still we were wondering if the piece of code is needed somewhere or not ?
You made it simpler and the tests succeed, ergo it is better.
Is this needed/necessary, that is another question.
I would guess that the reversing was a kind of optimization.
> Thanks in advance,
> Ben and Cami
> :squeezeNumberOutOfString: stringOrStream onError: errorBlock
> "Try and find a number in this string. First, look if the string
> starts with a number. Then, see if it ends with a number. Then,
> remove a character from the front and see if the remaining
> string makes a number. Repeat the process until no characters
> are left or the number has been found. As soon as a number is
> found, it is returned. Otherwise, the method fails."
> | string try |
> stringOrStream isEmpty ifTrue: errorBlock.
> (self parse: stringOrStream onError: [nil])
> ifNotNilDo: [:result | ^ result ].
> (self parse: stringOrStream asString copy reversed onError: [nil])
> ifNotNilDo: [:result | ^ result asString reversed asNumber].
> string := stringOrStream.
> "We do the loop n-1 times because at the n-th iteration, string is empty"
> (stringOrStream size -1) timesRepeat: [
> string := string allButFirst.
> (self parse: string onError: [ nil ])
> ifNotNilDo: [ :result| ^ result ].].
> ^ errorBlock value
More information about the Pharo-users