From f262bb350ec2d1d5909e264e3e39193eaa2aa522 Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Thu, 18 Jan 2024 08:54:39 +0700 Subject: [PATCH 1/2] fix formatting floating-point values without type and precision provided --- stl/inc/format | 10 +++++++++- .../tests/P0645R10_text_formatting_formatting/test.cpp | 9 +++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/stl/inc/format b/stl/inc/format index b0b97ca0575..622bcbca5f8 100644 --- a/stl/inc/format +++ b/stl/inc/format @@ -2967,6 +2967,7 @@ _NODISCARD _OutputIt _Fmt_write( auto _Format = chars_format::general; auto _Exponent = 'e'; auto _Precision = _Specs._Precision; + auto _None_Type = false; switch (_Specs._Type) { case 'A': @@ -3003,6 +3004,9 @@ _NODISCARD _OutputIt _Fmt_write( } _Format = chars_format::general; break; + default: + _None_Type = true; + break; } // Consider the powers of 2 in decimal: @@ -3043,7 +3047,11 @@ _NODISCARD _OutputIt _Fmt_write( _Result.ptr += 3; } else { if (_Precision == -1) { - _Result = _STD to_chars(_Buffer, _STD end(_Buffer), _Value, _Format); + if (_None_Type) { + _Result = _STD to_chars(_Buffer, _STD end(_Buffer), _Value); + } else { + _Result = _STD to_chars(_Buffer, _STD end(_Buffer), _Value, _Format); + } } else { _Result = _STD to_chars(_Buffer, _STD end(_Buffer), _Value, _Format, _Precision); } diff --git a/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp b/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp index 7dea640f249..f516e4735e7 100644 --- a/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp +++ b/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp @@ -1506,6 +1506,12 @@ constexpr bool test_format_string() { return true; } +// Also test GH-4319: incorrect output for some floating-point values +template +void test_gh_4319() { + assert(format(STR("{:}"), 12345678.0) == STR("12345678")); +} + void test() { test_simple_formatting(); test_simple_formatting(); @@ -1582,6 +1588,9 @@ void test() { test_localized_char(); test_localized_char(); test_localized_char(); + + test_gh_4319(); + test_gh_4319(); } int main() { From 3400d621ada9299ee89118a0fb977d092762e3af Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Thu, 18 Jan 2024 09:09:34 +0700 Subject: [PATCH 2/2] _None_Type => _None_type --- stl/inc/format | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stl/inc/format b/stl/inc/format index 622bcbca5f8..d5e6de99b86 100644 --- a/stl/inc/format +++ b/stl/inc/format @@ -2967,7 +2967,7 @@ _NODISCARD _OutputIt _Fmt_write( auto _Format = chars_format::general; auto _Exponent = 'e'; auto _Precision = _Specs._Precision; - auto _None_Type = false; + auto _None_type = false; switch (_Specs._Type) { case 'A': @@ -3005,7 +3005,7 @@ _NODISCARD _OutputIt _Fmt_write( _Format = chars_format::general; break; default: - _None_Type = true; + _None_type = true; break; } @@ -3047,7 +3047,7 @@ _NODISCARD _OutputIt _Fmt_write( _Result.ptr += 3; } else { if (_Precision == -1) { - if (_None_Type) { + if (_None_type) { _Result = _STD to_chars(_Buffer, _STD end(_Buffer), _Value); } else { _Result = _STD to_chars(_Buffer, _STD end(_Buffer), _Value, _Format);