From ef31a40a2d9d2ae2a10ec38a5d879654f7b5853a Mon Sep 17 00:00:00 2001 From: Muhammad Al-Tahhan Date: Fri, 22 Dec 2023 10:35:05 +0300 Subject: [PATCH 1/3] ``: Fix `formatter` ignoring dynamically provided width --- stl/inc/chrono | 9 ++++++++- tests/std/test.lst | 1 + .../std/tests/GH_004201_chrono_formatter/env.lst | 4 ++++ .../std/tests/GH_004201_chrono_formatter/test.cpp | 15 +++++++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/std/tests/GH_004201_chrono_formatter/env.lst create mode 100644 tests/std/tests/GH_004201_chrono_formatter/test.cpp diff --git a/stl/inc/chrono b/stl/inc/chrono index 34f10386d07..f4274aec8a7 100644 --- a/stl/inc/chrono +++ b/stl/inc/chrono @@ -5477,7 +5477,14 @@ namespace chrono { int _Estimated_width = -1; (void) _Measure_string_prefix(_Stream.view(), _Estimated_width); - return _Write_aligned(_STD move(_FormatCtx.out()), _Estimated_width, _Specs, _Fmt_align::_Left, + + auto _FormatSpecs = _Specs; + if (_Specs._Dynamic_width_index >= 0) { + _FormatSpecs._Width = _Get_dynamic_specs<_Width_checker>( + _FormatCtx.arg(static_cast(_Specs._Dynamic_width_index))); + } + + return _Write_aligned(_STD move(_FormatCtx.out()), _Estimated_width, _FormatSpecs, _Fmt_align::_Left, [&](auto _Out) { return _Fmt_write(_STD move(_Out), _Stream.view()); }); } diff --git a/tests/std/test.lst b/tests/std/test.lst index ee4fa316ece..cb53ebe3e84 100644 --- a/tests/std/test.lst +++ b/tests/std/test.lst @@ -234,6 +234,7 @@ tests\GH_003840_tellg_when_reading_lf_file_in_text_mode tests\GH_003867_output_nan tests\GH_004023_mdspan_fwd_prod_overflow tests\GH_004040_container_nonmember_functions +tests\GH_004201_chrono_formatter tests\LWG2381_num_get_floating_point tests\LWG2597_complex_branch_cut tests\LWG3018_shared_ptr_function diff --git a/tests/std/tests/GH_004201_chrono_formatter/env.lst b/tests/std/tests/GH_004201_chrono_formatter/env.lst new file mode 100644 index 00000000000..d6d824b5879 --- /dev/null +++ b/tests/std/tests/GH_004201_chrono_formatter/env.lst @@ -0,0 +1,4 @@ +# Copyright (c) Microsoft Corporation. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +RUNALL_INCLUDE ..\concepts_20_matrix.lst diff --git a/tests/std/tests/GH_004201_chrono_formatter/test.cpp b/tests/std/tests/GH_004201_chrono_formatter/test.cpp new file mode 100644 index 00000000000..eb531f2a592 --- /dev/null +++ b/tests/std/tests/GH_004201_chrono_formatter/test.cpp @@ -0,0 +1,15 @@ +#include +#include +#include +#include +#include +#include + +using namespace std::literals::chrono_literals; + +int main() { + assert(std::format("[{:20%T}]", 314159s) == "[87:15:59 ]"); + + // std::formatter specializations for types used to ignore dynamically provided width + assert(std::format("[{:{}%T}]", 314159s, 20) == "[87:15:59 ]"); +} From b7b0f5b7b9edea0ed0d3167638c71009e2c0f84d Mon Sep 17 00:00:00 2001 From: Muhammad Al-Tahhan Date: Fri, 22 Dec 2023 15:16:09 +0300 Subject: [PATCH 2/3] Fix the formatting of the test file --- tests/std/tests/GH_004201_chrono_formatter/test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/std/tests/GH_004201_chrono_formatter/test.cpp b/tests/std/tests/GH_004201_chrono_formatter/test.cpp index eb531f2a592..d8ee387725e 100644 --- a/tests/std/tests/GH_004201_chrono_formatter/test.cpp +++ b/tests/std/tests/GH_004201_chrono_formatter/test.cpp @@ -1,9 +1,9 @@ +#include #include #include #include #include #include -#include using namespace std::literals::chrono_literals; From 13b5d93a408e4a5b6ce1f21bef875c277078c267 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Wed, 10 Jan 2024 18:24:21 -0800 Subject: [PATCH 3/3] Code review feedback. --- stl/inc/chrono | 6 +++--- tests/std/tests/GH_004201_chrono_formatter/test.cpp | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/stl/inc/chrono b/stl/inc/chrono index f4274aec8a7..982be0a92d1 100644 --- a/stl/inc/chrono +++ b/stl/inc/chrono @@ -5478,13 +5478,13 @@ namespace chrono { int _Estimated_width = -1; (void) _Measure_string_prefix(_Stream.view(), _Estimated_width); - auto _FormatSpecs = _Specs; + auto _Format_specs = _Specs; if (_Specs._Dynamic_width_index >= 0) { - _FormatSpecs._Width = _Get_dynamic_specs<_Width_checker>( + _Format_specs._Width = _Get_dynamic_specs<_Width_checker>( _FormatCtx.arg(static_cast(_Specs._Dynamic_width_index))); } - return _Write_aligned(_STD move(_FormatCtx.out()), _Estimated_width, _FormatSpecs, _Fmt_align::_Left, + return _Write_aligned(_STD move(_FormatCtx.out()), _Estimated_width, _Format_specs, _Fmt_align::_Left, [&](auto _Out) { return _Fmt_write(_STD move(_Out), _Stream.view()); }); } diff --git a/tests/std/tests/GH_004201_chrono_formatter/test.cpp b/tests/std/tests/GH_004201_chrono_formatter/test.cpp index d8ee387725e..2b3e1096214 100644 --- a/tests/std/tests/GH_004201_chrono_formatter/test.cpp +++ b/tests/std/tests/GH_004201_chrono_formatter/test.cpp @@ -1,9 +1,10 @@ +// Copyright (c) Microsoft Corporation. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + #include #include #include -#include #include -#include using namespace std::literals::chrono_literals;