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] add after and before helpers #75

Closed
azjezz opened this issue Oct 3, 2020 · 2 comments · Fixed by #102
Closed

[Str] add after and before helpers #75

azjezz opened this issue Oct 3, 2020 · 2 comments · Fixed by #102
Labels
hacktoberfest Priority: Medium This issue may be useful, and needs some attention. Status: Available No one has claimed responsibility for resolving this issue. Type: Enhancement Most issues will probably ask for additions or changes.
Milestone

Comments

@azjezz
Copy link
Owner

azjezz commented Oct 3, 2020

add after{_ci|_last{_ci}} and before{_ci|_last{_ci}} helper string functions.

We should implement these functions for Psl\Str, Psl\Str\Byte, and Psl\Str\Grapheme ( refs #74 ), which totals to a new 24 functions.

Psl\Str implementations would have an extra argument ?string $encoding = null.

function after(string $haystack, string $needle, bool $include_needle = false, int $offset = 0): string;

function after_ci(string $haystack, string $needle, bool $include_needle = false, int $offset = 0): string;

function after_last(string $haystack, string $needle, bool $include_needle = false, int $offset = 0): string;

function after_last_ci(string $haystack, string $needle, bool $include_needle = false, int $offset = 0): string;

function before(string $haystack, string $needle, bool $include_needle = false, int $offset = 0): string;

function before_ci(string $haystack, string $needle, bool $include_needle = false, int $offset = 0): string;

function before_last(string $haystack, string $needle, bool $include_needle = false, int $offset = 0): string;

function before_last_ci(string $haystack, string $needle, bool $include_needle = false, int $offset = 0): string;
@azjezz azjezz added Priority: Medium This issue may be useful, and needs some attention. Status: Available No one has claimed responsibility for resolving this issue. Type: Enhancement Most issues will probably ask for additions or changes. hacktoberfest labels Oct 3, 2020
@azjezz azjezz added this to the v1.0.0 milestone Oct 3, 2020
@azjezz azjezz changed the title [Str] add after and before helpers [Str] add after and before helpers Oct 3, 2020
@khamer
Copy link

khamer commented Oct 3, 2020

I'm not sure what after and before are supposed to do, to be honest. Is the idea that

  • Str\after('hello world', 'w') === 'orld'
  • Str\after('hello world', 'w', true) === 'world'
  • Str\after('hello world', 'w', true, 2) === 'rld'
  • Str\after('hello world', 'w', false, 2) === 'ld'
  • Str\before('hello world', 'o') === 'hell'
  • Str\before('hello world', 'o', true) === 'hello'
  • Str\before('hello world', 'o', true, 2) === 'hello w'
  • Str\before('hello world', 'o', false, 2) === 'hello '

I understand based on hack that _last would indicate using the last occurrence of $needle in $haystack and _ci indicates case insensitive, but wasn't sure if I understood how you wanted after/before to work, and whether the offset was used as a length of to offset from $needle or what.

@azjezz
Copy link
Owner Author

azjezz commented Oct 3, 2020

your understanding is correct, but $offset is not used as a length, but as an $offset since these functions would use search, search_last, search_ci, and search_last_ci internally, the $offset argument should be passed to the search function.

see: after, afterLast, before, and beforeLast at https://github.com/symfony/string/blob/master/AbstractString.php#L130

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest Priority: Medium This issue may be useful, and needs some attention. Status: Available No one has claimed responsibility for resolving this issue. Type: Enhancement Most issues will probably ask for additions or changes.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants