Skip to content

Normalize increment and decrement operator behaviour #547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Zend/tests/027.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ var_dump(${${$a}}('foo') == 'FOO');

$a = 'b';
$b = 'c';
$c = 'strtoupper';
${"1"} = 'strtoupper';
$strtoupper = 'strtolower';

var_dump(${${++$a}}('FOO') == 'foo');

?>
--EXPECT--
--EXPECTF--
bool(true)

Notice: String increment is deprecated, use str_inc() instead in %s on line 14
bool(true)
8 changes: 4 additions & 4 deletions Zend/tests/decrement_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ int(-1)
int(0)
float(1.5)
int(-1)
string(6) "string"
int(-1)
int(122)
float(1.5)
NULL
bool(true)
bool(false)
int(-1)
int(0)
int(-1)
object(stdClass)#%d (0) {
}
array(0) {
Expand Down
8 changes: 4 additions & 4 deletions Zend/tests/decrement_001_64bit.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ int(-1)
int(0)
float(1.5)
int(-1)
string(6) "string"
int(-1)
int(122)
float(1.5)
NULL
bool(true)
bool(false)
int(-1)
int(0)
int(-1)
object(stdClass)#%d (0) {
}
array(0) {
Expand Down
26 changes: 22 additions & 4 deletions Zend/zend_operators.c
Original file line number Diff line number Diff line change
Expand Up @@ -2027,7 +2027,7 @@ ZEND_API zend_bool instanceof_function(const zend_class_entry *instance_ce, cons
#define UPPER_CASE 2
#define NUMERIC 3

static void increment_string(zval *str) /* {{{ */
ZEND_API void increment_string(zval *str) /* {{{ */
{
int carry=0;
size_t pos=Z_STRLEN_P(str)-1;
Expand Down Expand Up @@ -2132,6 +2132,9 @@ ZEND_API int increment_function(zval *op1) /* {{{ */
case IS_NULL:
ZVAL_LONG(op1, 1);
break;
case IS_BOOL:
ZVAL_LONG(op1, Z_LVAL_P(op1) + 1);
break;
case IS_STRING: {
zend_long lval;
double dval;
Expand All @@ -2152,8 +2155,14 @@ ZEND_API int increment_function(zval *op1) /* {{{ */
ZVAL_DOUBLE(op1, dval+1);
break;
default:
/* Perl style string increment */
increment_string(op1);
if (Z_STRLEN_P(op1)) {
zend_error(E_NOTICE, "String increment is deprecated, use str_inc() instead");
/* Perl style string increment */
increment_string(op1);
} else {
str_efree(Z_STRVAL_P(op1));
ZVAL_LONG(op1, 1);
}
break;
}
}
Expand Down Expand Up @@ -2199,13 +2208,19 @@ ZEND_API int decrement_function(zval *op1) /* {{{ */
case IS_DOUBLE:
Z_DVAL_P(op1) = Z_DVAL_P(op1) - 1;
break;
case IS_BOOL:
ZVAL_LONG(op1, Z_LVAL_P(op1) - 1);
break;
case IS_NULL:
ZVAL_LONG(op1, -1);
break;
case IS_STRING: /* Like perl we only support string increment */
if (Z_STRLEN_P(op1) == 0) { /* consider as 0 */
zend_string_release(Z_STR_P(op1));
ZVAL_LONG(op1, -1);
break;
}
switch (is_numeric_string(Z_STRVAL_P(op1), Z_STRLEN_P(op1), &lval, &dval, 0)) {
switch (is_numeric_string(Z_STRVAL_P(op1), Z_STRLEN_P(op1), &lval, &dval, 1)) {
case IS_LONG:
zend_string_release(Z_STR_P(op1));
if (lval == ZEND_LONG_MIN) {
Expand All @@ -2219,6 +2234,9 @@ ZEND_API int decrement_function(zval *op1) /* {{{ */
zend_string_release(Z_STR_P(op1));
ZVAL_DOUBLE(op1, dval - 1);
break;
default:
str_efree(Z_STRVAL_P(op1));
ZVAL_LONG(op1, -1);
}
break;
case IS_OBJECT:
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_operators.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#define LONG_SIGN_MASK (((zend_long)1) << (8*sizeof(zend_long)-1))

BEGIN_EXTERN_C()
ZEND_API void increment_string(zval *str);
ZEND_API int add_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
ZEND_API int sub_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
ZEND_API int mul_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
Expand Down
5 changes: 3 additions & 2 deletions ext/reflection/tests/bug48336.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ class F extends E {
static protected $prop;
}

$class = 'A';
for($class = 'A'; $class <= 'F'; $class ++) {
$classes = ['A', 'B', 'C', 'D', 'E', 'F'];

foreach ($classes as $class) {
print($class.' => ');
try {
$rp = new ReflectionProperty($class, 'prop');
Expand Down
4 changes: 3 additions & 1 deletion ext/reflection/tests/traits004.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ class C1 { }
class C2 { use T1; }
class C3 { use T1; use T2; }

for ($c = "C1"; $c <= "C3"; $c++) {
$classes = ['C1', 'C2', 'C3'];

foreach ($classes as $c) {
echo "class $c:\n";
$r = new ReflectionClass($c);
var_dump($r->getTraitNames());
Expand Down
4 changes: 3 additions & 1 deletion ext/reflection/tests/traits005.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ class C2 { use T1; }
class C3 { use T1 { m1 as a1; } }
class C4 { use T1 { m1 as a1; m2 as a2; } }

for ($c = "C1"; $c <= "C4"; $c++) {
$classes = ['C1', 'C2', 'C3', 'C4'];

foreach ($classes as $c) {
echo "class $c:\n";
$r = new ReflectionClass($c);
var_dump($r->getTraitAliases());
Expand Down
6 changes: 6 additions & 0 deletions ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -2459,6 +2459,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_substr_compare, 0, 0, 3)
ZEND_ARG_INFO(0, length)
ZEND_ARG_INFO(0, case_sensitivity)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_str_inc_dec, 0)
ZEND_ARG_INFO(0, str)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ syslog.c */
#ifdef HAVE_SYSLOG_H
Expand Down Expand Up @@ -2733,6 +2737,8 @@ const zend_function_entry basic_functions[] = { /* {{{ */
PHP_FE(str_split, arginfo_str_split)
PHP_FE(strpbrk, arginfo_strpbrk)
PHP_FE(substr_compare, arginfo_substr_compare)
PHP_FE(str_inc, arginfo_str_inc_dec)
PHP_FE(str_dec, arginfo_str_inc_dec)

#ifdef HAVE_STRCOLL
PHP_FE(strcoll, arginfo_strcoll)
Expand Down
2 changes: 2 additions & 0 deletions ext/standard/php_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ PHP_FUNCTION(str_word_count);
PHP_FUNCTION(str_split);
PHP_FUNCTION(strpbrk);
PHP_FUNCTION(substr_compare);
PHP_FUNCTION(str_inc);
PHP_FUNCTION(str_dec);
#ifdef HAVE_STRCOLL
PHP_FUNCTION(strcoll);
#endif
Expand Down
114 changes: 114 additions & 0 deletions ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -5442,6 +5442,120 @@ PHP_FUNCTION(substr_compare)
}
/* }}} */

PHP_FUNCTION(str_inc)
{
char *str;
int str_len;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
RETURN_FALSE;
}

ZVAL_STRINGL(return_value, str, str_len, 1);
increment_string(return_value);
}

#define LOWER_CASE 1
#define UPPER_CASE 2
#define NUMERIC 3

static void decrement_string(zval *str) /* {{{ */
{
int carry=0;
int pos=Z_STRLEN_P(str)-1;
char *s=Z_STRVAL_P(str);
char *t;
int last=0; /* Shut up the compiler warning */
int ch;

if (Z_STRLEN_P(str) == 0) {
str_efree(Z_STRVAL_P(str));
Z_STRVAL_P(str) = estrndup("-1", sizeof("-1")-1);
Z_STRLEN_P(str) = 2;
return;
}

if (IS_INTERNED(s)) {
Z_STRVAL_P(str) = s = estrndup(s, Z_STRLEN_P(str));
}

while (pos >= 0) {
ch = s[pos];
if (ch >= 'a' && ch <= 'z') {
if (ch == 'a') {
s[pos] = 'z';
carry=1;
} else {
s[pos]--;
carry=0;
}
last=LOWER_CASE;
} else if (ch >= 'A' && ch <= 'Z') {
if (ch == 'A') {
s[pos] = 'Z';
carry=1;
} else {
s[pos]--;
carry=0;
}
last=UPPER_CASE;
} else if (ch >= '0' && ch <= '9') {
if (ch == '0') {
s[pos] = '9';
carry=1;
} else {
s[pos]--;
carry=0;
}
last = NUMERIC;
} else {
carry=0;
break;
}
if (carry == 0) {
break;
}
pos--;
}

if (carry) {
if (Z_STRLEN_P(str) > 1) {
t = (char *) emalloc(Z_STRLEN_P(str)-1+1);
memcpy(t, Z_STRVAL_P(str), Z_STRLEN_P(str) - 1);
Z_STRLEN_P(str)--;
t[Z_STRLEN_P(str)] = '\0';
switch (last) {
case NUMERIC:
t[Z_STRLEN_P(str) - 1] = '9';
break;
case UPPER_CASE:
t[0] = 'Z';
break;
case LOWER_CASE:
t[0] = 'z';
break;
}
str_efree(Z_STRVAL_P(str));
Z_STRVAL_P(str) = t;
} else {
s[0] = '0';
}
}
}

PHP_FUNCTION(str_dec)
{
char *str;
int str_len;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
RETURN_FALSE;
}

ZVAL_STRINGL(return_value, str, str_len, 1);
decrement_string(return_value);
}

/*
* Local variables:
* tab-width: 4
Expand Down
43 changes: 23 additions & 20 deletions ext/standard/tests/array/array_combine.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,28 @@ basic array_combine test
$array2 = array('1', '2', '3');
$array3 = array(0, 1, 2);
$array4 = array(TRUE, FALSE, NULL);
$a = array_combine($array1, $array1);
$b = array_combine($array1, $array2);
$c = array_combine($array1, $array3);
$d = array_combine($array1, $array4);
$e = array_combine($array2, $array1);
$f = array_combine($array2, $array2);
$g = array_combine($array2, $array3);
$h = array_combine($array2, $array4);
$i = array_combine($array3, $array1);
$j = array_combine($array3, $array2);
$k = array_combine($array3, $array3);
$l = array_combine($array3, $array4);
$m = array_combine($array4, $array1);
$n = array_combine($array4, $array2);
$o = array_combine($array4, $array3);
$p = array_combine($array4, $array4);
for($letter = "a"; $letter <= "p"; $letter++)
{
print_r($$letter);

$cases = array(
array_combine($array1, $array1),
array_combine($array1, $array2),
array_combine($array1, $array3),
array_combine($array1, $array4),
array_combine($array2, $array1),
array_combine($array2, $array2),
array_combine($array2, $array3),
array_combine($array2, $array4),
array_combine($array3, $array1),
array_combine($array3, $array2),
array_combine($array3, $array3),
array_combine($array3, $array4),
array_combine($array4, $array1),
array_combine($array4, $array2),
array_combine($array4, $array3),
array_combine($array4, $array4),
);

foreach ($cases as $case) {
print_r($case);
}
?>
--EXPECT--
Expand Down Expand Up @@ -119,4 +122,4 @@ Array
(
[1] => 1
[] =>
)
)
16 changes: 8 additions & 8 deletions tests/lang/operators/postdec_variationStr.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ float(0.2)
--- testing: '-7.7' ---
float(-8.7)
--- testing: 'abc' ---
string(3) "abc"
int(-1)
--- testing: '123abc' ---
string(6) "123abc"
int(122)
--- testing: '123e5' ---
float(12299999)
--- testing: '123e5xyz' ---
string(8) "123e5xyz"
float(12299999)
--- testing: ' 123abc' ---
string(7) " 123abc"
int(122)
--- testing: '123 abc' ---
string(7) "123 abc"
int(122)
--- testing: '123abc ' ---
string(7) "123abc "
int(122)
--- testing: '3.4a' ---
string(4) "3.4a"
float(2.4)
--- testing: 'a5.9' ---
string(4) "a5.9"
int(-1)
===DONE===
Loading