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#start_with? inconsistent with other Ruby implementations #2434

Closed
tobiashm opened this issue Jan 6, 2015 · 2 comments
Closed

String#start_with? inconsistent with other Ruby implementations #2434

tobiashm opened this issue Jan 6, 2015 · 2 comments

Comments

@tobiashm
Copy link

tobiashm commented Jan 6, 2015

Both MRI and Rubinius returns true when testing if an empty string starts with an empty string. But JRuby returns false. But when testing if the empty string ends with an empty string, they all return true.

I would suggest that JRuby should change its behavior for String#start_with?.
I've looked in the source code, but wasn't able to figure out where and how this change should be implemented, partly because it seems that the call is forwarded to some Java classes.

$ rvm jruby-head,ruby,rbx do ruby -e "p ''.start_with?('')"
false
true
true

$ rvm jruby-head,ruby,rbx do ruby -e "p ''.end_with?('')"
true
true
true
@enebo
Copy link
Member

enebo commented Jan 6, 2015

This also happens on 1.7 as well. Marked for 1.7.19 since this should be simple to fix (no doubt just looking at end_with? will give the snippet missing from begin_with?). I even marked it beginner if someone wants to try and submit their first PR.

@headius
Copy link
Member

headius commented Jan 6, 2015

This appears to be a bug in ByteList. The logic for startsWith includes this line:

if (realSize == 0 || this.realSize < other.realSize + toffset) return false;

Whether we should change this behavior is debateable...on the one hand, the string doesn't start with anything. On the other, it starts with nothing and the incoming string is nothing.

In any case, we need to fix at Ruby level, and it appears that end_with also uses this logic, so there must be a short-circuit somewhere.

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