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

Str::afterLast() #31085

Closed
m8d3 opened this issue Jan 9, 2020 · 10 comments · Fixed by #31095
Closed

Str::afterLast() #31085

m8d3 opened this issue Jan 9, 2020 · 10 comments · Fixed by #31095
Labels

Comments

@m8d3
Copy link

m8d3 commented Jan 9, 2020

  • Laravel Version: v6.10.1
  • PHP Version: 7.2

Description:

I think there is a bug in Str::afterLast().

Steps To Reproduce:

The statement Str::afterLast('----Foo','---') delivers "-Foo" instead of "Foo".

@devcircus
Copy link
Contributor

Which, to me, would be correct. Explain your expectation.
It says "give me everything after the last occurrence of "---". Which in the given case, is "-Foo"

@m8d3
Copy link
Author

m8d3 commented Jan 9, 2020

strpos 0: -
strpos 1: -
strpos 2: -
strpos 3: -
strpos 4: F
strpos 5: o
strpos 6: o

My expectation is, that the last occurrence of "---" is at strpos 1-3. So the Result has to be "Foo".

@Miguel-Serejo
Copy link

This does indeed look odd.

I would expect Str::afterLast($subject, $search) to be the equivalent of substr($string, strrpos($string, $search) + strlen($search)). Instead, it explodes the string by search and then returns the last part.

This is only a problem when the search string is only one character and there is a sequence of the same character in the subject string of a length that is not a multiple of the search string's length.

@driesvints
Copy link
Member

This works as expected.

@devcircus
Copy link
Contributor

Definitely see the issue now upon further inspection. 🤷🏻‍♂️

@m8d3
Copy link
Author

m8d3 commented Jan 10, 2020

@driesvints Could you please explain, why the last occurrence of "---" is not at strpos 1-3?

@driesvints
Copy link
Member

Hmm, you're indeed correct. I misread this. Wondering what's causing this.

@driesvints driesvints reopened this Jan 10, 2020
@driesvints driesvints added the bug label Jan 10, 2020
@Miguel-Serejo
Copy link

I tried to explain the cause in my first post.

The current implementation explodes the string using the search string as a delimiter. This works fine for almost all cases.

It only breaks when the search string is a sequence of the same character and there is a sequence of that character of length greater than the search string and not a multiple of the length of the search string, because the string explodes on the first occurrence of the search string. ----Foo explodes into [ '', '-Foo'].

For the example being used here, the search string -- would work fine, because the length of the sequence ---- in ----Foo is a multiple of the length of --.

If we want to fix this edge case, Str::last could just return substr($string, strrpos($string, $search) + strlen($search)). I don't know the performance impact.

@driesvints
Copy link
Member

I tried substr($string, strrpos($string, $search) + strlen($search)) but that breaks some of the other use cases unfortunately.

@driesvints
Copy link
Member

I've sent in a fix for this. Thanks for reporting 👍

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

Successfully merging a pull request may close this issue.

4 participants