cvs: php4 / NEWS /ext/standard formatted_print.c /tests/strings 002.phpt
| From: | Rasmus Lerdorf | Date: | Mon, 09 Apr 2001 15:44:25 +0000 |
| Subject: | cvs: php4 / NEWS /ext/standard formatted_print.c /tests/strings 002.phpt | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-5050@lists.php.net to get a copy of this message | ||
rasmus Mon Apr 9 08:44:25 2001 EDT
Modified files:
/php4 NEWS
/php4/ext/standard formatted_print.c
/php4/tests/strings 002.phpt
Log:
printf argnum (parameter swapping) support from Morten Poulsen
Index: php4/NEWS
diff -u php4/NEWS:1.628 php4/NEWS:1.629
--- php4/NEWS:1.628 Sun Apr 8 18:09:23 2001
+++ php4/NEWS Mon Apr 9 08:44:23 2001
@@ -2,6 +2,7 @@
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?? ??? 200?, Version 4.0.6
+- printf argnum (parameter swapping) support (Morten Poulsen, Rasmus)
- Add DIRECTORY_SEPARATOR constant ('/' on UNIX, '\' on Windows) (Stig)
- Added small change to php_odbc module, to check for failed SQLDisconnects
and to close any outstanding transactions if the call fails, then disconnect
Index: php4/ext/standard/formatted_print.c
diff -u php4/ext/standard/formatted_print.c:1.29 php4/ext/standard/formatted_print.c:1.30
--- php4/ext/standard/formatted_print.c:1.29 Tue Mar 27 09:21:45 2001
+++ php4/ext/standard/formatted_print.c Mon Apr 9 08:44:24 2001
@@ -16,7 +16,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: formatted_print.c,v 1.29 2001/03/27 17:21:45 derick Exp $ */
+/* $Id: formatted_print.c,v 1.30 2001/04/09 15:44:24 rasmus Exp $ */
#include <math.h> /* modf() */
#include "php.h"
@@ -390,8 +390,8 @@
php_formatted_print(int ht, int *len)
{
pval ***args;
- int argc, size = 240, inpos = 0, outpos = 0;
- int alignment, width, precision, currarg, adjusting;
+ int argc, size = 240, inpos = 0, outpos = 0, temppos;
+ int alignment, width, precision, currarg, adjusting, argnum;
char *format, *result, padding;
argc = ZEND_NUM_ARGS();
@@ -437,7 +437,23 @@
PRINTF_DEBUG(("sprintf: first looking at '%c', inpos=%d\n",
format[inpos], inpos));
if (isascii((int)format[inpos]) && !isalpha((int)format[inpos])) {
- /* first look for modifiers */
+ /* first look for argnum */
+ temppos = inpos;
+ while (isdigit((int)format[temppos])) temppos++;
+ if (format[temppos] == '$') {
+ argnum = php_sprintf_getnumber(format, &inpos);
+ inpos++; /* skip the '$' */
+ } else {
+ argnum = currarg++;
+ }
+ if (argnum >= argc) {
+ efree(result);
+ efree(args);
+ php_error(E_WARNING, "%s(): too few arguments",get_active_function_name());
+ return NULL;
+ }
+
+ /* after argnum comes modifiers */
PRINTF_DEBUG(("sprintf: looking for modifiers\n"
"sprintf: now looking at '%c', inpos=%d\n",
format[inpos], inpos));
@@ -469,7 +485,7 @@
}
PRINTF_DEBUG(("sprintf: width=%d\n", width));
- /* after width comes precision */
+ /* after width and argnum comes precision */
if (format[inpos] == '.') {
inpos++;
PRINTF_DEBUG(("sprintf: getting precision\n"));
@@ -486,6 +502,7 @@
PRINTF_DEBUG(("sprintf: precision=%d\n", precision));
} else {
width = precision = 0;
+ argnum = currarg++;
}
if (format[inpos] == 'l') {
@@ -495,67 +512,67 @@
/* now we expect to find a type specifier */
switch (format[inpos]) {
case 's':
- convert_to_string_ex(args[currarg]);
+ convert_to_string_ex(args[argnum]);
php_sprintf_appendstring(&result, &outpos, &size,
- (*args[currarg])->value.str.val,
+ (*args[argnum])->value.str.val,
width, precision, padding,
alignment,
- (*args[currarg])->value.str.len,
+ (*args[argnum])->value.str.len,
0, expprec);
break;
case 'd':
- convert_to_long_ex(args[currarg]);
+ convert_to_long_ex(args[argnum]);
php_sprintf_appendint(&result, &outpos, &size,
- (*args[currarg])->value.lval,
+ (*args[argnum])->value.lval,
width, padding, alignment);
break;
case 'e':
case 'f':
/* XXX not done */
- convert_to_double_ex(args[currarg]);
+ convert_to_double_ex(args[argnum]);
php_sprintf_appenddouble(&result, &outpos, &size,
- (*args[currarg])->value.dval,
+ (*args[argnum])->value.dval,
width, padding, alignment,
precision, adjusting,
format[inpos]);
break;
case 'c':
- convert_to_long_ex(args[currarg]);
+ convert_to_long_ex(args[argnum]);
php_sprintf_appendchar(&result, &outpos, &size,
- (char) (*args[currarg])->value.lval);
+ (char) (*args[argnum])->value.lval);
break;
case 'o':
- convert_to_long_ex(args[currarg]);
+ convert_to_long_ex(args[argnum]);
php_sprintf_append2n(&result, &outpos, &size,
- (*args[currarg])->value.lval,
+ (*args[argnum])->value.lval,
width, padding, alignment, 3,
hexchars, expprec);
break;
case 'x':
- convert_to_long_ex(args[currarg]);
+ convert_to_long_ex(args[argnum]);
php_sprintf_append2n(&result, &outpos, &size,
- (*args[currarg])->value.lval,
+ (*args[argnum])->value.lval,
width, padding, alignment, 4,
hexchars, expprec);
break;
case 'X':
- convert_to_long_ex(args[currarg]);
+ convert_to_long_ex(args[argnum]);
php_sprintf_append2n(&result, &outpos, &size,
- (*args[currarg])->value.lval,
+ (*args[argnum])->value.lval,
width, padding, alignment, 4,
HEXCHARS, expprec);
break;
case 'b':
- convert_to_long_ex(args[currarg]);
+ convert_to_long_ex(args[argnum]);
php_sprintf_append2n(&result, &outpos, &size,
- (*args[currarg])->value.lval,
+ (*args[argnum])->value.lval,
width, padding, alignment, 1,
hexchars, expprec);
break;
@@ -567,7 +584,6 @@
default:
break;
}
- currarg++;
inpos++;
}
}
Index: php4/tests/strings/002.phpt
diff -u php4/tests/strings/002.phpt:1.2 php4/tests/strings/002.phpt:1.3
--- php4/tests/strings/002.phpt:1.2 Wed Mar 21 04:47:46 2001
+++ php4/tests/strings/002.phpt Mon Apr 9 08:44:24 2001
@@ -34,6 +34,10 @@
printf("printf test 23:%016X\n", 170);
printf("printf test 24:%.5s\n", "abcdefghij");
printf("printf test 25:%-2s\n", "gazonk");
+printf("printf test 26:%2\$d %1\$d\n", 1, 2);
+printf("printf test 27:%3\$d %d %d\n", 1, 2, 3);
+printf("printf test 28:%2\$02d %1\$2d\n", 1, 2);
+printf("printf test 29:%2\$-2d %1\$2d\n", 1, 2);
?>
--EXPECT--
@@ -64,3 +68,7 @@
printf test 23:00000000000000AA
printf test 24:abcde
printf test 25:gazonk
+printf test 26:2 1
+printf test 27:3 1 2
+printf test 28:02 1
+printf test 29:2 1