strn(case)cmp supporting a negative length as its third paramter View as Markdown

Withdrawn
Authors
Xinchen Hui laruence@php.net
Date
2011-07-28
Version
1.0
Source
wiki.php.net/rfc/strncmpnegativelen

Vote

Do you want this feature in PHP 5.4 closed
yes
11
no
5
16 voters
arpad yes 2012-02-23 06:34
cataphract yes 2011-11-23 15:33
derick no 2011-08-14 11:16
felipe yes 2011-08-06 15:07
gwynne yes 2011-08-11 02:54
iliaa no 2011-08-08 16:08
klaussilveira yes 2011-12-13 11:17
laruence yes 2011-08-13 10:48
levim no 2011-12-19 10:16
pajoye yes 2011-08-05 21:24
pierrick yes 2011-08-06 16:31
rasmus no 2011-08-05 21:41
rdohms yes 2011-08-15 15:39
salathe no 2011-08-13 11:57
stas yes 2012-02-26 08:31
tyrael yes 2011-08-03 15:32

Discussions on the mailing list

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:53 UTC

Introduction

strncmp doesn't supporting negative length:

<?php
   if (strncmp("prefix_num", "num", -3) === 0) {
        echo "they have same suffix\n";
   }
?> 

running the above script in PHP 5.3.6 will result:

warning: Length must be greater than or equal to 0 in /tmp/1.php 

We need to write some codes like following one to make it works as expect:

<?php
   if (strncmp(substr("prefix_num", -3, 3), "num", 3) === 0) {
        echo "they have same suffix\n";
   }
?> 

Proposal

base on feature request: https://bugs.php.net/bug.php?id=36944 , I wrote a patch to make strn(case)cmp supporting negative length,

after patched, following script:

<?php
   if (strncmp("prefix_num", "num", -3) === 0) {
        echo "they have same suffix\n";
   }
?> 

will work as expect.

and if the abs of the negative length is greater than any strlen of the first two parameters, then strn(case)cmp will work as strncmp in the reverse order with the abs value:

<?php
var_dump(strncmp("prefix_num", "num", -10));
var_dump(strncmp("mun_xiferp", "mun", 10));
/** output:
int(7)
int(7)
*/
?> 

BC Breaks

  • Any strncmp call with a computed length which may end up being negative will now potentially return a match where it wouldn't before.

Patches

Tests

Vote

Vote: Do you want this feature in PHP 5.4 — yes: 11 · no: 5 (closed)

Changelog

  • 2011-07-28 Xinchen Hui: Initial RFC creation
  • 2011-07-28 Xinchen Hui: Updated patch to avoid making binary API changed
  • 2011-08-01 Xinchen Hui: Start voting
  • 2011-08-08 Xinchen Hui: Edited phpt
  • 2011-08-13 Xinchen Hui: Stop voting
  • 2011-08-14 Xinchen Hui: Open voting again, since there comes some new objections
  • 2011-12-13 Xinchen Hui: Won't apply now