12namespace CppClingo::Util {
33 if (out_ !=
nullptr) {
34 fwrite(buf_.data(),
sizeof(
char), size_, out_);
44 constexpr auto n = 8192;
45 if (out_ !=
nullptr && size_ > n) {
46 fwrite(buf_.data(),
sizeof(
char), size_, out_);
52 [[nodiscard]]
auto size() const ->
size_t {
return static_cast<size_t>(size_); }
55 [[nodiscard]]
auto empty() const ->
bool {
return size_ == 0; }
58 [[nodiscard]]
auto view() const -> std::string_view {
59 return std::string_view{buf_.data(),
static_cast<size_t>(size_)};
63 [[nodiscard]]
auto str() const -> std::
string {
return std::string{buf_.data(),
static_cast<size_t>(size_)}; }
66 [[nodiscard]]
auto c_str() ->
char const * {
80 buf_.emplace_back(
'\0');
81 auto ret = std::move(buf_);
89 auto n =
static_cast<std::ptrdiff_t
>(std::strlen(
str));
90 std::copy(
str, std::next(
str, n), ensure_(n));
96 auto n =
static_cast<std::ptrdiff_t
>(
str.length());
97 std::ranges::copy(
str, ensure_(n));
111 template <std::
integral T>
void append(T num) {
112 constexpr auto n = 128;
113 auto *end = ensure_(n);
114 auto res = std::to_chars(end, limit_(), num);
115 size_ += res.ptr - end;
121 auto reserve(std::ptrdiff_t n) -> std::span<char> {
122 auto *begin = ensure_(n);
124 return {begin, std::next(begin, n)};
131 auto sp = std::span{buf_.data(),
static_cast<size_t>(size_)};
133 auto ib = sp.begin() + (size_ - len);
134 auto it = std::find(ib, ie,
'\0');
158 static constexpr std::ptrdiff_t n = 32;
159 auto *begin = out.ensure_(n);
160 auto *end = std::next(begin, n);
161 auto [res, ec] = std::to_chars(begin, end, value);
162 out.size_ += std::distance(begin, res);
173 auto limit_() ->
char * {
return std::next(buf_.data(),
static_cast<std::ptrdiff_t
>(buf_.size())); }
175 auto ensure_(std::ptrdiff_t n) ->
char * {
177 assert(n >= 0 && m >= 0);
178 if (buf_.size() <
static_cast<size_t>(m)) {
179 buf_.reserve(2 *
static_cast<size_t>(m));
180 buf_.resize(buf_.capacity());
182 return std::next(buf_.data(), size_);
185 std::vector<char> buf_;
186 std::ptrdiff_t size_ = 0;
195 template <
class Out>
void operator()(Out &out,
auto const &x) { out << x; }
199template <
class It,
class F>
class PrintRange {
203 PrintRange(It first, It last,
char const *sep, A &&fun)
204 : first_{first}, last_{last}, sep_{sep}, fun_{std::forward<A>(fun)} {}
206 template <
class Out>
friend auto operator<<(Out &out, PrintRange rng) -> Out & {
207 if (rng.first_ != rng.last_) {
208 rng.fun_(out, *rng.first_);
209 for (++rng.first_; rng.first_ != rng.last_; ++rng.first_) {
211 rng.fun_(out, *rng.first_);
221 [[no_unique_address]] F fun_;
224template <
class It,
class F> PrintRange(It, It,
char const *, F &&) -> PrintRange<It, std::unwrap_ref_decay_t<F>>;
227template <
class F>
class PrintFun {
230 template <
class A> PrintFun([[maybe_unused]]
int tag, A &&fun) : fun_{std::forward<A>(fun)} {}
232 template <
class Out>
friend auto operator<<(Out &out, PrintFun x) -> Out & {
241template <
class F> PrintFun(
int, F &&) -> PrintFun<std::unwrap_ref_decay_t<F>>;
247 PrintQuoted(std::string_view str) : str_{str} {}
249 template <
class Out>
friend auto operator<<(Out &out, PrintQuoted x) -> Out & {
252 for (
auto c : x.str_) {
255 }
else if (c ==
'\n') {
257 }
else if (c ==
'\t') {
259 }
else if (c ==
'"') {
270 std::string_view str_;
276template <
class F>
auto p_fun(F &&fun) {
277 return Detail::PrintFun(0, std::forward<F>(fun));
281template <
class T,
class F>
auto p_range(T
const &rng,
char const *sep, F &&fun) {
282 using std::begin, std::end;
283 return Detail::PrintRange{begin(rng), end(rng), sep, std::forward<F>(fun)};
287template <
class T>
auto p_range(T
const &rng) {
288 return p_range(rng,
",", Detail::PrintSelf{});
292template <
class T,
class F>
auto p_range(T
const &rng, F &&fun) {
293 return p_range(rng,
",", std::forward<F>(fun));
297template <
class T>
auto p_range(T
const &rng,
char const *sep) {
298 return p_range(rng, sep, Detail::PrintSelf{});
303 return Detail::PrintQuoted{str};
Create an output buffer that bears some similarities with C++'s iostreams.
Definition print.hh:24
auto size() const -> size_t
Get the number of bytes currently stored in the buffer.
Definition print.hh:52
auto release() -> std::vector< char >
Empty the buffer and return a vector with the previous content.
Definition print.hh:78
friend auto operator<<(OutputBuffer &out, T num) -> OutputBuffer &
Append the given integral to the buffer.
Definition print.hh:139
void flush()
Flush the buffer.
Definition print.hh:32
auto empty() const -> bool
Check if the buffer is currently emtpy.
Definition print.hh:55
auto view() const -> std::string_view
Get a string view of the current buffer content.
Definition print.hh:58
void append(char c)
Append a char to the buffer.
Definition print.hh:102
auto c_str() -> char const *
Get a C string with the current buffer content.
Definition print.hh:66
friend auto operator<<(OutputBuffer &out, double value) -> OutputBuffer &
Append the given double to the buffer.
Definition print.hh:157
friend auto operator<<(OutputBuffer &out, char const *str) -> OutputBuffer &
Append the given string to the buffer.
Definition print.hh:167
void pop()
Pop a char from the buffer.
Definition print.hh:108
void append(std::string_view str)
Append a string to the buffer.
Definition print.hh:95
void trim_zero(std::ptrdiff_t len)
Trim trailing zeros.
Definition print.hh:130
friend auto operator<<(OutputBuffer &out, std::string_view str) -> OutputBuffer &
Append the given string to the buffer.
Definition print.hh:151
auto str() const -> std::string
Get a string with the current buffer content.
Definition print.hh:63
auto reserve(std::ptrdiff_t n) -> std::span< char >
Append n bytes at the end of the buffer.
Definition print.hh:121
void endl()
Flush the buffer if it has a predefined minimum size.
Definition print.hh:43
OutputBuffer(FILE *out=nullptr)
Construt the buffer with an optional file handle.
Definition print.hh:27
friend auto operator<<(OutputBuffer &out, char c) -> OutputBuffer &
Append the given char to the buffer.
Definition print.hh:145
auto reset() -> OutputBuffer &
Empty the buffer.
Definition print.hh:72
void append(T num)
Append an integral to the buffer.
Definition print.hh:111
void append(char const *str)
Append a string to the buffer.
Definition print.hh:88
auto operator<<(std::ostream &out, Sign sign) -> std::ostream &
Output the given sign.
auto p_quoted(std::string_view str)
Quote and print the given string.
Definition print.hh:302
auto p_fun(F &&fun)
Print with a function.
Definition print.hh:276
auto p_range(T const &rng, char const *sep, F &&fun)
Print a range with a separator.
Definition print.hh:281