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

Add a new Psl\Str\reverse() function #238

Merged
merged 14 commits into from
Oct 18, 2021
Merged

Add a new Psl\Str\reverse() function #238

merged 14 commits into from
Oct 18, 2021

Conversation

yivi
Copy link
Contributor

@yivi yivi commented Oct 17, 2021

Psl\Str\Byte\reverse() exists, providing the functionality of strrev(), but there is no multibyte equivalent on the Psl\Str namespace.

This still breaks on some complex characters, as some multi byte emojis, but for that it would probably better to create a Psl\Str\Grapheme\reverse() for completion sake.

@coveralls
Copy link

coveralls commented Oct 17, 2021

Pull Request Test Coverage Report for Build 1354621226

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+4.0e**-05%**) to 99.936%

Totals Coverage Status
Change from base Build 1328169160: 4.0e-05%
Covered Lines: 3125
Relevant Lines: 3127

💛 - Coveralls

src/Psl/Str/reverse.php Outdated Show resolved Hide resolved
src/Psl/Str/reverse.php Show resolved Hide resolved
src/Psl/Str/reverse.php Outdated Show resolved Hide resolved
tests/unit/Str/ReverseTest.php Outdated Show resolved Hide resolved
yivi and others added 6 commits October 18, 2021 07:52
split -> chunk. named 'encoding' parameter.

Co-authored-by: Saif Eddin Gmati <29315886+azjezz@users.noreply.github.com>
add @psalm-pure

Co-authored-by: Saif Eddin Gmati <29315886+azjezz@users.noreply.github.com>
tests are final

Co-authored-by: Saif Eddin Gmati <29315886+azjezz@users.noreply.github.com>
@azjezz azjezz added hacktoberfest hacktoberfest-accepted Priority: Medium This issue may be useful, and needs some attention. Status: Revision Needed At least two people have seen issues in the PR that makes them uneasy. Type: Enhancement Most issues will probably ask for additions or changes. labels Oct 18, 2021
@azjezz azjezz added this to the 1.9.0 milestone Oct 18, 2021
@yivi
Copy link
Contributor Author

yivi commented Oct 18, 2021

Mmmh. I think this would be better accomplished with a regex_split. With this it would be possible to capture multibyte extended unicode sequences:

use Psl\Vec;

$rev_array = Vec\reverse(\preg_split('/(\X)/u', $string, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY));

I've been testing and this would work with much more complex unicode sequences.

The "problem" is that as far as I can see, we cannot pass additional flags to Regex\split()

For a use-case like this, would using the native preg_split() over the Psl version be fine?

Also, going through preg_split instead of mb_* functions would make $encoding not needed. Which I don't know if it's a good or a bad thing.

@azjezz
Copy link
Owner

azjezz commented Oct 18, 2021

I think that's not needed, I'm happy with the current version, and i don't think we need to overdo it here.

@azjezz azjezz merged commit 2f0dad8 into azjezz:1.9.x Oct 18, 2021
@yivi
Copy link
Contributor Author

yivi commented Oct 18, 2021

Good. Thanks for all your help.

Further checking shows that the regex version also seems to choke with extended unicode chars (trying with emojis like this '👩‍👩‍👦👩🏽‍❤️‍👨🏼👩🏽‍🏫', given my ignorance of more interesting languages).

Would it be ok if I also created a PR for Str\Grapheme\reverse(), for completeness sake?

@azjezz
Copy link
Owner

azjezz commented Oct 18, 2021

Would it be ok if I also created a PR for Str\Grapheme\reverse(), for completeness sake?

Yes, i think that would be a nice addition as well :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest hacktoberfest-accepted Priority: Medium This issue may be useful, and needs some attention. Status: Revision Needed At least two people have seen issues in the PR that makes them uneasy. Type: Enhancement Most issues will probably ask for additions or changes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants