rfc:strncmpnegativelen
This is an old revision of the document!
RFC: strn(case)cmp supporting a negative length as its third paramter
- Version: 1.0
- Date: 2011-07-28
- Author: Xinchen Hui laruence@php.net
- Status: Under Discussion
- First Published at: http://wiki.php.net/rfc/strncmpnegativelen/
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: (external edit)