Skip to content

Commit

Permalink
Merge branch 'upstream' of github.com:kassane/fmt into zig-pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Oct 5, 2023
2 parents 8c07080 + f76603f commit 79ad694
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/scorecard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:

steps:
- name: "Checkout code"
uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0
uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
with:
persist-credentials: false

Expand Down
2 changes: 2 additions & 0 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -4215,6 +4215,8 @@ template <typename T> struct nested_formatter {
formatter<T> formatter_;

public:
constexpr nested_formatter() : width_(0), align_(align_t::none) {}

FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> const char* {
auto specs = detail::dynamic_format_specs<char>();
auto it = parse_format_specs(ctx.begin(), ctx.end(), specs, ctx,
Expand Down
4 changes: 4 additions & 0 deletions include/fmt/printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ template <typename Char> class basic_printf_context {
detail::buffer_appender<Char> out_;
basic_format_args<basic_printf_context> args_;

static_assert(
std::is_same<Char, char>::value || std::is_same<Char, wchar_t>::value,
"Unsupported code unit type.");

public:
using char_type = Char;
using parse_context_type = basic_format_parse_context<Char>;
Expand Down
26 changes: 26 additions & 0 deletions include/fmt/std.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,32 @@ FMT_END_NAMESPACE
#endif

FMT_BEGIN_NAMESPACE
FMT_EXPORT
template <std::size_t N, typename Char>
struct formatter<std::bitset<N>, Char> : nested_formatter<string_view> {
private:
// Functor because C++11 doesn't support generic lambdas.
struct writer {
const std::bitset<N>& bs;

template <typename OutputIt>
FMT_CONSTEXPR OutputIt operator()(OutputIt out) {
for (auto pos = N; pos > 0; --pos) {
out = detail::write<Char>(out, bs[pos - 1] ? Char('1') : Char('0'));
}

return out;
}
};

public:
template <typename FormatContext>
auto format(const std::bitset<N>& bs, FormatContext& ctx) const
-> decltype(ctx.out()) {
return write_padded(ctx, writer{bs});
}
};

FMT_EXPORT
template <typename Char>
struct formatter<std::thread::id, Char> : basic_ostream_formatter<Char> {};
Expand Down
2 changes: 0 additions & 2 deletions src/fmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ module;
# if defined(__GLIBCXX__)
# include <ext/stdio_filebuf.h>
# include <ext/stdio_sync_filebuf.h>
# elif defined(_LIBCPP_VERSION)
# include <__std_stream>
# endif
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
Expand Down
8 changes: 8 additions & 0 deletions test/std-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,14 @@ TEST(std_test, format_const_bit_reference) {
EXPECT_EQ(fmt::format("{} {}", v[0], v[1]), "true false");
}

TEST(std_test, format_bitset) {
const std::bitset<6> bs(42);
EXPECT_EQ(fmt::format("{}", bs), "101010");
EXPECT_EQ(fmt::format("{:.4}", bs), "101010");
EXPECT_EQ(fmt::format("{:0>8}", bs), "00101010");
EXPECT_EQ(fmt::format("{:-^12}", bs), "---101010---");
}

TEST(std_test, format_atomic) {
std::atomic<bool> b(false);
EXPECT_EQ(fmt::format("{}", b), "false");
Expand Down

0 comments on commit 79ad694

Please sign in to comment.