diff options
-rw-r--r-- | ext/standard/basic_functions.c | 2 | ||||
-rw-r--r-- | ext/standard/formatted_print.c | 69 | ||||
-rw-r--r-- | ext/standard/php_string.h | 2 |
3 files changed, 62 insertions, 11 deletions
diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 5bd25d7171..6093f530b7 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -208,6 +208,8 @@ function_entry basic_functions[] = { PHP_FALIAS(strchr, strstr, NULL) PHP_NAMED_FE(sprintf, PHP_FN(user_sprintf), NULL) PHP_NAMED_FE(printf, PHP_FN(user_printf), NULL) + PHP_FE(vprintf, NULL) + PHP_FE(vsprintf, NULL) PHP_FE(sscanf, third_and_rest_force_ref) PHP_FE(fscanf, third_and_rest_force_ref) PHP_FE(parse_url, NULL) diff --git a/ext/standard/formatted_print.c b/ext/standard/formatted_print.c index 0a4d6d3ee1..8a175989b2 100644 --- a/ext/standard/formatted_print.c +++ b/ext/standard/formatted_print.c @@ -408,23 +408,40 @@ php_sprintf_getnumber(char *buffer, int *pos) * */ static char * -php_formatted_print(int ht, int *len TSRMLS_DC) +php_formatted_print(int ht, int *len, int use_array TSRMLS_DC) { - pval ***args; + zval ***args, **z_format, **array; int argc, size = 240, inpos = 0, outpos = 0, temppos; int alignment, width, precision, currarg, adjusting, argnum; char *format, *result, padding; argc = ZEND_NUM_ARGS(); - if (argc < 1) { - WRONG_PARAM_COUNT_WITH_RETVAL(NULL); - } - args = (pval ***)emalloc(argc * sizeof(pval *)); + if (use_array) { + int i = 1; + + if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(argc, &z_format, &array) == FAILURE) { + WRONG_PARAM_COUNT_WITH_RETVAL(NULL); + } + SEPARATE_ZVAL(array); + convert_to_array_ex(array); + argc = 1 + zend_hash_num_elements(Z_ARRVAL_PP(array)); + args = (zval ***)emalloc(argc * sizeof(zval *)); + args[0] = z_format; + for (zend_hash_internal_pointer_reset(Z_ARRVAL_PP(array)); + zend_hash_get_current_data(Z_ARRVAL_PP(array), (void **)&args[i++]) == SUCCESS; + zend_hash_move_forward(Z_ARRVAL_PP(array))); + } else { + if (argc < 1) { + WRONG_PARAM_COUNT_WITH_RETVAL(NULL); + } + + args = (zval ***)emalloc(argc * sizeof(zval *)); - if (zend_get_parameters_array_ex(argc, args) == FAILURE) { - efree(args); - WRONG_PARAM_COUNT_WITH_RETVAL(NULL); + if (zend_get_parameters_array_ex(argc, args) == FAILURE) { + efree(args); + WRONG_PARAM_COUNT_WITH_RETVAL(NULL); + } } convert_to_string_ex(args[0]); format = (*args[0])->value.str.val; @@ -632,7 +649,22 @@ PHP_FUNCTION(user_sprintf) char *result; int len; - if ((result=php_formatted_print(ht, &len TSRMLS_CC))==NULL) { + if ((result=php_formatted_print(ht, &len, 0 TSRMLS_CC))==NULL) { + RETURN_FALSE; + } + RETVAL_STRINGL(result,len,1); + efree(result); +} +/* }}} */ + +/* {{{ proto string vsprintf(string format, array args) + Return a formatted string */ +PHP_FUNCTION(vsprintf) +{ + char *result; + int len; + + if ((result=php_formatted_print(ht, &len, 1 TSRMLS_CC))==NULL) { RETURN_FALSE; } RETVAL_STRINGL(result,len,1); @@ -647,7 +679,22 @@ PHP_FUNCTION(user_printf) char *result; int len; - if ((result=php_formatted_print(ht, &len TSRMLS_CC))==NULL) { + if ((result=php_formatted_print(ht, &len, 0 TSRMLS_CC))==NULL) { + RETURN_FALSE; + } + PHPWRITE(result,len); + efree(result); +} +/* }}} */ + +/* {{{ proto int printf(string format, array args) + Output a formatted string */ +PHP_FUNCTION(vprintf) +{ + char *result; + int len; + + if ((result=php_formatted_print(ht, &len, 1 TSRMLS_CC))==NULL) { RETURN_FALSE; } PHPWRITE(result,len); diff --git a/ext/standard/php_string.h b/ext/standard/php_string.h index 40d0ce1312..ef3c5fecfe 100644 --- a/ext/standard/php_string.h +++ b/ext/standard/php_string.h @@ -57,6 +57,8 @@ PHP_FUNCTION(hebrev); PHP_FUNCTION(hebrevc); PHP_FUNCTION(user_sprintf); PHP_FUNCTION(user_printf); +PHP_FUNCTION(vprintf); +PHP_FUNCTION(vsprintf); PHP_FUNCTION(addcslashes); PHP_FUNCTION(addslashes); PHP_FUNCTION(stripcslashes); |