]> granicus.if.org Git - php/commitdiff
printf argnum (parameter swapping) support from Morten Poulsen
authorRasmus Lerdorf <rasmus@php.net>
Mon, 9 Apr 2001 15:44:24 +0000 (15:44 +0000)
committerRasmus Lerdorf <rasmus@php.net>
Mon, 9 Apr 2001 15:44:24 +0000 (15:44 +0000)
NEWS
ext/standard/formatted_print.c
tests/strings/002.phpt

diff --git a/NEWS b/NEWS
index a52338ef1ccba2df525e9cae9b819bb358274239..52b265053d8bfb2b62d03018be24339c7e57c316 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,7 @@ PHP 4.0                                                                    NEWS
 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
 
 ?? ??? 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 20f30071fac47eee79c461c0f0174182e4eba18c..2962147e9aa55016f7a1e1188ceaf5653f700246 100644 (file)
@@ -390,8 +390,8 @@ static char *
 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 @@ php_formatted_print(int ht, int *len)
                        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 @@ php_formatted_print(int ht, int *len)
                                }
                                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 @@ php_formatted_print(int ht, int *len)
                                PRINTF_DEBUG(("sprintf: precision=%d\n", precision));
                        } else {
                                width = precision = 0;
+                               argnum = currarg++;
                        }
 
                        if (format[inpos] == 'l') {
@@ -495,67 +512,67 @@ php_formatted_print(int ht, int *len)
                        /* 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 @@ php_formatted_print(int ht, int *len)
                                default:
                                        break;
                        }
-                       currarg++;
                        inpos++;
                }
        }
index 50c0b352871f16fb2825600a5698789e65632ca0..195d3bd5e5e3f4dcc3e6fa725a6125cee3af6995 100644 (file)
@@ -34,6 +34,10 @@ printf("printf test 22:%016x\n", 170);
 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 22:00000000000000aa
 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