Skip to content

Commit cdf6096

Browse files
authored
fix formatting floating-point values without type and precision provided (#4327)
1 parent c1e42b9 commit cdf6096

File tree

2 files changed

+18
-1
lines changed
  • stl/inc
  • tests/std/tests/P0645R10_text_formatting_formatting

2 files changed

+18
-1
lines changed

stl/inc/format

+9-1
Original file line numberDiff line numberDiff line change
@@ -2967,6 +2967,7 @@ _NODISCARD _OutputIt _Fmt_write(
29672967
auto _Format = chars_format::general;
29682968
auto _Exponent = 'e';
29692969
auto _Precision = _Specs._Precision;
2970+
auto _None_type = false;
29702971

29712972
switch (_Specs._Type) {
29722973
case 'A':
@@ -3003,6 +3004,9 @@ _NODISCARD _OutputIt _Fmt_write(
30033004
}
30043005
_Format = chars_format::general;
30053006
break;
3007+
default:
3008+
_None_type = true;
3009+
break;
30063010
}
30073011

30083012
// Consider the powers of 2 in decimal:
@@ -3043,7 +3047,11 @@ _NODISCARD _OutputIt _Fmt_write(
30433047
_Result.ptr += 3;
30443048
} else {
30453049
if (_Precision == -1) {
3046-
_Result = _STD to_chars(_Buffer, _STD end(_Buffer), _Value, _Format);
3050+
if (_None_type) {
3051+
_Result = _STD to_chars(_Buffer, _STD end(_Buffer), _Value);
3052+
} else {
3053+
_Result = _STD to_chars(_Buffer, _STD end(_Buffer), _Value, _Format);
3054+
}
30473055
} else {
30483056
_Result = _STD to_chars(_Buffer, _STD end(_Buffer), _Value, _Format, _Precision);
30493057
}

tests/std/tests/P0645R10_text_formatting_formatting/test.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -1506,6 +1506,12 @@ constexpr bool test_format_string() {
15061506
return true;
15071507
}
15081508

1509+
// Also test GH-4319: incorrect output for some floating-point values
1510+
template <class charT>
1511+
void test_gh_4319() {
1512+
assert(format(STR("{:}"), 12345678.0) == STR("12345678"));
1513+
}
1514+
15091515
void test() {
15101516
test_simple_formatting<char>();
15111517
test_simple_formatting<wchar_t>();
@@ -1582,6 +1588,9 @@ void test() {
15821588
test_localized_char<char, char>();
15831589
test_localized_char<wchar_t, char>();
15841590
test_localized_char<wchar_t, wchar_t>();
1591+
1592+
test_gh_4319<char>();
1593+
test_gh_4319<wchar_t>();
15851594
}
15861595

15871596
int main() {

0 commit comments

Comments
 (0)