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

[5.3] Changed startsWith and endsWith to use substr (non mbstring) #15380

Merged
merged 2 commits into from
Sep 11, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static function contains($haystack, $needles)
public static function endsWith($haystack, $needles)
{
foreach ((array) $needles as $needle) {
if ((string) $needle === static::substr($haystack, -static::length($needle))) {
if ((string) $needle === substr($haystack, - strlen($needle))) {
return true;
}
}
Expand Down Expand Up @@ -403,7 +403,7 @@ public static function snake($value, $delimiter = '_')
public static function startsWith($haystack, $needles)
{
foreach ((array) $needles as $needle) {
if ($needle != '' && mb_strpos($haystack, $needle) === 0) {
if ($needle != '' && substr($haystack, 0, strlen($needle)) === $needle) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Perturbatio Why not just strpos($haystack, $needle) === 0 ?

Copy link
Contributor Author

@Perturbatio Perturbatio Sep 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vlakoff strpos only bails when it finds a match, in the case of a non-match it runs from the start to the end of the string, if you have a large string (i.e. 4.5MB as in my example) you have to check the entire string which is inefficient since we only care about the first strlen($needle) bytes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, great change then :)

One issue though, $needle should be cast to string. I'll make a PR for this tonight.

return true;
}
}
Expand Down