Bring forward the zero-precision (%.0s) string formatting fix

from PHP3.  (After being nagged by Thies. :-)  Whee!  My first
	PHP4 commit..
This commit is contained in:
Ken Coar 2000-07-18 16:34:30 +00:00
parent 7bb67a6200
commit 0ca493768b
2 changed files with 56 additions and 40 deletions

View file

@ -1,3 +1,7 @@
2000-07-18 Ken Coar <coar@php.net>
* ext/standard/formatted_print.c: Fix zero-precision %s processing
2000-07-17 Hénot David <henot@iie.cnam.fr> 2000-07-17 Hénot David <henot@iie.cnam.fr>
* ext/ingres_ii/ii.c * ext/ingres_ii/ii.c

View file

@ -151,18 +151,20 @@ php_sprintf_appendchar(char **buffer, int *pos, int *size, char add)
inline static void inline static void
php_sprintf_appendstring(char **buffer, int *pos, int *size, char *add, php_sprintf_appendstring(char **buffer, int *pos, int *size, char *add,
int min_width, int max_width, char padding, int min_width, int max_width, char padding,
int alignment, int len, int sign) int alignment, int len, int sign, int expprec)
{ {
register int npad = min_width - MIN(len,(max_width?max_width:len)); register int npad;
if (npad<0) { npad = min_width - MIN(len, (expprec ? max_width : len));
npad=0;
if (npad < 0) {
npad = 0;
} }
PRINTF_DEBUG(("sprintf: appendstring(%x, %d, %d, \"%s\", %d, '%c', %d)\n", PRINTF_DEBUG(("sprintf: appendstring(%x, %d, %d, \"%s\", %d, '%c', %d)\n",
*buffer, *pos, *size, add, min_width, padding, alignment)); *buffer, *pos, *size, add, min_width, padding, alignment));
if (max_width == 0) { if ((max_width == 0) && (! expprec)) {
max_width = MAX(min_width,len); max_width = MAX(min_width, len);
} }
if ((*pos + max_width) >= *size) { if ((*pos + max_width) >= *size) {
while ((*pos + max_width) >= *size) { while ((*pos + max_width) >= *size) {
@ -172,14 +174,18 @@ php_sprintf_appendstring(char **buffer, int *pos, int *size, char *add,
*buffer = erealloc(*buffer, *size); *buffer = erealloc(*buffer, *size);
} }
if (alignment == ALIGN_RIGHT) { if (alignment == ALIGN_RIGHT) {
if (sign && padding=='0') { (*buffer)[(*pos)++] = '-'; add++; len--; } if (sign && padding=='0') {
(*buffer)[(*pos)++] = '-';
add++;
len--;
}
while (npad-- > 0) { while (npad-- > 0) {
(*buffer)[(*pos)++] = padding; (*buffer)[(*pos)++] = padding;
} }
} }
PRINTF_DEBUG(("sprintf: appending \"%s\"\n", add)); PRINTF_DEBUG(("sprintf: appending \"%s\"\n", add));
strncpy(&(*buffer)[*pos], add, max_width); strncpy(&(*buffer)[*pos], add, max_width);
*pos += MIN(max_width,len); *pos += MIN(max_width, len);
if (alignment == ALIGN_LEFT) { if (alignment == ALIGN_LEFT) {
while (npad--) { while (npad--) {
(*buffer)[(*pos)++] = padding; (*buffer)[(*pos)++] = padding;
@ -190,7 +196,7 @@ php_sprintf_appendstring(char **buffer, int *pos, int *size, char *add,
inline static void inline static void
php_sprintf_appendint(char **buffer, int *pos, int *size, int number, php_sprintf_appendint(char **buffer, int *pos, int *size, int number,
int width, char padding, int alignment) int width, char padding, int alignment, int expprec)
{ {
char numbuf[NUM_BUF_SIZE]; char numbuf[NUM_BUF_SIZE];
register unsigned int magn, nmagn, i = NUM_BUF_SIZE - 1, neg = 0; register unsigned int magn, nmagn, i = NUM_BUF_SIZE - 1, neg = 0;
@ -222,16 +228,17 @@ php_sprintf_appendint(char **buffer, int *pos, int *size, int number,
PRINTF_DEBUG(("sprintf: appending %d as \"%s\", i=%d\n", PRINTF_DEBUG(("sprintf: appending %d as \"%s\", i=%d\n",
number, &numbuf[i], i)); number, &numbuf[i], i));
php_sprintf_appendstring(buffer, pos, size, &numbuf[i], width, 0, php_sprintf_appendstring(buffer, pos, size, &numbuf[i], width, 0,
padding, alignment, (NUM_BUF_SIZE - 1) - i, neg); padding, alignment, (NUM_BUF_SIZE - 1) - i,
neg, expprec);
} }
inline static void inline static void
php_sprintf_appenddouble(char **buffer, int *pos, php_sprintf_appenddouble(char **buffer, int *pos,
int *size, double number, int *size, double number,
int width, char padding, int width, char padding,
int alignment, int precision, int alignment, int precision,
int adjust, char fmt) int adjust, char fmt, int expprec)
{ {
char numbuf[NUM_BUF_SIZE]; char numbuf[NUM_BUF_SIZE];
char *cvt; char *cvt;
@ -283,14 +290,14 @@ php_sprintf_appenddouble(char **buffer, int *pos,
width += (precision + 1); width += (precision + 1);
} }
php_sprintf_appendstring(buffer, pos, size, numbuf, width, 0, padding, php_sprintf_appendstring(buffer, pos, size, numbuf, width, 0, padding,
alignment, i, sign); alignment, i, sign, expprec);
} }
inline static void inline static void
php_sprintf_append2n(char **buffer, int *pos, int *size, int number, php_sprintf_append2n(char **buffer, int *pos, int *size, int number,
int width, char padding, int alignment, int n, int width, char padding, int alignment, int n,
char *chartable) char *chartable, int expprec)
{ {
char numbuf[NUM_BUF_SIZE]; char numbuf[NUM_BUF_SIZE];
register unsigned int num, i = NUM_BUF_SIZE - 1, neg = 0; register unsigned int num, i = NUM_BUF_SIZE - 1, neg = 0;
@ -320,7 +327,8 @@ php_sprintf_append2n(char **buffer, int *pos, int *size, int number,
numbuf[--i] = '-'; numbuf[--i] = '-';
} }
php_sprintf_appendstring(buffer, pos, size, &numbuf[i], width, 0, php_sprintf_appendstring(buffer, pos, size, &numbuf[i], width, 0,
padding, alignment, (NUM_BUF_SIZE - 1) - i, neg); padding, alignment, (NUM_BUF_SIZE - 1) - i,
neg, expprec);
} }
@ -389,6 +397,8 @@ php_formatted_print(int ht, int *len)
currarg = 1; currarg = 1;
while (format[inpos]) { while (format[inpos]) {
int expprec = 0;
PRINTF_DEBUG(("sprintf: format[%d]='%c'\n", inpos, format[inpos])); PRINTF_DEBUG(("sprintf: format[%d]='%c'\n", inpos, format[inpos]));
PRINTF_DEBUG(("sprintf: outpos=%d\n", outpos)); PRINTF_DEBUG(("sprintf: outpos=%d\n", outpos));
if (format[inpos] != '%') { if (format[inpos] != '%') {
@ -451,6 +461,7 @@ php_formatted_print(int ht, int *len)
if (isdigit((int)format[inpos])) { if (isdigit((int)format[inpos])) {
precision = php_sprintf_getnumber(format, &inpos); precision = php_sprintf_getnumber(format, &inpos);
adjusting |= ADJ_PRECISION; adjusting |= ADJ_PRECISION;
expprec = 1;
} else { } else {
precision = 0; precision = 0;
} }
@ -471,17 +482,18 @@ php_formatted_print(int ht, int *len)
case 's': case 's':
convert_to_string_ex(args[currarg]); convert_to_string_ex(args[currarg]);
php_sprintf_appendstring(&result, &outpos, &size, php_sprintf_appendstring(&result, &outpos, &size,
(*args[currarg])->value.str.val, (*args[currarg])->value.str.val,
width, precision, padding, width, precision, padding,
alignment, alignment,
(*args[currarg])->value.str.len,0); (*args[currarg])->value.str.len,
0, expprec);
break; break;
case 'd': case 'd':
convert_to_long_ex(args[currarg]); convert_to_long_ex(args[currarg]);
php_sprintf_appendint(&result, &outpos, &size, php_sprintf_appendint(&result, &outpos, &size,
(*args[currarg])->value.lval, (*args[currarg])->value.lval,
width, padding, alignment); width, padding, alignment, expprec);
break; break;
case 'e': case 'e':
@ -489,10 +501,10 @@ php_formatted_print(int ht, int *len)
/* XXX not done */ /* XXX not done */
convert_to_double_ex(args[currarg]); convert_to_double_ex(args[currarg]);
php_sprintf_appenddouble(&result, &outpos, &size, php_sprintf_appenddouble(&result, &outpos, &size,
(*args[currarg])->value.dval, (*args[currarg])->value.dval,
width, padding, alignment, width, padding, alignment,
precision, adjusting, precision, adjusting,
format[inpos]); format[inpos], expprec);
break; break;
case 'c': case 'c':
@ -504,33 +516,33 @@ php_formatted_print(int ht, int *len)
case 'o': case 'o':
convert_to_long_ex(args[currarg]); convert_to_long_ex(args[currarg]);
php_sprintf_append2n(&result, &outpos, &size, php_sprintf_append2n(&result, &outpos, &size,
(*args[currarg])->value.lval, (*args[currarg])->value.lval,
width, padding, alignment, 3, width, padding, alignment, 3,
hexchars); hexchars, expprec);
break; break;
case 'x': case 'x':
convert_to_long_ex(args[currarg]); convert_to_long_ex(args[currarg]);
php_sprintf_append2n(&result, &outpos, &size, php_sprintf_append2n(&result, &outpos, &size,
(*args[currarg])->value.lval, (*args[currarg])->value.lval,
width, padding, alignment, 4, width, padding, alignment, 4,
hexchars); hexchars, expprec);
break; break;
case 'X': case 'X':
convert_to_long_ex(args[currarg]); convert_to_long_ex(args[currarg]);
php_sprintf_append2n(&result, &outpos, &size, php_sprintf_append2n(&result, &outpos, &size,
(*args[currarg])->value.lval, (*args[currarg])->value.lval,
width, padding, alignment, 4, width, padding, alignment, 4,
HEXCHARS); HEXCHARS, expprec);
break; break;
case 'b': case 'b':
convert_to_long_ex(args[currarg]); convert_to_long_ex(args[currarg]);
php_sprintf_append2n(&result, &outpos, &size, php_sprintf_append2n(&result, &outpos, &size,
(*args[currarg])->value.lval, (*args[currarg])->value.lval,
width, padding, alignment, 1, width, padding, alignment, 1,
hexchars); hexchars, expprec);
break; break;
case '%': case '%':