Limiting total size of result with C++20 fmt

Viewed 379

I know I can limit the size of a particular string argument, but IDK how to do it for the entire output. In other words can this program be written with only one call to format?

#include <string>
#include <iostream>
#include <fmt/format.h>

int LOG_MAX_MESSAGE_LENGTH = 11;

void f(const char* p) {
    std::cout << fmt::format("{:.{}}", fmt::format("ABI is {}", p),
                             LOG_MAX_MESSAGE_LENGTH)
              << std::endl;
}

int main() {
    f("hellooooo");
}
1 Answers

There's also format_to_n, which takes an output iterator and a size (and then the usual format string and arguments).

format_to_n(
    std::ostream_iterator(std::cout), LOG_MAX_MESSAGE_LENGTH,
    "ABI is {}", p);
Related