Skip to content

Commit

Permalink
Merge branch 'fmtlib:master' into zig-pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane authored Sep 11, 2023
2 parents b141926 + d498754 commit 63d6a99
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
2 changes: 0 additions & 2 deletions include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,6 @@ template <typename CodeUnit> struct codecvt_result {
CodeUnit buf[max_size];
CodeUnit* end;
};
template <typename CodeUnit>
constexpr const size_t codecvt_result<CodeUnit>::max_size;

template <typename CodeUnit>
void write_codecvt(codecvt_result<CodeUnit>& out, string_view in_buf,
Expand Down
3 changes: 2 additions & 1 deletion include/fmt/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,10 @@ template <typename Char> inline void reset_color(buffer<Char>& buffer) {
buffer.append(reset_color.begin(), reset_color.end());
}

template <typename T> struct styled_arg {
template <typename T> struct styled_arg : detail::view {
const T& value;
text_style style;
styled_arg(const T& v, text_style s) : value(v), style(s) {}
};

template <typename Char>
Expand Down
32 changes: 32 additions & 0 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -4198,6 +4198,38 @@ template <typename T> struct formatter<group_digits_view<T>> : formatter<T> {
}
};

template <typename T>
struct nested_view {
const formatter<T>* fmt;
const T* value;
};

template <typename T>
struct formatter<nested_view<T>> {
FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> const char* {
return ctx.begin();
}
auto format(nested_view<T> view, format_context& ctx) const
-> decltype(ctx.out()) {
return view.fmt->format(*view.value, ctx);
}
};

template <typename T>
struct nested_formatter {
private:
formatter<T> formatter_;

public:
FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> const char* {
return formatter_.parse(ctx);
}

auto nested(const T& value) const -> nested_view<T> {
return nested_view<T>{&formatter_, &value};
}
};

// DEPRECATED! join_view will be moved to ranges.h.
template <typename It, typename Sentinel, typename Char = char>
struct join_view : detail::view {
Expand Down
17 changes: 17 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1775,6 +1775,23 @@ TEST(format_test, group_digits_view) {
EXPECT_EQ(fmt::format("{:8}", fmt::group_digits(1000)), " 1,000");
}

struct point {
double x, y;
};

FMT_BEGIN_NAMESPACE
template <>
struct formatter<point> : nested_formatter<double> {
auto format(point p, format_context& ctx) const -> decltype(ctx.out()) {
return format_to(ctx.out(), "({}, {})", nested(p.x), nested(p.y));
}
};
FMT_END_NAMESPACE

TEST(format_test, nested_formatter) {
EXPECT_EQ(fmt::format("{:.2f}", point{1, 2}), "(1.00, 2.00)");
}

enum test_enum { foo, bar };
auto format_as(test_enum e) -> int { return e; }

Expand Down

0 comments on commit 63d6a99

Please sign in to comment.