From e1711b267212a4b11f2edcff24bd1765c398361e Mon Sep 17 00:00:00 2001 From: tjerk Date: Thu, 21 Feb 2013 14:37:00 +0800 Subject: [PATCH 1/3] added $use_keys argument to array_filter() --- ext/standard/array.c | 39 +++++++++-- .../tests/array/array_filter_error.phpt | 4 +- .../tests/array/array_filter_variation10.phpt | 70 +++++++++++++++++++ 3 files changed, 107 insertions(+), 6 deletions(-) create mode 100644 ext/standard/tests/array/array_filter_variation10.phpt diff --git a/ext/standard/array.c b/ext/standard/array.c index 40a27c053629..555f5db2d3f9 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -4170,9 +4170,11 @@ PHP_FUNCTION(array_filter) { zval *array; zval **operand; - zval **args[1]; + zval **args[2]; zval *retval = NULL; + zval *key = NULL; zend_bool have_callback = 0; + zend_bool use_key = 0; char *string_key; zend_fcall_info fci = empty_fcall_info; zend_fcall_info_cache fci_cache = empty_fcall_info_cache; @@ -4180,7 +4182,7 @@ PHP_FUNCTION(array_filter) ulong num_key; HashPosition pos; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|f", &array, &fci, &fci_cache) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|fb", &array, &fci, &fci_cache, &use_key) == FAILURE) { return; } @@ -4193,23 +4195,52 @@ PHP_FUNCTION(array_filter) have_callback = 1; fci.no_separation = 0; fci.retval_ptr_ptr = &retval; - fci.param_count = 1; + + if (use_key) { + fci.param_count = 2; + args[1] = &key; + } else { + fci.param_count = 1; + } } for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(array), &pos); zend_hash_get_current_data_ex(Z_ARRVAL_P(array), (void **)&operand, &pos) == SUCCESS; zend_hash_move_forward_ex(Z_ARRVAL_P(array), &pos) ) { + int key_type = zend_hash_get_current_key_ex(Z_ARRVAL_P(array), &string_key, &string_key_len, &num_key, 0, &pos); + if (have_callback) { + if (use_key) { + MAKE_STD_ZVAL(key); + /* Set up the key */ + switch (key_type) { + case HASH_KEY_IS_LONG: + Z_TYPE_P(key) = IS_LONG; + Z_LVAL_P(key) = num_key; + break; + + case HASH_KEY_IS_STRING: + ZVAL_STRINGL(key, string_key, string_key_len - 1, 1); + break; + } + } + args[0] = operand; fci.params = args; if (zend_call_function(&fci, &fci_cache TSRMLS_CC) == SUCCESS && retval) { if (!zend_is_true(retval)) { zval_ptr_dtor(&retval); + if (use_key) { + zval_ptr_dtor(&key); + } continue; } else { zval_ptr_dtor(&retval); + if (use_key) { + zval_ptr_dtor(&key); + } } } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "An error occurred while invoking the filter callback"); @@ -4220,7 +4251,7 @@ PHP_FUNCTION(array_filter) } zval_add_ref(operand); - switch (zend_hash_get_current_key_ex(Z_ARRVAL_P(array), &string_key, &string_key_len, &num_key, 0, &pos)) { + switch (key_type) { case HASH_KEY_IS_STRING: zend_hash_update(Z_ARRVAL_P(return_value), string_key, string_key_len, operand, sizeof(zval *), NULL); break; diff --git a/ext/standard/tests/array/array_filter_error.phpt b/ext/standard/tests/array/array_filter_error.phpt index 20e89aa4b776..3f8f51bc14a5 100644 --- a/ext/standard/tests/array/array_filter_error.phpt +++ b/ext/standard/tests/array/array_filter_error.phpt @@ -28,7 +28,7 @@ $extra_arg = 10; // with one more than the expected number of arguments echo "-- Testing array_filter() function with more than expected no. of arguments --"; -var_dump( array_filter($input, "odd", $extra_arg) ); +var_dump( array_filter($input, "odd", $extra_arg, $extra_arg) ); // with incorrect callback function echo "-- Testing array_filter() function with incorrect callback --"; @@ -42,7 +42,7 @@ echo "Done" Warning: array_filter() expects at least 1 parameter, 0 given in %s on line %d NULL -- Testing array_filter() function with more than expected no. of arguments -- -Warning: array_filter() expects at most 2 parameters, 3 given in %s on line %d +Warning: array_filter() expects at most 3 parameters, 4 given in %s on line %d NULL -- Testing array_filter() function with incorrect callback -- Warning: array_filter() expects parameter 2 to be a valid callback, function 'even' not found or invalid function name in %s on line %d diff --git a/ext/standard/tests/array/array_filter_variation10.phpt b/ext/standard/tests/array/array_filter_variation10.phpt new file mode 100644 index 000000000000..9a5b2a4a302f --- /dev/null +++ b/ext/standard/tests/array/array_filter_variation10.phpt @@ -0,0 +1,70 @@ +--TEST-- +Test array_filter() function : usage variations - using the array keys inside 'callback' +--FILE-- + 4; +} + +var_dump( array_filter($input, 'dump2', true) ); + +echo "*** Testing array_filter() : usage variations - 'callback' expecting second argument ***\n"; + +var_dump( array_filter($small, 'dump', false) ); + +echo "Done" +?> +--EXPECTF-- +*** Testing array_filter() : usage variations - using array keys in 'callback' *** +0 = 0 +1 = 1 +2 = -1 +3 = 10 +4 = 100 +5 = 1000 +6 = Hello +7 = +array(0) { +} +*** Testing array_filter() : usage variations - 'callback' filters based on key value *** +array(3) { + [5]=> + int(1000) + [6]=> + string(5) "Hello" + [7]=> + NULL +} +*** Testing array_filter() : usage variations - 'callback' expecting second argument *** + +Warning: Missing argument 2 for dump() in /home/tjerk/work/php/php-src/ext/standard/tests/array/array_filter_variation10.php on line %d + +Notice: Undefined variable: key in /home/tjerk/work/php/php-src/ext/standard/tests/array/array_filter_variation10.php on line %d + = 123 +array(0) { +} +Done From a549302b8585a7ddba1ba820024a5673a0347358 Mon Sep 17 00:00:00 2001 From: datibbaw Date: Tue, 17 Sep 2013 15:06:04 +0800 Subject: [PATCH 2/3] How fun! Revert "added $use_keys argument to array_filter()" This reverts commit e1711b267212a4b11f2edcff24bd1765c398361e. --- ext/standard/array.c | 39 ++--------- .../tests/array/array_filter_error.phpt | 4 +- .../tests/array/array_filter_variation10.phpt | 70 ------------------- 3 files changed, 6 insertions(+), 107 deletions(-) delete mode 100644 ext/standard/tests/array/array_filter_variation10.phpt diff --git a/ext/standard/array.c b/ext/standard/array.c index 1f98ef16f209..ae6e5d266fa2 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -4194,11 +4194,9 @@ PHP_FUNCTION(array_filter) { zval *array; zval **operand; - zval **args[2]; + zval **args[1]; zval *retval = NULL; - zval *key = NULL; zend_bool have_callback = 0; - zend_bool use_key = 0; char *string_key; zend_fcall_info fci = empty_fcall_info; zend_fcall_info_cache fci_cache = empty_fcall_info_cache; @@ -4206,7 +4204,7 @@ PHP_FUNCTION(array_filter) ulong num_key; HashPosition pos; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|fb", &array, &fci, &fci_cache, &use_key) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|f", &array, &fci, &fci_cache) == FAILURE) { return; } @@ -4219,52 +4217,23 @@ PHP_FUNCTION(array_filter) have_callback = 1; fci.no_separation = 0; fci.retval_ptr_ptr = &retval; - - if (use_key) { - fci.param_count = 2; - args[1] = &key; - } else { - fci.param_count = 1; - } + fci.param_count = 1; } for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(array), &pos); zend_hash_get_current_data_ex(Z_ARRVAL_P(array), (void **)&operand, &pos) == SUCCESS; zend_hash_move_forward_ex(Z_ARRVAL_P(array), &pos) ) { - int key_type = zend_hash_get_current_key_ex(Z_ARRVAL_P(array), &string_key, &string_key_len, &num_key, 0, &pos); - if (have_callback) { - if (use_key) { - MAKE_STD_ZVAL(key); - /* Set up the key */ - switch (key_type) { - case HASH_KEY_IS_LONG: - Z_TYPE_P(key) = IS_LONG; - Z_LVAL_P(key) = num_key; - break; - - case HASH_KEY_IS_STRING: - ZVAL_STRINGL(key, string_key, string_key_len - 1, 1); - break; - } - } - args[0] = operand; fci.params = args; if (zend_call_function(&fci, &fci_cache TSRMLS_CC) == SUCCESS && retval) { if (!zend_is_true(retval)) { zval_ptr_dtor(&retval); - if (use_key) { - zval_ptr_dtor(&key); - } continue; } else { zval_ptr_dtor(&retval); - if (use_key) { - zval_ptr_dtor(&key); - } } } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "An error occurred while invoking the filter callback"); @@ -4275,7 +4244,7 @@ PHP_FUNCTION(array_filter) } zval_add_ref(operand); - switch (key_type) { + switch (zend_hash_get_current_key_ex(Z_ARRVAL_P(array), &string_key, &string_key_len, &num_key, 0, &pos)) { case HASH_KEY_IS_STRING: zend_hash_update(Z_ARRVAL_P(return_value), string_key, string_key_len, operand, sizeof(zval *), NULL); break; diff --git a/ext/standard/tests/array/array_filter_error.phpt b/ext/standard/tests/array/array_filter_error.phpt index 3f8f51bc14a5..20e89aa4b776 100644 --- a/ext/standard/tests/array/array_filter_error.phpt +++ b/ext/standard/tests/array/array_filter_error.phpt @@ -28,7 +28,7 @@ $extra_arg = 10; // with one more than the expected number of arguments echo "-- Testing array_filter() function with more than expected no. of arguments --"; -var_dump( array_filter($input, "odd", $extra_arg, $extra_arg) ); +var_dump( array_filter($input, "odd", $extra_arg) ); // with incorrect callback function echo "-- Testing array_filter() function with incorrect callback --"; @@ -42,7 +42,7 @@ echo "Done" Warning: array_filter() expects at least 1 parameter, 0 given in %s on line %d NULL -- Testing array_filter() function with more than expected no. of arguments -- -Warning: array_filter() expects at most 3 parameters, 4 given in %s on line %d +Warning: array_filter() expects at most 2 parameters, 3 given in %s on line %d NULL -- Testing array_filter() function with incorrect callback -- Warning: array_filter() expects parameter 2 to be a valid callback, function 'even' not found or invalid function name in %s on line %d diff --git a/ext/standard/tests/array/array_filter_variation10.phpt b/ext/standard/tests/array/array_filter_variation10.phpt deleted file mode 100644 index 9a5b2a4a302f..000000000000 --- a/ext/standard/tests/array/array_filter_variation10.phpt +++ /dev/null @@ -1,70 +0,0 @@ ---TEST-- -Test array_filter() function : usage variations - using the array keys inside 'callback' ---FILE-- - 4; -} - -var_dump( array_filter($input, 'dump2', true) ); - -echo "*** Testing array_filter() : usage variations - 'callback' expecting second argument ***\n"; - -var_dump( array_filter($small, 'dump', false) ); - -echo "Done" -?> ---EXPECTF-- -*** Testing array_filter() : usage variations - using array keys in 'callback' *** -0 = 0 -1 = 1 -2 = -1 -3 = 10 -4 = 100 -5 = 1000 -6 = Hello -7 = -array(0) { -} -*** Testing array_filter() : usage variations - 'callback' filters based on key value *** -array(3) { - [5]=> - int(1000) - [6]=> - string(5) "Hello" - [7]=> - NULL -} -*** Testing array_filter() : usage variations - 'callback' expecting second argument *** - -Warning: Missing argument 2 for dump() in /home/tjerk/work/php/php-src/ext/standard/tests/array/array_filter_variation10.php on line %d - -Notice: Undefined variable: key in /home/tjerk/work/php/php-src/ext/standard/tests/array/array_filter_variation10.php on line %d - = 123 -array(0) { -} -Done From 981d68384ec330933aea997674ed25983a51b2f2 Mon Sep 17 00:00:00 2001 From: datibbaw Date: Tue, 17 Sep 2013 15:08:03 +0800 Subject: [PATCH 3/3] added option for empty objects when encoding empty arrays --- ext/json/json.c | 9 +++++++-- ext/json/php_json.h | 1 + ext/json/tests/json_encode_empty_object.phpt | 12 ++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 ext/json/tests/json_encode_empty_object.phpt diff --git a/ext/json/json.c b/ext/json/json.c index 782375e3717c..751f491b44fd 100644 --- a/ext/json/json.c +++ b/ext/json/json.c @@ -98,6 +98,7 @@ static PHP_MINIT_FUNCTION(json) REGISTER_LONG_CONSTANT("JSON_HEX_APOS", PHP_JSON_HEX_APOS, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("JSON_HEX_QUOT", PHP_JSON_HEX_QUOT, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("JSON_FORCE_OBJECT", PHP_JSON_FORCE_OBJECT, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("JSON_FORCE_EMPTY_OBJECT", PHP_JSON_FORCE_EMPTY_OBJECT, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("JSON_NUMERIC_CHECK", PHP_JSON_NUMERIC_CHECK, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("JSON_UNESCAPED_SLASHES", PHP_JSON_UNESCAPED_SLASHES, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("JSON_PRETTY_PRINT", PHP_JSON_PRETTY_PRINT, CONST_CS | CONST_PERSISTENT); @@ -247,6 +248,12 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC) return; } + i = myht ? zend_hash_num_elements(myht) : 0; + + if (i == 0 && (options & PHP_JSON_FORCE_EMPTY_OBJECT)) { + r = PHP_JSON_OUTPUT_OBJECT; + } + if (r == PHP_JSON_OUTPUT_ARRAY) { smart_str_appendc(buf, '['); } else { @@ -256,8 +263,6 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC) json_pretty_print_char(buf, options, '\n' TSRMLS_CC); ++JSON_G(encoder_depth); - i = myht ? zend_hash_num_elements(myht) : 0; - if (i > 0) { char *key; diff --git a/ext/json/php_json.h b/ext/json/php_json.h index ec707ce3465a..34c9cf4cb484 100644 --- a/ext/json/php_json.h +++ b/ext/json/php_json.h @@ -65,6 +65,7 @@ extern zend_class_entry *php_json_serializable_ce; #define PHP_JSON_PRETTY_PRINT (1<<7) #define PHP_JSON_UNESCAPED_UNICODE (1<<8) #define PHP_JSON_PARTIAL_OUTPUT_ON_ERROR (1<<9) +#define PHP_JSON_FORCE_EMPTY_OBJECT (1<<10) /* Internal flags */ #define PHP_JSON_OUTPUT_ARRAY 0 diff --git a/ext/json/tests/json_encode_empty_object.phpt b/ext/json/tests/json_encode_empty_object.phpt new file mode 100644 index 000000000000..dfd00b250309 --- /dev/null +++ b/ext/json/tests/json_encode_empty_object.phpt @@ -0,0 +1,12 @@ +--TEST-- +json_encode() with JSON_FORCE_EMPTY_OBJECT +--SKIPIF-- + +--FILE-- + +--EXPECT-- +string(2) "{}" +string(7) "[1,2,3]"