* - 0 or more: ArgumentCountError is thrown
*/
static zend_string *
-php_formatted_print(zval *z_format, zval *args, int argc, int nb_additional_parameters)
+php_formatted_print(char *format, size_t format_len, zval *args, int argc, int nb_additional_parameters)
{
size_t size = 240, outpos = 0;
int alignment, currarg, adjusting, argnum, width, precision;
- char *format, *temppos, padding;
+ char *temppos, padding;
zend_string *result;
int always_sign;
- size_t format_len;
int bad_arg_number = 0;
- if (!try_convert_to_string(z_format)) {
- return NULL;
- }
-
- format = Z_STRVAL_P(z_format);
- format_len = Z_STRLEN_P(z_format);
result = zend_string_alloc(size, 0);
currarg = 0;
PHP_FUNCTION(user_sprintf)
{
zend_string *result;
- zval *format, *args;
+ char *format;
+ size_t format_len;
+ zval *args;
int argc;
ZEND_PARSE_PARAMETERS_START(1, -1)
- Z_PARAM_ZVAL(format)
+ Z_PARAM_STRING(format, format_len)
Z_PARAM_VARIADIC('*', args, argc)
ZEND_PARSE_PARAMETERS_END();
- result = php_formatted_print(format, args, argc, 1);
+ result = php_formatted_print(format, format_len, args, argc, 1);
if (result == NULL) {
return;
}
PHP_FUNCTION(vsprintf)
{
zend_string *result;
- zval *format, *array, *args;
+ char *format;
+ size_t format_len;
+ zval *array, *args;
int argc;
ZEND_PARSE_PARAMETERS_START(2, 2)
- Z_PARAM_ZVAL(format)
+ Z_PARAM_STRING(format, format_len)
Z_PARAM_ZVAL(array)
ZEND_PARSE_PARAMETERS_END();
args = php_formatted_print_get_array(array, &argc);
- result = php_formatted_print(format, args, argc, -1);
+ result = php_formatted_print(format, format_len, args, argc, -1);
efree(args);
if (result == NULL) {
return;
{
zend_string *result;
size_t rlen;
- zval *format, *args;
+ char *format;
+ size_t format_len;
+ zval *args;
int argc;
ZEND_PARSE_PARAMETERS_START(1, -1)
- Z_PARAM_ZVAL(format)
+ Z_PARAM_STRING(format, format_len)
Z_PARAM_VARIADIC('*', args, argc)
ZEND_PARSE_PARAMETERS_END();
- result = php_formatted_print(format, args, argc, 1);
+ result = php_formatted_print(format, format_len, args, argc, 1);
if (result == NULL) {
return;
}
{
zend_string *result;
size_t rlen;
- zval *format, *array, *args;
+ char *format;
+ size_t format_len;
+ zval *array, *args;
int argc;
ZEND_PARSE_PARAMETERS_START(2, 2)
- Z_PARAM_ZVAL(format)
+ Z_PARAM_STRING(format, format_len)
Z_PARAM_ZVAL(array)
ZEND_PARSE_PARAMETERS_END();
args = php_formatted_print_get_array(array, &argc);
- result = php_formatted_print(format, args, argc, -1);
+ result = php_formatted_print(format, format_len, args, argc, -1);
efree(args);
if (result == NULL) {
return;
PHP_FUNCTION(fprintf)
{
php_stream *stream;
- zval *arg1, *format, *args;
+ char *format;
+ size_t format_len;
+ zval *arg1, *args;
int argc;
zend_string *result;
ZEND_PARSE_PARAMETERS_START(2, -1)
Z_PARAM_RESOURCE(arg1)
- Z_PARAM_ZVAL(format)
+ Z_PARAM_STRING(format, format_len)
Z_PARAM_VARIADIC('*', args, argc)
ZEND_PARSE_PARAMETERS_END();
php_stream_from_zval(stream, arg1);
- result = php_formatted_print(format, args, argc, 2);
+ result = php_formatted_print(format, format_len, args, argc, 2);
if (result == NULL) {
return;
}
PHP_FUNCTION(vfprintf)
{
php_stream *stream;
- zval *arg1, *format, *array, *args;
+ char *format;
+ size_t format_len;
+ zval *arg1, *array, *args;
int argc;
zend_string *result;
ZEND_PARSE_PARAMETERS_START(3, 3)
Z_PARAM_RESOURCE(arg1)
- Z_PARAM_ZVAL(format)
+ Z_PARAM_STRING(format, format_len)
Z_PARAM_ZVAL(array)
ZEND_PARSE_PARAMETERS_END();
args = php_formatted_print_get_array(array, &argc);
- result = php_formatted_print(format, args, argc, -1);
+ result = php_formatted_print(format, format_len, args, argc, -1);
efree(args);
if (result == NULL) {
return;
// writing to the file
foreach($resource_types as $value) {
- @fprintf($file_handle, $value);
+ @fprintf($file_handle, "%s", $value);
@fprintf($file_handle, "\n");
}
// closing the file
// writing to the file
foreach($array_types as $value) {
- @fprintf($file_handle, $value);
+ @fprintf($file_handle, "%s", $value);
@fprintf($file_handle, "\n");
}
// closing the file
// writing to the file
foreach($resource_types as $value) {
- @fprintf($file_handle, $value);
+ @fprintf($file_handle, "%s", $value);
@fprintf($file_handle, "\n");
}
// closing the file
// writing to the file
foreach($array_types as $value) {
- @fprintf($file_handle, $value);
+ @fprintf($file_handle, "%s", $value);
@fprintf($file_handle, "\n");
}
// closing the file
// writing to the file
foreach($resource_types as $value) {
- @fprintf($file_handle, $value);
+ @fprintf($file_handle, "%s", $value);
@fprintf($file_handle, "\n");
}
// closing the file
// writing to the file
foreach($array_types as $value) {
- @fprintf($file_handle, $value);
+ @fprintf($file_handle, "%s", $value);
@fprintf($file_handle, "\n");
}
// closing the file
// writing to the file
foreach($resource_types as $value) {
- @fprintf($file_handle, $value);
+ @fprintf($file_handle, "%s", $value);
@fprintf($file_handle, "\n");
}
// closing the file
// writing to the file
foreach($array_types as $value) {
- @fprintf($file_handle, $value);
+ @fprintf($file_handle, "%s", $value);
@fprintf($file_handle, "\n");
}
// closing the file
// writing to the file
foreach($resource_types as $value) {
- @fprintf($file_handle, $value);
+ @fprintf($file_handle, "%s", $value);
@fprintf($file_handle, "\n");
}
// closing the file
// writing to the file
foreach($array_types as $value) {
- @fprintf($file_handle, $value);
+ @fprintf($file_handle, "%s", $value);
@fprintf($file_handle, "\n");
}
// closing the file
// writing to the file
foreach($resource_types as $value) {
- @fprintf($file_handle, $value);
+ @fprintf($file_handle, "%s", $value);
@fprintf($file_handle, "\n");
}
// closing the file
// writing to the file
foreach($resource_types as $value) {
- @fprintf($file_handle, $value);
+ @fprintf($file_handle, "%s", $value);
@fprintf($file_handle, "\n");
}
// closing the file
// writing to the file
foreach($array_types as $value) {
- @fprintf($file_handle, $value);
+ @fprintf($file_handle, "%s", $value);
@fprintf($file_handle, "\n");
}
// closing the file
// writing to the file
foreach($resource_types as $value) {
- @fprintf($file_handle, $value);
+ @fprintf($file_handle, "%s", $value);
@fprintf($file_handle, "\n");
}
// closing the file
// writing to the file
foreach($array_types as $value) {
- @fprintf($file_handle, $value);
+ @fprintf($file_handle, "%s", $value);
@fprintf($file_handle, "\n");
}
// closing the file
// writing to the file
foreach($array_types as $value) {
- @fprintf($file_handle, $value);
+ @fprintf($file_handle, "%s", $value);
@fprintf($file_handle, "\n");
}
// closing the file
echo "\n-- Iteration $count --\n";
// with default argument
- $result = printf($value);
- echo "\n";
- var_dump($result);
+ try {
+ $result = printf($value);
+ echo "\n";
+ var_dump($result);
+ } catch (TypeError $exception) {
+ echo $exception->getMessage() . "\n";
+ }
// with two arguments
- $result = printf($value, $arg1);
- echo "\n";
- var_dump($result);
+ try {
+ $result = printf($value, $arg1);
+ echo "\n";
+ var_dump($result);
+ } catch (TypeError $exception) {
+ echo $exception->getMessage() . "\n";
+ }
// with three arguments
- $result = printf($value, $arg1, $arg2);
- echo "\n";
- var_dump($result);
+ try {
+ $result = printf($value, $arg1, $arg2);
+ echo "\n";
+ var_dump($result);
+ } catch (TypeError $exception) {
+ echo $exception->getMessage() . "\n";
+ }
$count++;
};
int(3)
-- Iteration 10 --
-
-Warning: Array to string conversion in %s on line %d
-Array
-int(5)
-
-Warning: Array to string conversion in %s on line %d
-Array
-int(5)
-
-Warning: Array to string conversion in %s on line %d
-Array
-int(5)
+printf() expects parameter 1 to be string, array given
+printf() expects parameter 1 to be string, array given
+printf() expects parameter 1 to be string, array given
-- Iteration 11 --
-
-Warning: Array to string conversion in %s on line %d
-Array
-int(5)
-
-Warning: Array to string conversion in %s on line %d
-Array
-int(5)
-
-Warning: Array to string conversion in %s on line %d
-Array
-int(5)
+printf() expects parameter 1 to be string, array given
+printf() expects parameter 1 to be string, array given
+printf() expects parameter 1 to be string, array given
-- Iteration 12 --
-
-Warning: Array to string conversion in %s on line %d
-Array
-int(5)
-
-Warning: Array to string conversion in %s on line %d
-Array
-int(5)
-
-Warning: Array to string conversion in %s on line %d
-Array
-int(5)
+printf() expects parameter 1 to be string, array given
+printf() expects parameter 1 to be string, array given
+printf() expects parameter 1 to be string, array given
-- Iteration 13 --
-
-Warning: Array to string conversion in %s on line %d
-Array
-int(5)
-
-Warning: Array to string conversion in %s on line %d
-Array
-int(5)
-
-Warning: Array to string conversion in %s on line %d
-Array
-int(5)
+printf() expects parameter 1 to be string, array given
+printf() expects parameter 1 to be string, array given
+printf() expects parameter 1 to be string, array given
-- Iteration 14 --
-
-Warning: Array to string conversion in %s on line %d
-Array
-int(5)
-
-Warning: Array to string conversion in %s on line %d
-Array
-int(5)
-
-Warning: Array to string conversion in %s on line %d
-Array
-int(5)
+printf() expects parameter 1 to be string, array given
+printf() expects parameter 1 to be string, array given
+printf() expects parameter 1 to be string, array given
-- Iteration 15 --
int(0)
-- Iteration 26 --
-Resource id #%d
-int(%d)
-Resource id #%d
-int(%d)
-Resource id #%d
-int(%d)
+printf() expects parameter 1 to be string, resource given
+printf() expects parameter 1 to be string, resource given
+printf() expects parameter 1 to be string, resource given
echo "\n-- Iteration $count --\n";
// with default argument
- var_dump( sprintf($value) );
+ try {
+ var_dump(sprintf($value));
+ } catch (TypeError $exception) {
+ echo $exception->getMessage() . "\n";
+ }
// with two arguments
- var_dump( sprintf($value, $arg1) );
+ try {
+ var_dump(sprintf($value, $arg1));
+ } catch (TypeError $exception) {
+ echo $exception->getMessage() . "\n";
+ }
// with three arguments
- var_dump( sprintf($value, $arg1, $arg2) );
+ try {
+ var_dump(sprintf($value, $arg1, $arg2));
+ } catch (TypeError $exception) {
+ echo $exception->getMessage() . "\n";
+ }
$count++;
-};
+}
// close the resource
fclose($file_handle);
string(3) "0.5"
-- Iteration 10 --
-
-Warning: Array to string conversion in %s on line %d
-string(5) "Array"
-
-Warning: Array to string conversion in %s on line %d
-string(5) "Array"
-
-Warning: Array to string conversion in %s on line %d
-string(5) "Array"
+sprintf() expects parameter 1 to be string, array given
+sprintf() expects parameter 1 to be string, array given
+sprintf() expects parameter 1 to be string, array given
-- Iteration 11 --
-
-Warning: Array to string conversion in %s on line %d
-string(5) "Array"
-
-Warning: Array to string conversion in %s on line %d
-string(5) "Array"
-
-Warning: Array to string conversion in %s on line %d
-string(5) "Array"
+sprintf() expects parameter 1 to be string, array given
+sprintf() expects parameter 1 to be string, array given
+sprintf() expects parameter 1 to be string, array given
-- Iteration 12 --
-
-Warning: Array to string conversion in %s on line %d
-string(5) "Array"
-
-Warning: Array to string conversion in %s on line %d
-string(5) "Array"
-
-Warning: Array to string conversion in %s on line %d
-string(5) "Array"
+sprintf() expects parameter 1 to be string, array given
+sprintf() expects parameter 1 to be string, array given
+sprintf() expects parameter 1 to be string, array given
-- Iteration 13 --
-
-Warning: Array to string conversion in %s on line %d
-string(5) "Array"
-
-Warning: Array to string conversion in %s on line %d
-string(5) "Array"
-
-Warning: Array to string conversion in %s on line %d
-string(5) "Array"
+sprintf() expects parameter 1 to be string, array given
+sprintf() expects parameter 1 to be string, array given
+sprintf() expects parameter 1 to be string, array given
-- Iteration 14 --
-
-Warning: Array to string conversion in %s on line %d
-string(5) "Array"
-
-Warning: Array to string conversion in %s on line %d
-string(5) "Array"
-
-Warning: Array to string conversion in %s on line %d
-string(5) "Array"
+sprintf() expects parameter 1 to be string, array given
+sprintf() expects parameter 1 to be string, array given
+sprintf() expects parameter 1 to be string, array given
-- Iteration 15 --
string(0) ""
string(0) ""
-- Iteration 26 --
-string(%d) "Resource id #%d"
-string(%d) "Resource id #%d"
-string(%d) "Resource id #%d"
+sprintf() expects parameter 1 to be string, resource given
+sprintf() expects parameter 1 to be string, resource given
+sprintf() expects parameter 1 to be string, resource given
Done
$fp = fopen( $file, "a+" );
echo "\n-- Testing vfprintf() function with wrong variable types as argument --\n";
-var_dump( vfprintf( $fp, array( 'foo %d', 'bar %s' ), 3.55552 ) );
-
-rewind( $fp );
-var_dump( stream_get_contents( $fp ) );
-ftruncate( $fp, 0 );
-rewind( $fp );
+try {
+ vfprintf($fp, array( 'foo %d', 'bar %s' ), 3.55552);
+} catch (TypeError $exception) {
+ echo $exception->getMessage() . "\n";
+}
var_dump( vfprintf( $fp, "Foo %y fake", "not available" ) );
unlink( $file );
?>
---EXPECTF--
+--EXPECT--
-- Testing vfprintf() function with wrong variable types as argument --
-
-Warning: Array to string conversion in %s on line %d
-int(5)
-string(5) "Array"
+vfprintf() expects parameter 2 to be string, array given
int(9)
string(9) "Foo fake"
$counter = 1;
foreach( $values as $value ) {
- fprintf( $fp, "\n-- Iteration %d --\n",$counter);
- vfprintf($fp, $value, $args);
+ fprintf($fp, "\n-- Iteration %d --\n", $counter);
+
+ try {
+ vfprintf($fp, $value, $args);
+ } catch (TypeError $exception) {
+ fprintf($fp, "%s\n", $exception->getMessage());
+ }
$counter++;
}
--EXPECTF--
*** Testing vfprintf() : with unexpected values for format argument ***
-Warning: Array to string conversion in %s on line %d
-
-Warning: Array to string conversion in %s on line %d
-
-Warning: Array to string conversion in %s on line %d
-
-Warning: Array to string conversion in %s on line %d
-
-Warning: Array to string conversion in %s on line %d
-
*** Testing vprintf() with with unexpected values for format argument ***
-- Iteration 1 --
-- Iteration 9 --
0.5
-- Iteration 10 --
-Array
+vfprintf() expects parameter 2 to be string, array given
+
-- Iteration 11 --
-Array
+vfprintf() expects parameter 2 to be string, array given
+
-- Iteration 12 --
-Array
+vfprintf() expects parameter 2 to be string, array given
+
-- Iteration 13 --
-Array
+vfprintf() expects parameter 2 to be string, array given
+
-- Iteration 14 --
-Array
+vfprintf() expects parameter 2 to be string, array given
+
-- Iteration 15 --
-- Iteration 16 --
-- Iteration 25 --
-- Iteration 26 --
-Resource id #%d
+vfprintf() expects parameter 2 to be string, resource given
$counter = 1;
foreach($values as $value) {
echo "\n -- Iteration $counter --\n";
- $result = vprintf($value,$args);
- echo "\n";
- var_dump($result);
- $counter++;
+ try {
+ $result = vprintf($value, $args);
+ echo "\n";
+ var_dump($result);
+ } catch (TypeError $exception) {
+ echo $exception->getMessage() . "\n";
+ }
-};
+ $counter++;
+}
// closing the resource
fclose($file_handle);
int(3)
-- Iteration 10 --
-
-Warning: Array to string conversion in %s on line %d
-Array
-int(5)
+vprintf() expects parameter 1 to be string, array given
-- Iteration 11 --
-
-Warning: Array to string conversion in %s on line %d
-Array
-int(5)
+vprintf() expects parameter 1 to be string, array given
-- Iteration 12 --
-
-Warning: Array to string conversion in %s on line %d
-Array
-int(5)
+vprintf() expects parameter 1 to be string, array given
-- Iteration 13 --
-
-Warning: Array to string conversion in %s on line %d
-Array
-int(5)
+vprintf() expects parameter 1 to be string, array given
-- Iteration 14 --
-
-Warning: Array to string conversion in %s on line %d
-Array
-int(5)
+vprintf() expects parameter 1 to be string, array given
-- Iteration 15 --
int(0)
-- Iteration 26 --
-Resource id #%d
-int(%d)
+vprintf() expects parameter 1 to be string, resource given
$counter = 1;
foreach($values as $value) {
echo "\n -- Iteration $counter --\n";
- var_dump( vsprintf($value,$args) );
+ try {
+ var_dump(vsprintf($value, $args));
+ } catch (TypeError $exception) {
+ echo $exception->getMessage() . "\n";
+ }
$counter++;
-
-};
+}
// closing the resource
fclose($file_handle);
string(3) "0.5"
-- Iteration 10 --
-
-Warning: Array to string conversion in %s on line %d
-string(5) "Array"
+vsprintf() expects parameter 1 to be string, array given
-- Iteration 11 --
-
-Warning: Array to string conversion in %s on line %d
-string(5) "Array"
+vsprintf() expects parameter 1 to be string, array given
-- Iteration 12 --
-
-Warning: Array to string conversion in %s on line %d
-string(5) "Array"
+vsprintf() expects parameter 1 to be string, array given
-- Iteration 13 --
-
-Warning: Array to string conversion in %s on line %d
-string(5) "Array"
+vsprintf() expects parameter 1 to be string, array given
-- Iteration 14 --
-
-Warning: Array to string conversion in %s on line %d
-string(5) "Array"
+vsprintf() expects parameter 1 to be string, array given
-- Iteration 15 --
string(0) ""
string(0) ""
-- Iteration 26 --
-string(%d) "Resource id #%d"
+vsprintf() expects parameter 1 to be string, resource given
Done
--EXPECT--
Object with no __toString():
Try 1:
-Object of class stdClass could not be converted to string
+printf() expects parameter 1 to be string, object given
Try 2: