Req #38437 [Opn->Csd]: substr() - slightly change its contract

From: Date: Fri, 15 Jan 2016 17:56:56 +0000
Subject: Req #38437 [Opn->Csd]: substr() - slightly change its contract
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-198691@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=38437&edit=1

 ID:                 38437
 Updated by:         nikic@php.net
 Reported by:        zizka at seznam dot cz
 Summary:            substr() - slightly change its contract
-Status:             Open
+Status:             Closed
 Type:               Feature/Change Request
-Package:            Feature/Change Request
+Package:            *General Issues
 PHP Version:        5.1.4
-Assigned To:        
+Assigned To:        nikic
 Block user comment: N
 Private report:     N

 New Comment:

This has been implemented in PHP 7.0.


Previous Comments:
------------------------------------------------------------------------
[2010-06-22 04:10:19] salsi at icosaedro dot it

The manual states that "If length is given and is 0, FALSE or NULL an empty string will be
returned.". This is not true, as substring($s, $start, $length) seems to behave in a very
unpredictable way with NULL string, empty string and at the string boundaries when $length==0:

var_dump( substr("a", 0, 0) ); # => "", ok
var_dump( substr("a", 1, 0) ); # => FALSE rather than ""

var_dump( substr("", 0, 0) );  # => FALSE rather than ""

var_dump( substr(NULL, 0, 0) ); # => FALSE rather than ""

In my opinion, substr() should always return a string, possibly empty, of length $length bytes
provided that 0 <= $start and $start + $length <= strlen($s). And then an empty string should
be returned when $length==0.

If $start is negative, the value $start = strlen($s) - $start should be considered and the algorithm
above applied.

The NULL value should be considered as the empty string "" as in PHP tradition.

------------------------------------------------------------------------
[2006-08-12 20:34:14] zizka at seznam dot cz

Description:
------------
string substr ( string string, int start [, int length] )

Currently:
If string is less than *or equal* to start characters long, FALSE will be returned.

I suggest:
If string is less than start characters long, FALSE will be returned.

The latter is more "ideologically clean". See the behavior of analogical methods in Java,
JavaScript, C, etc. E.g. I did some simple parser, where the string of the form $<anything> is
expected. I wanted to get <anything>, so I did:

if($s !== '' && $s[0] == '$')
  $s2 = substr($s, 1);

Then, behaved by Java's String.substring(), I wrote:

if($s2 === '')
  // Replace $s2 with some default value;

After several minutes of searching for the bug, I noticed the fact mentioned above.

Reproduce code:
---------------
substr("Ahoj", 4);

Expected result:
----------------
An empty string.

Actual result:
--------------
FALSE.


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=38437&edit=1


Thread (3 messages)

« previous php.bugs (#198691) next »