Re: [VOTE] Voting opens for str_starts_with and ends_with functions
| From: | Claude Pache | Date: | Wed, 10 Jul 2019 10:20:10 +0000 |
| Subject: | Re: [VOTE] Voting opens for str_starts_with and ends_with functions | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-106204@lists.php.net to get a copy of this message | ||
> Le 8 juil. 2019 à 18:52, Andrey Andreev <narf@devilix.net> a écrit :
>
> Hi,
>
> On Mon, Jul 8, 2019 at 4:54 PM Claude Pache <claude.pache@gmail.com
> <mailto:claude.pache@gmail.com>> wrote:
>>
>>
>>> Le 8 juil. 2019 à 15:20, Christoph M. Becker <cmbecker69@gmx.de> a écrit :
>>>
>>> FTR, there is already substr_compare().
>>
>>
substr_compare() (as well as
>> strncmp() which I am currently using in lieu of
>> str_starts_with()) forces you to provides the substring and the
>> length of the substring, instead of just the substring:
>>
>> substr_compare('foobarbaz', 'foo', 0, 3) === 0
>> strncmp('foobarbaz', 'foo', 3) === 0
>> str_starts_with('foobarbaz', 'foo')
>>
>
> The existence of substr_compare() and strncmp() is also my main
> motivation for voting No, though I also share Zeev's reasoning.
>
> You're right that the 2 already existing functions are a bit less
> convenient to use, but the RFC doesn't even try to make a case for
> that, so how do we know this was even considered? While I'm not
> against having more than one way of doing things, I do think we need a
> compelling enough reason to add a third way of doing the same thing
> and here we don't even have an attempt to convince us.
>
> Cheers,
> Andrey.
The current existing solutions (using substr(), strncmp(),
substr_compare(), preg_match() or whatever) are sufficient—and may even
look elegant—if you use them once in a blue moon. But if you happen to use them frequently,
you’ll find soon that they are cumbersome and error-prone compared to the prospective
str_starts_with() and str_ends_with(). Concretely:
* Each time I write strncmp($foo, "bar", 3) === 0, I spend a few seconds to
double-check the number of bytes in "bar".
* Not later than yesterday, I spent one minute in order to find why substr_compare($file,
".css", -3) === 0 didn’t do what I meant. Note also that the alternative
substr($file, -3) === ".css" suffers from the same issue, and that the
alternative preg_match('/\\.css$/', $file) requires you to not forget to
escape special characters.
Checking whether a string begins, respectively ends, with some substring is a relatively common task
for some programmers. The issue is that, although they have already plenty of ways to do that, none
of them is natural and all of them have issues.
But indeed, the RFC text fails to discuss the various existing alternatives and their downsides.
—Claude