I think when the values are positive everything is mostly great. I think
when the values are negative is where the main problems are. Both the C
function strncpy() and the C++ strings substr() function only support
positive values for length AFAIK.
I just think it is very unintuitive for the first parameter always to be a
position, and the 2nd parameter to be a length if the value is positive, and
a position if the value is negative.
substr('string', 1, 2); // Goes from position 1 to position 3
substr('string', -2, -1); // Goes from position -2 to position -1
So here is the same kind of thing in python, which uses [start, end):
string[1:2] ==> 't'
string[-2:-1] ==> 'n'
And ruby, which uses [start, end]:
"string"[2..3] ==> 'tr'
"string"[-2:-1] ==> 'ng'
Both of these languages use positions for positive and negative values. In
addition, in both of these languages if you slice a string impossibly, both
of them return an empy string as opposed to false, which just seems more
intuitive to me.
I don't think this function is particularly novel, I just think both
returning an empty string on impossible slicing and slicing based on
positions are improvements, and combined I think this function is noticeably
more durable and readable than substr().
-Dan
On Tue, Mar 29, 2011 at 11:22 PM, Lars Schultz <[email protected]>wrote:
> I just love substr() and I think all other languages got it wrong;)
>
> Seriously...it behaves the same as implementations in other languages as
> long as values are positive, right? how is that counter-intuitive? How do
> other languages handle negative values?
>
> Am 30.03.2011 08:06, schrieb Dan Birken:
>
> My apologizes if I am bringing up a topic that has been discussed before,
>> this is my first time wading into the PHP developers lists and I couldn't
>> find anything particularly relevant with the search.
>>
>> Here is a bug I submitted over the weekend (
>> http://bugs.php.net/bug.php?id=54387) with an attached patch that adds a
>> str_slice() function into PHP. This function is just a very simple string
>> slicing function, with the logical interface of str_slice(string, start,
>> [end]). It is of course meant to replace substr() as an interface for
>> string slicing.
>>
>> I detailed the reasons I submitted the patch in the bug a little bit, but
>> the main reason is that I think the substr() function is really overly
>> confusing and just not an intuitive method of string slicing, which is
>> exceedingly common functionality. I realize we don't want to go around
>> adding lots of random little functions into the language that don't offer
>> much, but the problem with that is that if we have a function like
>> substr()
>> with an unusual and unintuitive interface, it becomes unchangeable due to
>> legacy issues and then you can never improve. I think this particular
>> functionality is important enough to offer an updated interface. In the
>> bug
>> I also pointed to two related bugs that would be essentially fixed with
>> this
>> patch.
>>
>> -Dan
>>
>>
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>