Fixup range formatting to match stdlib (#5005)

* Fixup range formatting to match stdlib

fmt would format a range of characters with `{:s}`
spec with surrounding quotes, whereas stdlib omits the quotes.
This patch drops the quotes.

* update docs
This commit is contained in:
Ivan Lazarić 2026-11-08 15:33:02 +01:00 committed by GitHub
parent 9c19ea4664
commit 32777676cf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 5 additions and 7 deletions

View File

@ -667,7 +667,7 @@ fmt::print("{}", std::vector{'h', 'e', 'l', 'l', 'o'});
fmt::print("{:n}", std::vector{'h', 'e', 'l', 'l', 'o'});
// Output: 'h', 'e', 'l', 'l', 'o'
fmt::print("{:s}", std::vector{'h', 'e', 'l', 'l', 'o'});
// Output: "hello"
// Output: hello
fmt::print("{:?s}", std::vector{'h', 'e', 'l', 'l', 'o', '\n'});
// Output: "hello\n"
fmt::print("{::}", std::vector{'h', 'e', 'l', 'l', 'o'});

View File

@ -467,7 +467,6 @@ struct range_formatter<
break;
case '?':
is_debug = true;
set_brackets({}, {});
++it;
if (it == end || *it != 's') report_error("invalid format specifier");
FMT_FALLTHROUGH;
@ -475,8 +474,7 @@ struct range_formatter<
if (!std::is_same<T, Char>::value)
report_error("invalid format specifier");
if (!is_debug) {
set_brackets(detail::string_literal<Char, '"'>{},
detail::string_literal<Char, '"'>{});
set_brackets({}, {});
set_separator({});
detail::maybe_set_debug_format(underlying_, false);
}

View File

@ -59,10 +59,10 @@ TEST(ranges_test, format_vector) {
auto vec = std::vector<char>{'a', '\n', '\t'};
auto vvc = std::vector<std::vector<char>>{vc, vc};
EXPECT_EQ(fmt::format("{}", vc), "['a', 'b', 'c']");
EXPECT_EQ(fmt::format("{:s}", vc), "\"abc\"");
EXPECT_EQ(fmt::format("{:s}", vc), "abc");
EXPECT_EQ(fmt::format("{:?s}", vec), "\"a\\n\\t\"");
EXPECT_EQ(fmt::format("{:s}", vec), "\"a\n\t\"");
EXPECT_EQ(fmt::format("{::s}", vvc), "[\"abc\", \"abc\"]");
EXPECT_EQ(fmt::format("{:s}", vec), "a\n\t");
EXPECT_EQ(fmt::format("{::s}", vvc), "[abc, abc]");
EXPECT_EQ(fmt::format("{}", vvc), "[['a', 'b', 'c'], ['a', 'b', 'c']]");
EXPECT_EQ(fmt::format("{:n}", vvc), "['a', 'b', 'c'], ['a', 'b', 'c']");
EXPECT_EQ(fmt::format("{:n:n}", vvc), "'a', 'b', 'c', 'a', 'b', 'c'");