Skip to content

Patch add str_starts_with and str_ends_with #5300

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

Conversation

wkhudgins92
Copy link
Contributor

@wkhudgins92 wkhudgins92 commented Mar 25, 2020

This is the patch for the following RFC: https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions

Contains the str_starts_with and str_ends_with functions as well as tests.

Overall this is a narrowed-down revival of https://wiki.php.net/rfc/add_str_begin_and_end_functions and #2049 .

@glensc
Copy link
Contributor

glensc commented Mar 26, 2020

@wkhudgins92 the wiki states that php8 has been released, can you link that text or correct it. as to my knowledge php8 is not yet released (to be released in December 2020):

PHP8 has now been released and several individuals have requested the str_starts_with and str_ends_with functionality again. This is a simple but highly desired functionality for PHP.

@nikic nikic changed the title Patch add str starts with and str ends with Patch add str_starts_with and str_ends_with Mar 26, 2020
@guilliamxavier
Copy link
Contributor

Contains the str_starts_with and str_ends_with functions as well as tests.

Did you forget to commit the tests? 😅 (also please include tests for empty strings and NUL byte, like https://github.com/php/php-src/pull/5179/files#diff-efcf36fef88a923719524d7a5c568689)

@wkhudgins92
Copy link
Contributor Author

Contains the str_starts_with and str_ends_with functions as well as tests.

Did you forget to commit the tests? 😅 (also please include tests for empty strings and NUL byte, like https://github.com/php/php-src/pull/5179/files#diff-efcf36fef88a923719524d7a5c568689)

Ooops I forgot to commit the tests...I was really rushing to get this out last night, I knew if I didn't get it raise up last night it might take me another week to do anything on it! These are the tests from the old RFC I raised, they don't include empty strings or NULL byte but I can add those.

@wkhudgins92
Copy link
Contributor Author

@wkhudgins92 the wiki states that php8 has been released, can you link that text or correct it. as to my knowledge php8 is not yet released (to be released in December 2020):

PHP8 has now been released and several individuals have requested the str_starts_with and str_ends_with functionality again. This is a simple but highly desired functionality for PHP.

Fixed that this morning, thanks.

Copy link
Contributor

@guilliamxavier guilliamxavier left a comment

Choose a reason for hiding this comment

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

Thanks for the tests ;) You can indeed complete them (and address nikic's review) when you get some time again

@wkhudgins92
Copy link
Contributor Author

@guilliamxavier I added to the test cases as you requested and fixed the typos you found. To be honest, I'm not 100% sure I did the NULL BYTE tests properly, but that was my best guess at the time. I tested hour Python and Java handle startswith and endswith functionality, and at least with empty strings it is the same. I'll get to @nikic idea of using memcmp for str_ends_with soon.

@guilliamxavier
Copy link
Contributor

Thank you =) The idea of empty strings tests is basically that both ("a", "") and ("", "") give true while ("", "a") gives false (for both str_{starts,ends}_with like for str_contains), so the ones you added seem OK. Concerning NUL byte tests, the goal is to ensure that the functions are "binary safe" in that they handle \0 just like any other char (and don't crash nor stop at it), e.g. that str_starts_with gives true for ("a\x00bc", "a\x00b") (no crash) but false for all of ("a\x00b", "a\x00d"), ("a\x00b", "z\x00b"), ("a", "a\x00") and ("a", "\x00a") (no stop), and similarly for str_ends_with (with reversed strings), maybe as a complement of those you added.

@wkhudgins92 wkhudgins92 force-pushed the patch-add-str_starts_with-and-str_ends_with branch from 0ab6f6b to 31ba825 Compare March 31, 2020 23:49
@wkhudgins92
Copy link
Contributor Author

@guilliamxavier I added some more to the null byte test cases. All my test seem to be passing its just one of the Travis builds fails with this error:

No output has been received in the last 10m0s, this potentially indicates a stalled build or something wrong with the build itself.
Check the details on how to adjust your build configuration on: https://docs.travis-ci.com/user/common-build-problems/#build-times-out-because-no-output-was-received
The build has been terminated

Its failed like that at least two times in a row, so not sure what is going on.

I also changed str_ends_with to utilize a call to memcmp as suggested by @nikic , I believe I got that right. Unless other things come up on the PR, I'm going to incorporate some feedback I've received into the RFC page next.

}

k = ZSTR_LEN(haystack) - ZSTR_LEN(needle);
RETURN_BOOL(memcmp(&(ZSTR_VAL(haystack))[k], ZSTR_VAL(needle), ZSTR_LEN(needle)) == 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

&(ZSTR_VAL(haystack))[k] seems "non idiomatic" in this file, I would rather see
ZSTR_VAL(haystack) + ZSTR_LEN(haystack) - ZSTR_LEN(needle) and get rid of k entirely
(but let's see what nikic thinks)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm alright with changing it, I don't do a lot of C so I had to play around some to figure that one out. More than happy to use a more concise or idiomatic way.

Copy link
Contributor

@guilliamxavier guilliamxavier left a comment

Choose a reason for hiding this comment

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

Thanks for updating the PR. I still think that both test files should be kept "symmetrical", so (and this should be my last review of tests) I have added applicable suggestions:

@wkhudgins92
Copy link
Contributor Author

Thanks for the suggestions, @guilliamxavier I incorporated them into the tests!

@TysonAndre
Copy link
Contributor

TysonAndre commented May 5, 2020

@wkhudgins92 - I'd strongly suggest that you squash the 22 commits in https://github.com/php/php-src/pull/5300/commits so that it can get merged

  • Other RFCs are generally merged as a single commit when possible to do so, e.g. 1668ad7
  • It makes sense to have each commit work as a change on its own - some of these changes only make sense in combination with other changes (e.g. "changed spaces to tabs")
  • Attempting to merge this branch into master locally results in merge conflicts - Some commits change ext/standard/php_string.h and then revert the change.

Copy link
Member

@nikic nikic left a comment

Choose a reason for hiding this comment

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

This looks basically fine, I'll apply final fixups while merging.

PHP_FUNCTION(str_ends_with)
{
zend_string *haystack, *needle;
int k;
Copy link
Member

Choose a reason for hiding this comment

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

This needs to use size_t, otherwise it may truncate.

@php-pulls php-pulls closed this in 31fb6a0 May 5, 2020
@carusogabriel carusogabriel added this to the PHP 8.0 milestone May 29, 2020
jrfnl added a commit to PHPCompatibility/PHPCompatibility that referenced this pull request Aug 10, 2020
…h() functions

> `str_starts_with()` checks if a string begins with another string and returns a boolean value (true/false) whether it does.
> `str_ends_with()` checks if a string ends with another string and returns a boolean value (true/false) whether it does.

Refs:
* https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions
* php/php-src#5300
* php/php-src@31fb6a0
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 this pull request may close these issues.

7 participants