Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String>>#asInteger has not defined behavior #17277

Open
jordanmontt opened this issue Oct 16, 2024 · 1 comment · May be fixed by #17299
Open

String>>#asInteger has not defined behavior #17277

jordanmontt opened this issue Oct 16, 2024 · 1 comment · May be fixed by #17299

Comments

@jordanmontt
Copy link
Member

The comment of the method says the following:

String >> #asInteger
    "Return the integer present in the receiver, or nil. In case of float, returns the integer part."
    ....

But is not clear. Does returns the integer represented by String, what happens if the String is not an integer.

As an example, let's look at this:

'Weird222String' asInteger. "222"
'Weird222String111' asInteger. "222"
'333Weird222String111' asInteger. "333"

If we see into the implementation, the method returns the first characters that can be parsed as an integer and it stops as soon as there is something else.

I propose to use the NumberParser to parse the String and raise an exception if the complete String does not correspond to an integer.

@jordanmontt jordanmontt changed the title String >> #asInteger has not defined behavior String>>#asInteger has not defined behavior Oct 16, 2024
@jordanmontt jordanmontt linked a pull request Oct 18, 2024 that will close this issue
@astares
Copy link
Member

astares commented Oct 21, 2024

Funny enough this seems to be the case since Squeak times. I tried in SqueakJS (squeak.js.org).

image

not a problem in Cuis or Squeak Mini image as there is no implementation of #toInteger for String

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants