Browse Source

printf argnum (parameter swapping) support from Morten Poulsen

PHP-4.0.6
Rasmus Lerdorf 25 years ago
parent
commit
334cba47b9
  1. 1
      NEWS
  2. 60
      ext/standard/formatted_print.c
  3. 8
      tests/strings/002.phpt

1
NEWS

@ -2,6 +2,7 @@ PHP 4.0 NEWS
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?? ??? 200?, Version 4.0.6 ?? ??? 200?, Version 4.0.6
- printf argnum (parameter swapping) support (Morten Poulsen, Rasmus)
- Add DIRECTORY_SEPARATOR constant ('/' on UNIX, '\' on Windows) (Stig) - Add DIRECTORY_SEPARATOR constant ('/' on UNIX, '\' on Windows) (Stig)
- Added small change to php_odbc module, to check for failed SQLDisconnects - Added small change to php_odbc module, to check for failed SQLDisconnects
and to close any outstanding transactions if the call fails, then disconnect and to close any outstanding transactions if the call fails, then disconnect

60
ext/standard/formatted_print.c

@ -390,8 +390,8 @@ static char *
php_formatted_print(int ht, int *len) php_formatted_print(int ht, int *len)
{ {
pval ***args; 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; char *format, *result, padding;
argc = ZEND_NUM_ARGS(); 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", PRINTF_DEBUG(("sprintf: first looking at '%c', inpos=%d\n",
format[inpos], inpos)); format[inpos], inpos));
if (isascii((int)format[inpos]) && !isalpha((int)format[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" PRINTF_DEBUG(("sprintf: looking for modifiers\n"
"sprintf: now looking at '%c', inpos=%d\n", "sprintf: now looking at '%c', inpos=%d\n",
format[inpos], inpos)); format[inpos], inpos));
@ -469,7 +485,7 @@ php_formatted_print(int ht, int *len)
} }
PRINTF_DEBUG(("sprintf: width=%d\n", width)); PRINTF_DEBUG(("sprintf: width=%d\n", width));
/* after width comes precision */
/* after width and argnum comes precision */
if (format[inpos] == '.') { if (format[inpos] == '.') {
inpos++; inpos++;
PRINTF_DEBUG(("sprintf: getting precision\n")); PRINTF_DEBUG(("sprintf: getting precision\n"));
@ -486,6 +502,7 @@ php_formatted_print(int ht, int *len)
PRINTF_DEBUG(("sprintf: precision=%d\n", precision)); PRINTF_DEBUG(("sprintf: precision=%d\n", precision));
} else { } else {
width = precision = 0; width = precision = 0;
argnum = currarg++;
} }
if (format[inpos] == 'l') { if (format[inpos] == 'l') {
@ -495,67 +512,67 @@ php_formatted_print(int ht, int *len)
/* now we expect to find a type specifier */ /* now we expect to find a type specifier */
switch (format[inpos]) { switch (format[inpos]) {
case 's': case 's':
convert_to_string_ex(args[currarg]);
convert_to_string_ex(args[argnum]);
php_sprintf_appendstring(&result, &outpos, &size, php_sprintf_appendstring(&result, &outpos, &size,
(*args[currarg])->value.str.val,
(*args[argnum])->value.str.val,
width, precision, padding, width, precision, padding,
alignment, alignment,
(*args[currarg])->value.str.len,
(*args[argnum])->value.str.len,
0, expprec); 0, expprec);
break; break;
case 'd': case 'd':
convert_to_long_ex(args[currarg]);
convert_to_long_ex(args[argnum]);
php_sprintf_appendint(&result, &outpos, &size, php_sprintf_appendint(&result, &outpos, &size,
(*args[currarg])->value.lval,
(*args[argnum])->value.lval,
width, padding, alignment); width, padding, alignment);
break; break;
case 'e': case 'e':
case 'f': case 'f':
/* XXX not done */ /* XXX not done */
convert_to_double_ex(args[currarg]);
convert_to_double_ex(args[argnum]);
php_sprintf_appenddouble(&result, &outpos, &size, php_sprintf_appenddouble(&result, &outpos, &size,
(*args[currarg])->value.dval,
(*args[argnum])->value.dval,
width, padding, alignment, width, padding, alignment,
precision, adjusting, precision, adjusting,
format[inpos]); format[inpos]);
break; break;
case 'c': case 'c':
convert_to_long_ex(args[currarg]);
convert_to_long_ex(args[argnum]);
php_sprintf_appendchar(&result, &outpos, &size, php_sprintf_appendchar(&result, &outpos, &size,
(char) (*args[currarg])->value.lval);
(char) (*args[argnum])->value.lval);
break; break;
case 'o': case 'o':
convert_to_long_ex(args[currarg]);
convert_to_long_ex(args[argnum]);
php_sprintf_append2n(&result, &outpos, &size, php_sprintf_append2n(&result, &outpos, &size,
(*args[currarg])->value.lval,
(*args[argnum])->value.lval,
width, padding, alignment, 3, width, padding, alignment, 3,
hexchars, expprec); hexchars, expprec);
break; break;
case 'x': case 'x':
convert_to_long_ex(args[currarg]);
convert_to_long_ex(args[argnum]);
php_sprintf_append2n(&result, &outpos, &size, php_sprintf_append2n(&result, &outpos, &size,
(*args[currarg])->value.lval,
(*args[argnum])->value.lval,
width, padding, alignment, 4, width, padding, alignment, 4,
hexchars, expprec); hexchars, expprec);
break; break;
case 'X': case 'X':
convert_to_long_ex(args[currarg]);
convert_to_long_ex(args[argnum]);
php_sprintf_append2n(&result, &outpos, &size, php_sprintf_append2n(&result, &outpos, &size,
(*args[currarg])->value.lval,
(*args[argnum])->value.lval,
width, padding, alignment, 4, width, padding, alignment, 4,
HEXCHARS, expprec); HEXCHARS, expprec);
break; break;
case 'b': case 'b':
convert_to_long_ex(args[currarg]);
convert_to_long_ex(args[argnum]);
php_sprintf_append2n(&result, &outpos, &size, php_sprintf_append2n(&result, &outpos, &size,
(*args[currarg])->value.lval,
(*args[argnum])->value.lval,
width, padding, alignment, 1, width, padding, alignment, 1,
hexchars, expprec); hexchars, expprec);
break; break;
@ -567,7 +584,6 @@ php_formatted_print(int ht, int *len)
default: default:
break; break;
} }
currarg++;
inpos++; inpos++;
} }
} }

8
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 23:%016X\n", 170);
printf("printf test 24:%.5s\n", "abcdefghij"); printf("printf test 24:%.5s\n", "abcdefghij");
printf("printf test 25:%-2s\n", "gazonk"); 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-- --EXPECT--
@ -64,3 +68,7 @@ printf test 22:00000000000000aa
printf test 23:00000000000000AA printf test 23:00000000000000AA
printf test 24:abcde printf test 24:abcde
printf test 25:gazonk 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
Loading…
Cancel
Save