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

Standardize Str::startsWith() and endsWith() with empty needle #2627

Closed
vlakoff opened this Issue Nov 3, 2013 · 2 comments

Comments

Projects
None yet
2 participants
@vlakoff
Contributor

vlakoff commented Nov 3, 2013

Currently, starts_with('foobar', '') throws a warning (and returns false) and ends_with('foobar', '') returns true.

I think the warning should be removed, and the return value of these cases unified.

And, per this answer on stackoverflow:
Java and .NET implementations of String.StartsWith and String.EndsWith return true if needle is an empty string.

@vlakoff

This comment has been minimized.

Contributor

vlakoff commented Nov 3, 2013

Also, for the curious: why it returns true.

@vlakoff

This comment has been minimized.

Contributor

vlakoff commented Nov 3, 2013

Str::contains('foobar', '') could be fixed similarly. It currently produces a warning.

Please note that PHP function strpos('foobar', '') produce a warning, contrarily to Java and .NET 's indexOf() which return true.

So, we have two solutions for Laravel's contains(), startsWith() and endsWith():

  • Act like PHP and don't support empty needles
  • Act like Java and .NET and return true. PHP's strpos() being the exception.

Your opinion on this?

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