helly Sun May 16 17:20:30 2004 EDT Added files: (Branch: PHP_4_3) /php-src/ext/standard/tests/strings sprintf_f.phpt
Modified files: /php-src NEWS /php-src/ext/standard formatted_print.c Log: MFH: Fix printf with floats http://cvs.php.net/diff.php/php-src/NEWS?r1=1.1247.2.661&r2=1.1247.2.662&ty=u Index: php-src/NEWS diff -u php-src/NEWS:1.1247.2.661 php-src/NEWS:1.1247.2.662 --- php-src/NEWS:1.1247.2.661 Thu May 13 13:44:33 2004 +++ php-src/NEWS Sun May 16 17:20:29 2004 @@ -2,6 +2,7 @@ ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ?? ??? 2004, Version 4.3.7 - Upgraded bundled GD library to 2.0.23. (Ilia) +- Fixed problems with *printf() functions and '%f' formatting. (Marcus) - Fixed possible crash inside pg_copy_(to|from) function if delimiter is more then 1 character long. (Ilia) - Fixed crash inside cpdf_place_inline_image() when working with truecolor http://cvs.php.net/diff.php/php-src/ext/standard/formatted_print.c?r1=1.59.2.9&r2=1.59.2.10&ty=u Index: php-src/ext/standard/formatted_print.c diff -u php-src/ext/standard/formatted_print.c:1.59.2.9 php-src/ext/standard/formatted_print.c:1.59.2.10 --- php-src/ext/standard/formatted_print.c:1.59.2.9 Tue May 11 15:51:50 2004 +++ php-src/ext/standard/formatted_print.c Sun May 16 17:20:30 2004 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: formatted_print.c,v 1.59.2.9 2004/05/11 19:51:50 iliaa Exp $ */ +/* $Id: formatted_print.c,v 1.59.2.10 2004/05/16 21:20:30 helly Exp $ */ #include <math.h> /* modf() */ #include "php.h" @@ -65,6 +65,7 @@ static char *php_convert_to_decimal(double arg, int ndigits, int *decpt, int *sign, int eflag) { register int r2; + int mvl; double fi, fj; register char *p, *p1; /*THREADX*/ @@ -90,13 +91,21 @@ p1 = &cvt_buf[NDIG]; while (fi != 0) { fj = modf(fi / 10, &fi); + if (p1 <= &cvt_buf[0]) { + mvl = NDIG - ndigits; + memmove(&cvt_buf[mvl], &cvt_buf[0], NDIG-mvl-1); + p1 += mvl; + } *--p1 = (int) ((fj + .03) * 10) + '0'; r2++; } while (p1 < &cvt_buf[NDIG]) *p++ = *p1++; } else if (arg > 0) { - while ((fj = arg * 10.0) < 0.9999999) { + while ((fj = arg * 10) < 1) { + if (!eflag && (r2 * -1) < ndigits) { + break; + } arg = fj; r2--; } @@ -109,10 +118,17 @@ cvt_buf[0] = '\0'; return (cvt_buf); } + if (p <= p1 && p < &cvt_buf[NDIG]) { + arg = modf(arg * 10, &fj); + if ((int)fj==10) { + *p++ = '1'; + fj = 0; + *decpt = ++r2; + } while (p <= p1 && p < &cvt_buf[NDIG]) { - arg *= 10; - arg = modf(arg, &fj); *p++ = (int) fj + '0'; + arg = modf(arg * 10, &fj); + } } if (p1 >= &cvt_buf[NDIG]) { cvt_buf[NDIG - 1] = '\0'; @@ -286,7 +302,7 @@ char numbuf[NUM_BUF_SIZE]; char *cvt; register int i = 0, j = 0; - int sign, decpt; + int sign, decpt, cvt_len; char decimal_point = EG(float_separator)[0]; PRINTF_DEBUG(("sprintf: appenddouble(%x, %x, %x, %f, %d, '%c', %d, %c)\n", @@ -312,6 +328,7 @@ } cvt = php_convert_to_decimal(number, precision, &decpt, &sign, (fmt == 'e')); + cvt_len = strlen(cvt); if (sign) { numbuf[i++] = '-'; @@ -330,10 +347,15 @@ } } } else { - while (decpt-- > 0) - numbuf[i++] = cvt[j++]; - if (precision > 0) + while (decpt-- > 0) { + numbuf[i++] = j < cvt_len ? cvt[j++] : '0'; + } + if (precision > 0) { numbuf[i++] = decimal_point; + while (precision-- > 0) { + numbuf[i++] = j < cvt_len ? cvt[j++] : '0'; + } + } } } else if (fmt == 'e' || fmt == 'E') { char *exp_p; http://cvs.php.net/co.php/php-src/ext/standard/tests/strings/sprintf_f.phpt?r=1.1&p=1 Index: php-src/ext/standard/tests/strings/sprintf_f.phpt +++ php-src/ext/standard/tests/strings/sprintf_f.phpt --TEST-- sprintf %f --FILE-- <?php var_dump(sprintf("%3.2f", 1.2)); var_dump(sprintf("%-3.2f", 1.2)); var_dump(sprintf("%03.2f", 1.2)); var_dump(sprintf("%-03.2f", 1.2)); echo "\n"; var_dump(sprintf("%5.2f", 3.4)); var_dump(sprintf("%-5.2f", 3.4)); var_dump(sprintf("%05.2f", 3.4)); var_dump(sprintf("%-05.2f", 3.4)); echo "\n"; var_dump(sprintf("%7.2f", -5.6)); var_dump(sprintf("%-7.2f", -5.6)); var_dump(sprintf("%07.2f", -5.6)); var_dump(sprintf("%-07.2f", -5.6)); echo "\n"; var_dump(sprintf("%3.4f", 1.2345678e99)); ?> --EXPECTF-- string(4) "1.20" string(4) "1.20" string(4) "1.20" string(4) "1.20" string(5) " 3.40" string(5) "3.40 " string(5) "03.40" string(5) "3.400" string(7) " -5.60" string(7) "-5.60 " string(7) "-005.60" string(7) "-5.6000" string(105) "12345678%d00000000000000000000000000000000000000000000000000.0000" -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php