On 27/02/15 21:14, Damien Tournoud wrote:
> Hi Lester,
> 
> On Fri, Feb 27, 2015 at 9:53 PM, Lester Caine <les...@lsces.co.uk
> <mailto:les...@lsces.co.uk>> wrote:
> 
>     > Please, read the examples again, the current behavior is nothing but
>     > inconsistent:
>     >
>     >   substr("a", 1) => FALSE
>     >   substr("a", -300) => ""
> 
>     ? That was the case prior to PHP5.2.1
>     The fixes in 5.2.2 were not commonly accepted but give false for both,
>     but 5.2.7 and later give false and "a" which is what was the preferred
>     result at the time. Unless we are seeing something different, I'm only
>     seeing "a" or false for a current output of all your examples.
> 
> I meant "a", but you are right, it's a bit less inconsistent than I
> thought. The current behavior could be defined as "if the resulting
> slice is so that (start index <= end index) and (either start index or
> end index is in bound) return the slice, else return FALSE", which is
> not very useful but not so bad anyway.
> 
> The only real annoyance is that the check is strict on the right bound,
> so that:
> 
>     substr("abcd", 5) => FALSE
> 
> while:
> 
>     substr("abcd", -10, -4) => ""

This is the where the -ve logic stuff was changed. If you try -5 rather
than -4 things swap around versions wise. The 'problem' is where the
actual buffer length goes to 0 and then -ve. There still seem to be a
few edge cases that are not correctly caught, but then currently "" and
false default to false anyway so there is ONLY a problem if one DOES
remove the weak casting.

> That explains one of the E_DEPRECATED triggered by Drupal 7, because
> when you want to remove a prefix from a string, you often do:
> 
>     if (substr($str, 0, strlen($prefix)) {
>        $str = substr($str, strlen($prefix))
>     }
> 
> But this currently returns FALSE when $str == $prefix.

> (Obviously, the most useful behavior would be to return a string in all
> cases, like for example, Python.)

If you only want strings then OK, but in that case how would you rework
the drupal problem anyway? Although I'd not start from that if statement
anyway if there is a chance that the $str and $prefix ARE the same
string? The current if statement is what is wrong not the second substr.
if ( strlen($str) > strlen($prefix) ) {
  $str = substr($str, strlen($prefix))
} else { // nothing left }
But how do you know that the string contains the prefix? Yes the legacy
code base needs tidying up and is full of bugs, but we often just need
to tidy up the logic ...

-- 
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk

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

Reply via email to