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.c    Fri 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_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++;
                }
        }
diff -ur php-4.0.4pl1.orig/tests/strings/002.phpt php-4.0.4pl1/tests/strings/002.phpt
--- php-4.0.4pl1.orig/tests/strings/002.phpt    Fri Apr  6 16:08:13 2001
+++ php-4.0.4pl1/tests/strings/002.phpt Mon Apr  9 16:24:27 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--
@@ -65,3 +69,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

-- 
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]

Reply via email to