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]);
Re: [PHP-DEV] printf argument swapping patch
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. -Rasmus On Fri, 6 Apr 2001, Morten Poulsen wrote: 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] -- 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]
[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, + +