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: Vote
- 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
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) */ ?>
Patchs
Tests
Vote
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
rfc/strncmpnegativelen.1313236959.txt.gz · Last modified: 2017/09/22 13:28 (external edit)