Drop temporary buffer for custom formatter
This commit is contained in:
parent
01ff781d92
commit
877e80e947
3 changed files with 25 additions and 38 deletions
|
@ -6,7 +6,7 @@ on:
|
|||
- "justfile"
|
||||
|
||||
env:
|
||||
FEDORA_VERSION: 40
|
||||
FEDORA_VERSION: 41
|
||||
DEPENDENCIES: >-
|
||||
'pkgconfig(CLI11)'
|
||||
'pkgconfig(re2)'
|
||||
|
|
|
@ -1 +1 @@
|
|||
0.17
|
||||
0.18
|
||||
|
|
59
src/proc.cc
59
src/proc.cc
|
@ -128,45 +128,32 @@ struct std::formatter<AppConfig> {
|
|||
static constexpr auto parse(std::format_parse_context& ctx) { return ctx.begin(); }
|
||||
|
||||
static auto format(AppConfig const& cfg, format_context& ctx) {
|
||||
auto const digits =
|
||||
total_digits10<decltype(cfg.tps), decltype(cfg.nproc), decltype(cfg.interval),
|
||||
decltype(cfg.grace_period), decltype(cfg.threshold)>();
|
||||
auto out = ctx.out();
|
||||
|
||||
std::size_t max_size =
|
||||
rn::fold_left(cfg.pattern, 256ZU,
|
||||
[](std::size_t acc, std::string_view v) { return acc + v.size(); }) +
|
||||
digits;
|
||||
// Write initial opening
|
||||
out = std::format_to(out, "AppConfig {{\n pattern: ");
|
||||
|
||||
DEBUG_PRINTF("Reserved: {}\n"sv, max_size);
|
||||
// Pattern list with exact same formatting as your version
|
||||
out = std::format_to(out, "{{\n");
|
||||
for (auto const& pattern : cfg.pattern) {
|
||||
out = std::copy_n(" ", 4, out);
|
||||
out = rn::copy(pattern, out).out;
|
||||
out = std::copy_n(",\n", 2, out);
|
||||
}
|
||||
out = std::copy_n(" }", 3, out);
|
||||
|
||||
std::string buf{};
|
||||
buf.resize_and_overwrite(max_size, [&cfg, max_size](char* data, std::size_t) {
|
||||
auto* ptr = data;
|
||||
|
||||
ptr = std::copy_n("{\n", 2, ptr);
|
||||
for (auto const& pattern : cfg.pattern) {
|
||||
ptr = std::copy_n(" ", 4, ptr);
|
||||
ptr = rn::copy(pattern, ptr).out;
|
||||
ptr = std::copy_n(",\n", 2, ptr);
|
||||
}
|
||||
ptr = std::copy_n(" }", 3, ptr);
|
||||
|
||||
auto total = static_cast<std::size_t>(ptr - data);
|
||||
ALWAYS_ASSERT(total <= max_size);
|
||||
|
||||
return total;
|
||||
});
|
||||
|
||||
return std::format_to(ctx.out(),
|
||||
"AppConfig {{\n pattern: {},\n"sv
|
||||
" cycles: {},\n"sv
|
||||
" threshold: {},\n"sv
|
||||
" interval: {},\n"sv
|
||||
" grace_period: {},\n"sv
|
||||
" nproc: {},\n"sv
|
||||
" tps: {}\n}};"sv,
|
||||
buf, cfg.cycles, cfg.threshold, cfg.interval, cfg.grace_period,
|
||||
cfg.nproc, cfg.tps);
|
||||
// Rest identical to your version
|
||||
return std::format_to(out,
|
||||
",\n"
|
||||
" cycles: {},\n"
|
||||
" threshold: {},\n"
|
||||
" interval: {},\n"
|
||||
" grace_period: {},\n"
|
||||
" nproc: {},\n"
|
||||
" tps: {}\n"
|
||||
"}};"sv,
|
||||
cfg.cycles, cfg.threshold, cfg.interval, cfg.grace_period, cfg.nproc,
|
||||
cfg.tps);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue