On 24.06.21 13:17, Kamil Tekiela wrote:
I am against adding these functions, but for different reasons than Sara
and George.
If we add str_left and str_right then there should be a corresponding
variant in mbstring. The byte-string functions are rarely useful. Adding
these functions to mbstring unnecessarily complicates the extension for
little to no gain.
Another point is that if we decide to add them, then we will bikeshed
forever in an unresolvable manner about the name. Should it be called
str_left or strleft? Current functions don't have a naming convention, so
using either variant will be wrong.
Personally, I find substr to be more clear about the intent. I know that I
am asking for a part of the string. Whereas str_left doesn't convey an
action immediately. Without knowing its purpose I wouldn't know if it will
pad the string from the left, strip characters from left, or take the
leftmost part of the string.
Don't take it the wrong way, but I think it's a waste of time to implement
a function that doesn't even need a polyfill in the userland.

str_left / str_right also seems very unclear to me in terms of the name.
But I don't think naming new string functions in general should be
difficult - the recent string additions have all started with str_ (like
str_contains, str_starts_with) which seems more readable than any of the
non-underscore functions.

I do think the intended functions can make sense, just because I find
substr to be a bit obnoxious - with a necessary offset and an optional
length it rarely is obvious to me at a glance what the intention is, as
you can do so many different things with it, because offset and length
can be positive or negative, and then very different things happen.
These new functions are quite similar to str_contains or str_starts_with
for me, in that you don't need them, but it makes code so much more
readable.

str_left_part(string $string, int $length) would make it clear the left
part of the string is returned, and it could throw an exception if
$length is negative. With substr you might accidentally give it a
negative length, or would need to check if a length is positive or
negative (if it is in a variable) to know what is happening.

str_right_part(string $string, int $length) would make it clear the
right part of the string is returned. And if you compare substr usage to
these two functions, it might come through how different in arguments &
readability it is:

substr($string, -3);
str_right_part($string, 3);

substr($string, 0, 3);
str_left_part($string, 3);

substr($string, -6, 3);
str_left_part(str_right_part($string, 6), 3);

If you use substr all the time you might be used to it, but to me I have
to stop and think what is happening each time more than would be
necessary with dedicated functions. A function like str_contains
definitely had a bigger impact than something like str_left_part would
have, but it would still make a difference.

(By the way, str_left_part/str_right_part is just what came to mind,
could be anything as long as it conveys what it is doing clearly enough,
which probably always needs two words, not just one)

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to