Skip to content

Add support for %h and %H in printf() #5436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions ext/standard/formatted_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,17 +281,25 @@ php_sprintf_appenddouble(zend_string **buffer, size_t *pos,

case 'g':
case 'G':
case 'h':
case 'H':
{
if (precision == 0)
precision = 1;
/*
* * We use &num_buf[ 1 ], so that we have room for the sign
*/

char decimal_point = '.';
if (fmt == 'g' || fmt == 'G') {
#ifdef ZTS
localeconv_r(&lconv);
localeconv_r(&lconv);
#else
lconv = localeconv();
lconv = localeconv();
#endif
s = php_gcvt(number, precision, LCONV_DECIMAL_POINT, (fmt == 'G')?'E':'e', &num_buf[1]);
decimal_point = LCONV_DECIMAL_POINT;
}

char exp_char = fmt == 'G' || fmt == 'H' ? 'E' : 'e';
/* We use &num_buf[ 1 ], so that we have room for the sign. */
s = php_gcvt(number, precision, decimal_point, exp_char, &num_buf[1]);
is_negative = 0;
if (*s == '-') {
is_negative = 1;
Expand All @@ -303,6 +311,7 @@ php_sprintf_appenddouble(zend_string **buffer, size_t *pos,

s_len = strlen(s);
break;
}
}

php_sprintf_appendstring(buffer, pos, s, width, 0, padding,
Expand Down Expand Up @@ -562,12 +571,14 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n
width, padding, alignment);
break;

case 'g':
case 'G':
case 'e':
case 'E':
case 'f':
case 'F':
case 'g':
case 'G':
case 'h':
case 'H':
php_sprintf_appenddouble(&result, &outpos,
zval_get_double(tmp),
width, padding, alignment,
Expand Down
19 changes: 19 additions & 0 deletions ext/standard/tests/strings/printf_h_H.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
sprintf() %h and %H specifiers
--SKIPIF--
<?php
if (!setlocale(LC_ALL, "de_DE.utf8")) die("skip de_DE.utf8 locale not available");
?>
--FILE--
<?php

setlocale(LC_ALL, "de_DE.utf8");
$f = 1.25;
printf("%g %G %h %H\n", $f, $f, $f, $f);
$f = 0.00000125;
printf("%g %G %h %H\n", $f, $f, $f, $f);

?>
--EXPECT--
1,25 1,25 1.25 1.25
1,25e-6 1,25E-6 1.25e-6 1.25E-6