-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Patch add str begins and ends #2049
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 begins and ends #2049
Conversation
Not sure what the build failure is about. I was able to compile this on my local machine using |
ext/standard/string.c
Outdated
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "SS|l", &str, &search_value, &case_sensitive) == FAILURE) | ||
RETURN_NULL(); | ||
|
||
for (int i = 0; i < search_value->len; i++) { |
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.
The int i
should go to the top of the function. That should fix the build issue.
how does str_begins different from strncmp/strncasecmp? <?php
$str = "bEginningMiddleEnd";
var_dump(strcmp($str, "bEg", 3));
var_dump(strncasecmp($str, "beg", 3)); |
and how does str_begins and str_ends better than substr_compare? <?php
$str = "bEginningMiddleEnd";
var_dump(substr_compare($str, "End", -3));
var_dump(substr_compare($str, "end", -3, 3, true));
?> |
Without getting into micro-optimization, str_ends($str, $end); is easier to write but more importantly a lot easier to read than substr_compare($str, $end, -strlen($end), strlen($end), true); Just from the name of it anybody should understand what it does without having to check the documentation. @wkhudgins92 It would have been even better to name them |
IMO this can easily be achieved in userland with a simple function. |
@staabm Of course ! But this is a so common task that most programming languages have their native implementations:
|
I would fully agree, if these were methods of a string object ( |
Not sure I see the "functions" issue. "startswith" still imposes an ordering, that the first arg startswith the second arg. Unlike just "starts" which would imply the reverse argument order (first arg starts second arg). |
Heh. It kinda does make sense to invert the argument order in Haskell in particular, because this makes partial application more useful... |
Yes, that makes particular sense for Haskell (and other functional languages), but they could have called it |
There are already such implementations based on @nikic extension in here https://github.com/rossriley/php-scalar-objects#startswith scalar objects in PHP would be awesome feature, would love to have it in core. Great job on this extension from @nikic |
But they didn't. 😉 Wrt the order of arguments, as @nikic said, you're either in an OO-like language (or library, like glib/gobject) and you expect Which raises the infamous issue: function array_filter(array $array, callable $callback): array;
function array_map(callable $callback, array $array): array; deserving a RFC on it's own ! 😆 |
Edited the code and modified the RFC (https://wiki.php.net/rfc/add_str_begin_and_end_functions) to reflect the comments brought up here and in the mailing list. I split the functions into separate functions based on case sensitivity and switched the order of |
Will these have |
ext/standard/string.c
Outdated
RETURN_NULL(); | ||
|
||
for (i = 0; i < search_value->len; i++) | ||
if (str->val[i] != search_value->val[i]) |
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.
When the length of search_value is greater than str, maybe it will access out of bounds?
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.
can you change the the interface to bool str_begin(string $haytstack, $needle)
and bool str_ends(string $haystack, string $needle)
for consistency with other string function like stristr, strpos, strrchr?
support~ |
7fc90b6
to
10205e8
Compare
ext/mbstring/mbstring.c
Outdated
@@ -2581,6 +2609,192 @@ PHP_FUNCTION(mb_strripos) | |||
} | |||
/* }}} */ | |||
|
|||
/* {{{ proto boolean mb_str_begins(string haystack, string needle [, string encoding]) | |||
Checks if haystack begins with needle */ |
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.
Checks if haystack begins with needle */ | |
Checks if haystack begins with a needle */ |
Thanks for working on this 👍
i quite agree with this ( or |
…2/php-src into patch-add-str_begins-and-ends
Is this RFC going to vote before the deadline for PHP 7.4 in a few days? |
@theodorejb Yesterday marked 2 weeks of discussion, so I plan to move the RFC to a vote. |
The RFC related to this has gone to a vote: https://wiki.php.net/rfc/add_str_begin_and_end_functions |
Does anyone know why the |
The RFC was unfortunately :( :( :( declined, so I'm closing this PR. |
That's mad... All thanks to the silly 2/3 thing is seems. >50% is a majority, when will people realise that 😂 =| |
the plain string vs mbstring vote difference seems stupid. probably shouldn't even have the opportunity to vote separately. however, I'm personally looking for Symfony 5 string component: |
So... is there any possibility to redo the voting and add those functions together with str_contains? It’s the same family. :/ |
I am going to re-raise the RFC+PR for only |
otoh, maybe instead of adding two sets of functions, add support for substring operators on the language level, like in Python (and Ruby?) do:
but this can't be done until the $string can be declared as ByteArray or UnicodeString, or maybe add also object support to have custom slicers support like |
@glensc: The "slice" syntactic sugar idea (which I have already seen proposed elsewhere BTW) is IMHO orthogonal to thoses functions (which are not implemented as |
This is the patch for RFC
https://wiki.php.net/rfc/add_str_begin_and_end_functions
This PR contains the implementations of str_begins() and str_ends() and also the phpt tests.