Skip to content

Add support for * width and precision in printf() #5432

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 1 commit 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
107 changes: 90 additions & 17 deletions ext/standard/formatted_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,27 @@ php_sprintf_getnumber(char **buffer, size_t *len)
}
/* }}} */

#define ARG_NUM_NEXT -1
#define ARG_NUM_INVALID -2

int php_sprintf_get_argnum(char **format, size_t *format_len) {
char *temppos = *format;
while (isdigit((int) *temppos)) temppos++;
if (*temppos != '$') {
return ARG_NUM_NEXT;
}

int argnum = php_sprintf_getnumber(format, format_len);
if (argnum <= 0) {
zend_value_error("Argument number must be greater than zero");
return ARG_NUM_INVALID;
}

(*format)++; /* skip the '$' */
(*format_len)--;
return argnum - 1;
}

/* php_formatted_print() {{{
* New sprintf implementation for PHP.
*
Expand Down Expand Up @@ -438,23 +459,12 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n
*format, format - Z_STRVAL_P(z_format)));
if (isalpha((int)*format)) {
width = precision = 0;
argnum = currarg++;
argnum = ARG_NUM_NEXT;
} else {
/* first look for argnum */
temppos = format;
while (isdigit((int)*temppos)) temppos++;
if (*temppos == '$') {
argnum = php_sprintf_getnumber(&format, &format_len);

if (argnum <= 0) {
zend_value_error("Argument number must be greater than zero");
goto fail;
}
argnum--;
format++; /* skip the '$' */
format_len--;
} else {
argnum = currarg++;
argnum = php_sprintf_get_argnum(&format, &format_len);
if (argnum == ARG_NUM_INVALID) {
goto fail;
}

/* after argnum comes modifiers */
Expand Down Expand Up @@ -489,7 +499,34 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n


/* after modifiers comes width */
if (isdigit((int)*format)) {
if (*format == '*') {
format++;
format_len--;

int width_argnum = php_sprintf_get_argnum(&format, &format_len);
if (width_argnum == ARG_NUM_INVALID) {
goto fail;
}
if (width_argnum == ARG_NUM_NEXT) {
width_argnum = currarg++;
}
if (width_argnum >= argc) {
max_missing_argnum = MAX(max_missing_argnum, width_argnum);
continue;
}
tmp = &args[width_argnum];
ZVAL_DEREF(tmp);
if (Z_TYPE_P(tmp) != IS_LONG) {
zend_value_error("Width must be an integer");
goto fail;
}
if (Z_LVAL_P(tmp) < 0 || Z_LVAL_P(tmp) > INT_MAX) {
zend_value_error("Width must be greater than zero and less than %d", INT_MAX);
goto fail;
}
width = Z_LVAL_P(tmp);
adjusting |= ADJ_WIDTH;
} else if (isdigit((int)*format)) {
PRINTF_DEBUG(("sprintf: getting width\n"));
if ((width = php_sprintf_getnumber(&format, &format_len)) < 0) {
zend_value_error("Width must be greater than zero and less than %d", INT_MAX);
Expand All @@ -506,7 +543,35 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n
format++;
format_len--;
PRINTF_DEBUG(("sprintf: getting precision\n"));
if (isdigit((int)*format)) {
if (*format == '*') {
format++;
format_len--;

int prec_argnum = php_sprintf_get_argnum(&format, &format_len);
if (prec_argnum == ARG_NUM_INVALID) {
goto fail;
}
if (prec_argnum == ARG_NUM_NEXT) {
prec_argnum = currarg++;
}
if (prec_argnum >= argc) {
max_missing_argnum = MAX(max_missing_argnum, prec_argnum);
continue;
}
tmp = &args[prec_argnum];
ZVAL_DEREF(tmp);
if (Z_TYPE_P(tmp) != IS_LONG) {
zend_value_error("Precision must be an integer");
goto fail;
}
if (Z_LVAL_P(tmp) < -1 || Z_LVAL_P(tmp) > INT_MAX) {
zend_value_error("Precision must be between -1 and %d", INT_MAX);
goto fail;
}
precision = Z_LVAL_P(tmp);
adjusting |= ADJ_PRECISION;
expprec = 1;
} else if (isdigit((int)*format)) {
if ((precision = php_sprintf_getnumber(&format, &format_len)) < 0) {
zend_value_error("Precision must be greater than zero and less than %d", INT_MAX);
goto fail;
Expand All @@ -528,11 +593,19 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n
}
PRINTF_DEBUG(("sprintf: format character='%c'\n", *format));

if (argnum == ARG_NUM_NEXT) {
argnum = currarg++;
}
if (argnum >= argc) {
max_missing_argnum = MAX(max_missing_argnum, argnum);
continue;
}

if (expprec && precision == -1 && *format != 'g' && *format != 'G') {
zend_value_error("Precision -1 is only supported for %%g and %%G");
goto fail;
}

/* now we expect to find a type specifier */
tmp = &args[argnum];
switch (*format) {
Expand Down
90 changes: 90 additions & 0 deletions ext/standard/tests/strings/sprintf_star.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
--TEST--
Star width and precision in sprintf()
--FILE--
<?php


$f = 1.23456789012345678;
var_dump($f);

printf("%.*f\n", 10, $f);
printf("%.*G\n", 10, $f);
printf("%.*G\n", -1, $f);
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";
}

?>
--EXPECTF--
float(1.2345678901234567)
1.2345678901
1.23456789
1.2345678901234567
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 and %G
Width must be greater than zero and less than 2147483647