[PHP-DEV] printf argument swapping patch
Hi PHP Team, I have made a patch, so PHP can support swapping of arguments to printf (and friends), the way libc do it. This is especialy usefull when doing internationalization (see http://www.gnu.org/manual/gettext/html_mono/gettext.html#SEC17 for example), but it is also usefull when you need to print some argument several times. I hope it's usefull :-) Greetings, Morten -- mortenp "Programs expand to fill the memory available to hold them" --- Parkinson's Law diff -ur php-4.0.4pl1.orig/ext/standard/formatted_print.c php-4.0.4pl1/ext/standard/formatted_print.c --- php-4.0.4pl1.orig/ext/standard/formatted_print.cFri Apr 6 16:07:49 2001 +++ php-4.0.4pl1/ext/standard/formatted_print.c Fri Apr 6 16:48:02 2001 @@ -391,7 +391,7 @@ { pval ***args; int argc, size = 240, inpos = 0, outpos = 0; - int alignment, width, precision, currarg, adjusting; + int alignment, width, precision, currarg, adjusting, argnum, tempnum; char *format, *result, padding; argc = ZEND_NUM_ARGS(); @@ -459,17 +459,38 @@ (alignment == ALIGN_LEFT) ? "left" : "right")); - /* after modifiers comes width */ + /* after modifiers comes width or argnum */ if (isdigit((int)format[inpos])) { - PRINTF_DEBUG(("sprintf: getting width\n")); - width = php_sprintf_getnumber(format, inpos); - adjusting |= ADJ_WIDTH; + PRINTF_DEBUG(("sprintf: getting width or +argnum\n")); + tempnum = php_sprintf_getnumber(format, +inpos); + if (format[inpos] == '$') { + argnum = tempnum; + inpos++; /* skip the '$' */ + if (isdigit((int)format[inpos])) { + width = +php_sprintf_getnumber(format, inpos); + adjusting |= ADJ_WIDTH; + } else { + width = 0; + } + } else { + argnum = currarg++; + width = tempnum; + adjusting |= ADJ_WIDTH; + } } else { + argnum = currarg++; width = 0; } PRINTF_DEBUG(("sprintf: width=%d\n", width)); - /* after width comes precision */ + if (argnum = argc) { + efree(result); + efree(args); + php_error(E_WARNING, "%s(): too few +arguments",get_active_function_name()); + return NULL; + } + + /* after width and argnum comes precision */ if (format[inpos] == '.') { inpos++; PRINTF_DEBUG(("sprintf: getting precision\n")); @@ -495,67 +516,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,
Re: [PHP-DEV] printf argument swapping patch
Morten Poulsen [EMAIL PROTECTED] said: I hope it's usefull :-) is's not! i just found a bug.. i'll fix it and send a new patch, sorry.. -- mortenp Thus spake the master programmer: "Let the programmers be many and the managers few -- then all will be productive." -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DEV] printf argument swapping patch
Morten Poulsen [EMAIL PROTECTED] said: i'll fix it and send a new patch, sorry.. Allright, here is the new patch. Sorry about all the fuzz. Greetings, Morten -- mortenp Thus spake the master programmer: "Let the programmers be many and the managers few -- then all will be productive." diff -ur php-4.0.4pl1.orig/ext/standard/formatted_print.c php-4.0.4pl1/ext/standard/formatted_print.c --- php-4.0.4pl1.orig/ext/standard/formatted_print.cFri Apr 6 16:07:49 2001 +++ php-4.0.4pl1/ext/standard/formatted_print.c Fri Apr 6 18:19:38 2001 @@ -391,7 +391,7 @@ { pval ***args; int argc, size = 240, inpos = 0, outpos = 0; - int alignment, width, precision, currarg, adjusting; + int alignment, width, precision, currarg, adjusting, argnum, tempnum; char *format, *result, padding; argc = ZEND_NUM_ARGS(); @@ -459,17 +459,38 @@ (alignment == ALIGN_LEFT) ? "left" : "right")); - /* after modifiers comes width */ + /* after modifiers comes width or argnum */ if (isdigit((int)format[inpos])) { - PRINTF_DEBUG(("sprintf: getting width\n")); - width = php_sprintf_getnumber(format, inpos); - adjusting |= ADJ_WIDTH; + PRINTF_DEBUG(("sprintf: getting width or +argnum\n")); + tempnum = php_sprintf_getnumber(format, +inpos); + if (format[inpos] == '$') { + argnum = tempnum; + inpos++; /* skip the '$' */ + if (isdigit((int)format[inpos])) { + width = +php_sprintf_getnumber(format, inpos); + adjusting |= ADJ_WIDTH; + } else { + width = 0; + } + } else { + argnum = currarg++; + width = tempnum; + adjusting |= ADJ_WIDTH; + } } else { + argnum = currarg++; width = 0; } PRINTF_DEBUG(("sprintf: width=%d\n", width)); - /* after width comes precision */ + if (argnum = argc) { + efree(result); + efree(args); + php_error(E_WARNING, "%s(): too few +arguments",get_active_function_name()); + return NULL; + } + + /* after width and argnum comes precision */ if (format[inpos] == '.') { inpos++; PRINTF_DEBUG(("sprintf: getting precision\n")); @@ -486,6 +507,7 @@ PRINTF_DEBUG(("sprintf: precision=%d\n", precision)); } else { width = precision = 0; + argnum = currarg++; } if (format[inpos] == 'l') { @@ -495,67 +517,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, +
Re: [PHP-DEV] printf argument swapping patch
Hi, Rasmus Lerdorf [EMAIL PROTECTED] said: Would you mind writing a test case or two to go along with this patch? See the tests/README file for help on how to write these. no, of cause. The attached patch also contains four more tests for printf (tests/strings/002.phpt), which tests the added feature. -- mortenp Thus spake the master programmer: "Let the programmers be many and the managers few -- then all will be productive." diff -ur php-4.0.4pl1.orig/ext/standard/formatted_print.c php-4.0.4pl1/ext/standard/formatted_print.c --- php-4.0.4pl1.orig/ext/standard/formatted_print.cFri Apr 6 16:07:49 2001 +++ php-4.0.4pl1/ext/standard/formatted_print.c Mon Apr 9 16:33:42 2001 @@ -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-DEV] patch for sprintf() argnum
Hi, I discovered a bug in sprintf()'s argument swapping code. It accepts an argument number of zero, which is invalid. It is handled in different ways in different libcs, but i figured the best way to handle it in PHP was to make the functioncall fail. Patch is attached. Best regards, Morten PS. Thanks to mbn for whining :-) diff -ur php-4.1.2.orig/ext/standard/formatted_print.c php-4.1.2/ext/standard/formatted_print.c --- php-4.1.2.orig/ext/standard/formatted_print.c Fri Mar 15 16:33:12 2002 +++ php-4.1.2/ext/standard/formatted_print.c Fri Mar 15 17:12:29 2002 -479,7 +479,12 temppos = inpos; while (isdigit((int)format[temppos])) temppos++; if (format[temppos] == '$') { - argnum = php_sprintf_getnumber(format, inpos); + if ((argnum = php_sprintf_getnumber(format, inpos)) == 0) { + efree(result); + efree(args); + php_error(E_WARNING, %s(): zero is not a valid argument number, get_active_function_name(TSRMLS_C)); + return NULL; + } inpos++; /* skip the '$' */ } else { argnum = currarg++; diff -ur php-4.1.2.orig/tests/strings/002.phpt php-4.1.2/tests/strings/002.phpt --- php-4.1.2.orig/tests/strings/002.phpt Fri Mar 15 16:33:13 2002 +++ php-4.1.2/tests/strings/002.phpt Fri Mar 15 17:10:28 2002 -38,6 +38,7 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); +print(printf test 30:); printf(%0\$s); print(x\n); ? --EXPECT-- -72,3 +73,4 printf test 27:3 1 2 printf test 28:02 1 printf test 29:2 1 +printf test 30:x -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-DEV] [PATCH] sprintf() argnum
Hi, I discovered a bug in sprintf()'s argument swapping code. It accepts an argument number of zero, which is invalid. It is handled in different ways in different libcs, but i figured the best way to handle it in PHP was to make the functioncall fail. Patch is attached. Best regards, Morten PS. Thanks to mbn for whining :-) -- Morten Poulsen [EMAIL PROTECTED] http://www.afdelingp.dk/ Index: ext/standard/formatted_print.c === RCS file: /repository/php4/ext/standard/formatted_print.c,v retrieving revision 1.46 diff -u -r1.46 formatted_print.c --- ext/standard/formatted_print.c 28 Feb 2002 08:26:45 - 1.46 +++ ext/standard/formatted_print.c 21 Mar 2002 16:26:35 - @@ -479,7 +479,12 @@ temppos = inpos; while (isdigit((int)format[temppos])) temppos++; if (format[temppos] == '$') { - argnum = php_sprintf_getnumber(format, inpos); + if ((argnum = php_sprintf_getnumber(format, inpos)) == 0) { + efree(result); + efree(args); + php_error(E_WARNING, %s(): zero is not a valid argument number, get_active_function_name(TSRMLS_C)); + return NULL; + } inpos++; /* skip the '$' */ } else { argnum = currarg++; Index: tests/strings/002.phpt === RCS file: /repository/php4/tests/strings/002.phpt,v retrieving revision 1.3 diff -u -r1.3 002.phpt --- tests/strings/002.phpt 9 Apr 2001 15:44:24 - 1.3 +++ tests/strings/002.phpt 21 Mar 2002 16:26:36 - @@ -38,6 +38,7 @@ 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); +print(printf test 30:); printf(%0\$s, 1); print(x\n); ? --EXPECT-- @@ -72,3 +73,4 @@ printf test 27:3 1 2 printf test 28:02 1 printf test 29:2 1 +printf test 30:x -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] standard platform names
On Wed, 2002-04-10 at 00:22, Stig S. Bakken wrote: The general format could be CPU-OS[-EXTRA] I like OS-CPU[-EXTRA] more. A shell script (as one of your examples) would often be CPU and EXTRA - but not OS - independend. If we use OS-CPU[-EXTRA] it runs on eg. linux-*, and not *-linux*. It's not often I have code that runs on _all_ powerpc's, no matter which OS. Just my .02 Euro, Morten -- Morten Poulsen [EMAIL PROTECTED] http://www.afdelingp.dk/ -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php