Hi all, I'v add support of "*" modifier for sprintf, it exist in sprintf C
implementation (http://www.cplusplus.com/reference/clibrary/cstdio/printf/)

Usage:
sprintf("%.*f", 2, 2) // output "2.00"
sprintf("%.2f", 2) // output "2.00"

My patch attached

-- 
Azat Khuzhin
diff --git a/ext/standard/formatted_print.c b/ext/standard/formatted_print.c
index 4aebbf3..68093b7 100644
--- a/ext/standard/formatted_print.c
+++ b/ext/standard/formatted_print.c
@@ -354,6 +354,7 @@ php_sprintf_getnumber(char *buffer, int *pos)
  *   n    field size
  *  "."n  precision (floats only)
  *  "+"   Always place a sign (+ or -) in front of a number
+ *  "*"   The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted
  *
  * Type specifiers:
  *
@@ -504,7 +505,34 @@ php_formatted_print(int ht, int *len, int use_array, int format_offset TSRMLS_DC
 				if (format[inpos] == '.') {
 					inpos++;
 					PRINTF_DEBUG(("sprintf: getting precision\n"));
-					if (isdigit((int)format[inpos])) {
+
+					/* "*" modifier */
+					if (format[inpos] == '*') {
+						inpos++;
+
+						/* now we expect to find a precision arg */
+						if (multiuse) {
+							MAKE_STD_ZVAL(tmp);
+							*tmp = **(args[argnum]);
+							INIT_PZVAL(tmp);
+							zval_copy_ctor(tmp);
+						} else {
+							SEPARATE_ZVAL(args[argnum]);
+							tmp = *(args[argnum]);
+						}
+						convert_to_long(tmp);
+
+						if (Z_LVAL_P(tmp) > 0 && Z_LVAL_P(tmp) < INT_MAX) {
+							precision = Z_LVAL_P(tmp);
+							adjusting |= ADJ_PRECISION;
+							expprec = 1;
+						} else {
+							efree(result);
+							efree(args);
+							php_error_docref(NULL TSRMLS_CC, E_WARNING, "Precision must be greater than zero and less than %d", INT_MAX);
+							return NULL;
+						}
+					} else if (isdigit((int)format[inpos])) {
 						if ((precision = php_sprintf_getnumber(format, &inpos)) < 0) {
 							efree(result);
 							efree(args);
-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to