bjori Mon Dec 18 09:26:54 2006 UTC
Added files: (Branch: PHP_5_2)
/php-src/ext/standard/tests/strings sprintf_f_2.phpt
sprintf_f_3.phpt
Modified files:
/php-src/ext/standard formatted_print.c
/php-src/main snprintf.c
Log:
MFH:
- make %f locale aware again
- fix precision loss with %F (noticed by Sebastian Nohn)
- add new tests
http://cvs.php.net/viewvc.cgi/php-src/ext/standard/formatted_print.c?r1=1.82.2.1.2.5&r2=1.82.2.1.2.6&diff_format=u
Index: php-src/ext/standard/formatted_print.c
diff -u php-src/ext/standard/formatted_print.c:1.82.2.1.2.5
php-src/ext/standard/formatted_print.c:1.82.2.1.2.6
--- php-src/ext/standard/formatted_print.c:1.82.2.1.2.5 Thu Dec 7 20:45:58 2006
+++ php-src/ext/standard/formatted_print.c Mon Dec 18 09:26:54 2006
@@ -16,7 +16,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: formatted_print.c,v 1.82.2.1.2.5 2006/12/07 20:45:58 tony2001 Exp $ */
+/* $Id: formatted_print.c,v 1.82.2.1.2.6 2006/12/18 09:26:54 bjori Exp $ */
#include <math.h> /* modf() */
#include "php.h"
@@ -222,15 +222,13 @@
}
switch (fmt) {
- case 'F':
- fmt = 'f';
- /* break is missing */
case 'e':
if (precision) {
precision--;
}
case 'E':
case 'f':
+ case 'F':
s = ap_php_conv_fp(fmt, number, 0, precision,
&is_negative, &num_buf[1],
&s_len);
if (is_negative) {
http://cvs.php.net/viewvc.cgi/php-src/main/snprintf.c?r1=1.37.2.4.2.2&r2=1.37.2.4.2.3&diff_format=u
Index: php-src/main/snprintf.c
diff -u php-src/main/snprintf.c:1.37.2.4.2.2
php-src/main/snprintf.c:1.37.2.4.2.3
--- php-src/main/snprintf.c:1.37.2.4.2.2 Wed Dec 6 09:52:51 2006
+++ php-src/main/snprintf.c Mon Dec 18 09:26:54 2006
@@ -16,7 +16,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: snprintf.c,v 1.37.2.4.2.2 2006/12/06 09:52:51 tony2001 Exp $ */
+/* $Id: snprintf.c,v 1.37.2.4.2.3 2006/12/18 09:26:54 bjori Exp $ */
#include "php.h"
@@ -363,12 +363,20 @@
register char *s = buf;
register char *p, *p_orig;
int decimal_point;
+ char dec_point = '.';
+
+ if (format == 'f') {
+ struct lconv *lconv;
+ lconv = localeconv();
+ dec_point = *lconv->decimal_point;
+ format = 'F';
+ }
if (precision >= NDIG - 1) {
precision = NDIG - 2;
}
- if (format == 'f')
+ if (format == 'F')
p_orig = p = bsd_fcvt(num, precision, &decimal_point,
is_negative);
else /* either e or E format
*/
p_orig = p = bsd_ecvt(num, precision + 1, &decimal_point,
is_negative);
@@ -383,16 +391,16 @@
free(p_orig);
return (buf);
}
- if (format == 'f') {
+ if (format == 'F') {
if (decimal_point <= 0) {
if (num != 0 || precision > 0) {
*s++ = '0';
if (precision > 0) {
- *s++ = '.';
+ *s++ = dec_point;
while (decimal_point++ < 0)
*s++ = '0';
} else if (add_dp) {
- *s++ = '.';
+ *s++ = dec_point;
}
}
} else {
@@ -405,7 +413,7 @@
*s++ = '0';
}
if (precision > 0 || add_dp) {
- *s++ = '.';
+ *s++ = dec_point;
}
}
} else {
@@ -420,7 +428,7 @@
while (*p)
*s++ = *p++;
- if (format != 'f') {
+ if (format != 'F') {
char temp[EXPONENT_LENGTH]; /* for exponent
conversion */
int t_len;
bool_int exponent_is_negative;
http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/sprintf_f_2.phpt?view=markup&rev=1.1
Index: php-src/ext/standard/tests/strings/sprintf_f_2.phpt
+++ php-src/ext/standard/tests/strings/sprintf_f_2.phpt
--TEST--
sprintf #2
--FILE--
<?php
var_dump(sprintf("%.3F", 100.426));
var_dump(sprintf("%.2F", 100.426));
var_dump(sprintf("%d", 100.426));
var_dump(sprintf("%d", 100.9));
var_dump(sprintf("%o", 100.426));
var_dump(sprintf("%o", 100.9));
/* copy & paste from the docs */
/* example#1: Argument swapping */
$num = 100.1;
$location = "world";
$format = 'There are %d monkeys in the %s';
var_dump(sprintf($format, $num, $location));
/* example#2: Argument swapping */
$format = 'The %s contains %d monkeys';
var_dump(sprintf($format, $num, $location));
/* example#3: Argument swapping */
$format = 'The %2$s contains %1$d monkeys';
var_dump(sprintf($format, $num, $location));
/* example#4: Argument swapping */
$format = 'The %2$s contains %1$d monkeys.
That\'s a nice %2$s full of %1$d monkeys.';
var_dump(sprintf($format, $num, $location));
/* example#5: various examples */
$n = 43951789;
$u = -43951789;
$c = 65; // ASCII 65 is 'A'
// notice the double %%, this prints a literal '%' character
var_dump(sprintf("%%b = '%b'", $n)); // binary representation
var_dump(sprintf("%%c = '%c'", $c)); // print the ascii character, same as
chr() function
var_dump(sprintf("%%d = '%d'", $n)); // standard integer representation
var_dump(sprintf("%%e = '%e'", $n)); // scientific notation
var_dump(sprintf("%%u = '%u'", $n)); // unsigned integer representation of a
positive integer
var_dump(sprintf("%%u = '%u'", $u)); // unsigned integer representation of a
negative integer
var_dump(sprintf("%%f = '%f'", $n)); // floating point representation
var_dump(sprintf("%%o = '%o'", $n)); // octal representation
var_dump(sprintf("%%s = '%s'", $n)); // string representation
var_dump(sprintf("%%x = '%x'", $n)); // hexadecimal representation (lower-case)
var_dump(sprintf("%%X = '%X'", $n)); // hexadecimal representation (upper-case)
var_dump(sprintf("%%+d = '%+d'", $n)); // sign specifier on a positive integer
var_dump(sprintf("%%+d = '%+d'", $u)); // sign specifier on a negative integer
/* example#6: string specifiers */
$s = 'monkey';
$t = 'many monkeys';
var_dump(sprintf("[%s]", $s)); // standard string output
var_dump(sprintf("[%10s]", $s)); // right-justification with spaces
var_dump(sprintf("[%-10s]", $s)); // left-justification with spaces
var_dump(sprintf("[%010s]", $s)); // zero-padding works on strings too
var_dump(sprintf("[%'#10s]", $s)); // use the custom padding character '#'
var_dump(sprintf("[%10.10s]", $t)); // left-justification but with a cutoff of
10 characters
/* example#7: zero-padded integers */
var_dump(sprintf("%04d-%02d-%02d", 2006, 12, 18));
/* example#8: formatting currency */
$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
var_dump(sprintf("%01.2f", $money)); // output "123.10"
/* example#9: scientific notation */
$number = 362525200;
var_dump(sprintf("%.3e", $number)); // outputs 3.63e+8
?>
--EXPECT--
string(7) "100.426"
string(6) "100.43"
string(3) "100"
string(3) "100"
string(3) "144"
string(3) "144"
string(34) "There are 100 monkeys in the world"
string(28) "The 100.1 contains 0 monkeys"
string(30) "The world contains 100 monkeys"
string(76) "The world contains 100 monkeys.
That's a nice world full of 100 monkeys."
string(33) "%b = '10100111101010011010101101'"
string(8) "%c = 'A'"
string(15) "%d = '43951789'"
string(17) "%e = '4.39518e+7'"
string(15) "%u = '43951789'"
string(17) "%u = '4251015507'"
string(22) "%f = '43951789.000000'"
string(16) "%o = '247523255'"
string(15) "%s = '43951789'"
string(14) "%x = '29ea6ad'"
string(14) "%X = '29EA6AD'"
string(17) "%+d = '+43951789'"
string(17) "%+d = '-43951789'"
string(8) "[monkey]"
string(12) "[ monkey]"
string(12) "[monkey ]"
string(12) "[0000monkey]"
string(12) "[####monkey]"
string(12) "[many monke]"
string(10) "2006-12-18"
string(6) "123.10"
string(7) "3.63e+8"
http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/sprintf_f_3.phpt?view=markup&rev=1.1
Index: php-src/ext/standard/tests/strings/sprintf_f_3.phpt
+++ php-src/ext/standard/tests/strings/sprintf_f_3.phpt
--TEST--
sprintf #2
--INI--
error_reporting=6143
--SKIPIF--
<?php if(false == setlocale(LC_NUMERIC, "is_IS", "is_IS.UTF-8")) print "skip
icelandic locale not supported"; ?>
--FILE--
<?php
setlocale(LC_NUMERIC, "is_IS", "is_IS.UTF-8");
var_dump(sprintf("%.3f", 100.426));
var_dump(sprintf("%.2f", 100.426));
var_dump(sprintf("%f'", 100.426));
$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
var_dump(sprintf("%01.2f", $money));
var_dump(sprintf("%.3e", $money));
?>
--EXPECT--
string(7) "100,426"
string(6) "100,43"
string(11) "100,426000'"
string(6) "123,10"
string(7) "1.23e+2"
--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php