1
0
mirror of https://github.com/php/php-src.git synced 2026-04-23 16:08:35 +02:00
Files
Niels Dossche 799ec7b8c5 Fix misleading errors in printf()
The precision and width _can_ be zero.

Closes GH-18911.
2025-06-23 19:58:49 +02:00

113 lines
2.0 KiB
PHP

--TEST--
Star width and precision in sprintf()
--FILE--
<?php
$f = 1.23456789012345678;
$fx = 1.23456789012345678e100;
var_dump($f, $fx);
printf("%.*f\n", 10, $f);
printf("%.*G\n", 10, $f);
printf("%.*g\n", -1, $fx);
printf("%.*G\n", -1, $fx);
printf("%.*h\n", -1, $fx);
printf("%.*H\n", -1, $fx);
printf("%.*s\n", 3, "foobar");
echo "\n";
printf("%*f\n", 10, $f);
printf("%*G\n", 10, $f);
printf("%*s\n", 10, "foobar");
echo "\n";
printf("%*.*f\n", 10, 3, $f);
printf("%*.*G\n", 10, 3, $f);
printf("%*.*s\n", 10, 3, "foobar");
echo "\n";
printf("%1$.*2\$f\n", $f, 10);
printf("%.*2\$f\n", $f, 10);
printf("%2$.*f\n", 10, $f);
printf("%1$*2\$f\n", $f, 10);
printf("%*2\$f\n", $f, 10);
printf("%2$*f\n", 10, $f);
printf("%1$*2$.*3\$f\n", $f, 10, 3);
printf("%*2$.*3\$f\n", $f, 10, 3);
printf("%3$*.*f\n", 10, 3, $f);
echo "\n";
try {
printf("%.*G\n", "foo", 1.5);
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}
try {
printf("%.*G\n", -100, 1.5);
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}
try {
printf("%.*s\n", -1, "Foo");
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}
try {
printf("%*G\n", -1, $f);
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}
try {
printf("%9999999999999999999999.f\n", $f);
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}
try {
printf("%.9999999999999999999999f\n", $f);
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}
?>
--EXPECT--
float(1.2345678901234567)
float(1.2345678901234569E+100)
1.2345678901
1.23456789
1.2345678901234569e+100
1.2345678901234569E+100
1.2345678901234569e+100
1.2345678901234569E+100
foo
1.234568
1.23457
foobar
1.235
1.23
foo
1.2345678901
1.2345678901
1.2345678901
1.234568
1.234568
1.234568
1.235
1.235
1.235
Precision must be an integer
Precision must be between -1 and 2147483647
Precision -1 is only supported for %g, %G, %h and %H
Width must be between 0 and 2147483647
Width must be between 0 and 2147483647
Precision must be between 0 and 2147483647