diff options
author | Rasmus Lerdorf <rasmus@php.net> | 2001-04-09 15:44:24 +0000 |
---|---|---|
committer | Rasmus Lerdorf <rasmus@php.net> | 2001-04-09 15:44:24 +0000 |
commit | 334cba47b9920c5400a49e630e035ac8c52fc824 (patch) | |
tree | 65dbe7da37cec4be2d63b341070e309bc53af6d4 | |
parent | 066547a220b460fcd92a9174734a4dea1f0cfb3f (diff) | |
download | php-git-334cba47b9920c5400a49e630e035ac8c52fc824.tar.gz |
printf argnum (parameter swapping) support from Morten Poulsen
-rw-r--r-- | NEWS | 1 | ||||
-rw-r--r-- | ext/standard/formatted_print.c | 60 | ||||
-rw-r--r-- | tests/strings/002.phpt | 8 |
3 files changed, 47 insertions, 22 deletions
@@ -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 diff --git a/ext/standard/formatted_print.c b/ext/standard/formatted_print.c index 20f30071fa..2962147e9a 100644 --- a/ext/standard/formatted_print.c +++ b/ext/standard/formatted_print.c @@ -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++; } } diff --git a/tests/strings/002.phpt b/tests/strings/002.phpt index 50c0b35287..195d3bd5e5 100644 --- a/tests/strings/002.phpt +++ b/tests/strings/002.phpt @@ -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 |