[libcxx-commits] [libcxx] [libc++][format] Propagate `m` when formatting range elements (PR #94562)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Wed Jun 5 20:37:06 PDT 2024


https://github.com/frederick-vs-ja updated https://github.com/llvm/llvm-project/pull/94562

>From f7eacf857fdc6b90c500e103d85392fbd54f1119 Mon Sep 17 00:00:00 2001
From: "A. Jiang" <de34 at live.cn>
Date: Thu, 6 Jun 2024 11:36:10 +0800
Subject: [PATCH] [libc++][format] Propagate `m` when formatting range elements

As per [tab:formatter.range.type], the effects of the `m` option need
to be propagated to the formatter of range elements.
---
 libcxx/include/__format/range_formatter.h                   | 2 ++
 .../format.range.fmtset/format.functions.tests.h            | 6 +++---
 .../format.range.formatter/format.functions.tests.h         | 6 +++---
 3 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/libcxx/include/__format/range_formatter.h b/libcxx/include/__format/range_formatter.h
index 6915630743493..ee94acc71776a 100644
--- a/libcxx/include/__format/range_formatter.h
+++ b/libcxx/include/__format/range_formatter.h
@@ -214,6 +214,8 @@ struct _LIBCPP_TEMPLATE_VIS range_formatter {
     switch (*__begin) {
     case _CharT('m'):
       if constexpr (__fmt_pair_like<_Tp>) {
+        __underlying_.set_separator(_LIBCPP_STATICALLY_WIDEN(_CharT, ": "));
+        __underlying_.set_brackets({}, {});
         set_brackets(_LIBCPP_STATICALLY_WIDEN(_CharT, "{"), _LIBCPP_STATICALLY_WIDEN(_CharT, "}"));
         set_separator(_LIBCPP_STATICALLY_WIDEN(_CharT, ", "));
         ++__begin;
diff --git a/libcxx/test/std/utilities/format/format.range/format.range.fmtset/format.functions.tests.h b/libcxx/test/std/utilities/format/format.range/format.range.fmtset/format.functions.tests.h
index 5a0c89ecccfff..c7a2873798d24 100644
--- a/libcxx/test/std/utilities/format/format.range/format.range.fmtset/format.functions.tests.h
+++ b/libcxx/test/std/utilities/format/format.range/format.range.fmtset/format.functions.tests.h
@@ -1175,10 +1175,10 @@ void test_pair_tuple(TestFunction check, ExceptionTest check_exception, auto&& i
 
   // *** n
   check(SV("__(1, 'a'), (42, '*')___"), SV("{:_^24n}"), input);
-  check(SV("__(1, 'a'), (42, '*')___"), SV("{:_^24nm}"), input); // m should have no effect
+  check(SV("____1: 'a', 42: '*'_____"), SV("{:_^24nm}"), input);
 
   // *** type ***
-  check(SV("__{(1, 'a'), (42, '*')}___"), SV("{:_^26m}"), input);
+  check(SV("____{1: 'a', 42: '*'}_____"), SV("{:_^26m}"), input);
   check_exception("Type s requires character type as formatting argument", SV("{:s}"), input);
   check_exception("Type ?s requires character type as formatting argument", SV("{:?s}"), input);
 
@@ -1239,7 +1239,7 @@ void test_pair_tuple(TestFunction check, ExceptionTest check_exception, auto&& i
   check(SV("1: 'a', 42: '*'"), SV("{:n:m}"), input);
   check(SV("1, 'a', 42, '*'"), SV("{:n:n}"), input);
   check(SV("{1: 'a', 42: '*'}"), SV("{:m:m}"), input);
-  check(SV("{1, 'a', 42, '*'}"), SV("{:m:n}"), input);
+  check(SV("{1: 'a', 42: '*'}"), SV("{:m:n}"), input);
 }
 
 template <class CharT, class TestFunction, class ExceptionTest>
diff --git a/libcxx/test/std/utilities/format/format.range/format.range.formatter/format.functions.tests.h b/libcxx/test/std/utilities/format/format.range/format.range.formatter/format.functions.tests.h
index 78b067e8cffa9..3d4a5db09b70f 100644
--- a/libcxx/test/std/utilities/format/format.range/format.range.formatter/format.functions.tests.h
+++ b/libcxx/test/std/utilities/format/format.range/format.range.formatter/format.functions.tests.h
@@ -993,10 +993,10 @@ void test_pair_tuple(TestFunction check, ExceptionTest check_exception, auto&& i
 
   // *** n
   check(SV("__(1, 'a'), (42, '*')___"), SV("{:_^24n}"), input);
-  check(SV("__(1, 'a'), (42, '*')___"), SV("{:_^24nm}"), input); // m should have no effect
+  check(SV("____1: 'a', 42: '*'_____"), SV("{:_^24nm}"), input);
 
   // *** type ***
-  check(SV("__{(1, 'a'), (42, '*')}___"), SV("{:_^26m}"), input);
+  check(SV("____{1: 'a', 42: '*'}_____"), SV("{:_^26m}"), input);
   check_exception("Type s requires character type as formatting argument", SV("{:s}"), input);
   check_exception("Type ?s requires character type as formatting argument", SV("{:?s}"), input);
   for (std::basic_string_view<CharT> fmt : fmt_invalid_types<CharT>("s"))
@@ -1053,7 +1053,7 @@ void test_pair_tuple(TestFunction check, ExceptionTest check_exception, auto&& i
   check(SV("1: 'a', 42: '*'"), SV("{:n:m}"), input);
   check(SV("1, 'a', 42, '*'"), SV("{:n:n}"), input);
   check(SV("{1: 'a', 42: '*'}"), SV("{:m:m}"), input);
-  check(SV("{1, 'a', 42, '*'}"), SV("{:m:n}"), input);
+  check(SV("{1: 'a', 42: '*'}"), SV("{:m:n}"), input);
 }
 
 template <class CharT, class TestFunction, class ExceptionTest>



More information about the libcxx-commits mailing list