rfc:strncmpnegativelen

This is an old revision of the document!


RFC: strn(case)cmp supporting a negative length as its third paramter

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 

but we know substr supports a negative length as its parameter:

<?php
   echo substr("test", 0, -1);

Proposal

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, strn(case)cmp will works as similar as the same situation with positive length:

<?php
  var_dump(strncmp("prefix_num", "num", -10));
  //outpu: int(7)
?> 

Patchs

Tests

Changelog

  • 2011-07-28 Xinchen Hui: Initial RFC creation
rfc/strncmpnegativelen.1311829755.txt.gz · Last modified: 2017/09/22 13:28 (external edit)