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

Weird substring behaviour #55

Open
wasamasa opened this issue Apr 23, 2016 · 3 comments
Open

Weird substring behaviour #55

wasamasa opened this issue Apr 23, 2016 · 3 comments

Comments

@wasamasa
Copy link

"x".substring(1) errors out for me with an out of bounds exception. I find this unexpected, as translating that code to Python, Ruby, Emacs Lisp, etc. gives me an empty string as result. Is this a bug or expected behaviour?

@aimran96
Copy link
Contributor

Try this: "x".substring(0)
It returns the string "x"

@wasamasa
Copy link
Author

wasamasa commented Apr 24, 2016

Well, yeah, I'm aware of that part of it behaving as expecting. Perhaps it would help to explain my motivation here.

I'm in the process of creating a tokenizer which scans through a string for tokens. This is done by doing a regex match on the original string, extracting the token from it, then repeating the process on the remaining string. In the end I only have a string with a single token in it. I've chosen to detect this by the remaining string after it being an empty string. Unfortunately ChucK doesn't let me detect that and instead errors out when creating a substring starting at the end of the string which is unexpected behaviour for me as it's not how it works in other programming languages. Therefore I'm considering it to be a bug or at the very least, something that ought to be documented.

@spencersalazar
Copy link
Member

I wasnt aware of this behavior of string APIs on other systems; it seems it would be useful to model after those.

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

No branches or pull requests

3 participants