-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Patch add str_starts_with and str_ends_with #5300
Conversation
@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):
|
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. |
Fixed that this morning, thanks. |
There was a problem hiding this 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
@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 |
Thank you =) The idea of empty strings tests is basically that both |
0ab6f6b
to
31ba825
Compare
@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:
Its failed like that at least two times in a row, so not sure what is going on. I also changed |
ext/standard/string.c
Outdated
} | ||
|
||
k = ZSTR_LEN(haystack) - ZSTR_LEN(needle); | ||
RETURN_BOOL(memcmp(&(ZSTR_VAL(haystack))[k], ZSTR_VAL(needle), ZSTR_LEN(needle)) == 0); |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this 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:
Thanks for the suggestions, @guilliamxavier I incorporated them into the tests! |
@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
|
There was a problem hiding this 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; |
There was a problem hiding this comment.
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.
…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
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 .