14namespace CppClingo::Util {
38 if (out_ !=
nullptr) {
39 fwrite(buf_.data(),
sizeof(
char), size_, out_);
49 constexpr auto n = 8192;
50 if (out_ !=
nullptr && size_ > n) {
51 fwrite(buf_.data(),
sizeof(
char), size_, out_);
57 [[nodiscard]]
auto size() const ->
size_t {
return static_cast<size_t>(size_); }
60 [[nodiscard]]
auto empty() const ->
bool {
return size_ == 0; }
63 [[nodiscard]]
auto view() const -> std::string_view {
64 return std::string_view{buf_.data(),
static_cast<size_t>(size_)};
68 [[nodiscard]]
auto span() -> std::span<char> {
return std::span{buf_.data(),
static_cast<size_t>(size_)}; }
71 [[nodiscard]]
auto str() const -> std::
string {
return std::string{buf_.data(),
static_cast<size_t>(size_)}; }
74 [[nodiscard]]
auto c_str() ->
char const * {
88 buf_.emplace_back(
'\0');
89 auto ret = std::move(buf_);
97 auto n =
static_cast<std::ptrdiff_t
>(std::strlen(
str));
98 std::copy(
str, std::next(
str, n), ensure_(n));
104 auto n =
static_cast<std::ptrdiff_t
>(
str.length());
105 std::ranges::copy(
str, ensure_(n));
122 template <std::
integral T>
void append(T num,
int base = 10) {
123 constexpr auto n = 256;
124 auto *end = ensure_(n);
125 auto res = std::to_chars(end, limit_(), num, base);
126 size_ += res.ptr - end;
132 auto reserve(std::ptrdiff_t n) -> std::span<char> {
133 auto *begin = ensure_(n);
135 return {begin, std::next(begin, n)};
142 auto sp = std::span{buf_.data(),
static_cast<size_t>(size_)};
144 auto ib = sp.begin() + (size_ - len);
145 auto it = std::find(ib, ie,
'\0');
169 static constexpr std::ptrdiff_t n = 32;
170 auto *begin = out.ensure_(n);
171 auto *end = std::next(begin, n);
172 auto [res, ec] = std::to_chars(begin, end, value);
173 out.size_ += std::distance(begin, res);
184 auto limit_() ->
char * {
return std::next(buf_.data(),
static_cast<std::ptrdiff_t
>(buf_.size())); }
186 auto ensure_(std::ptrdiff_t n) ->
char * {
188 assert(n >= 0 && m >= 0);
189 if (buf_.size() <
static_cast<size_t>(m)) {
190 buf_.reserve(2 *
static_cast<size_t>(m));
191 buf_.resize(buf_.capacity());
193 return std::next(buf_.data(), size_);
196 std::vector<char> buf_;
197 std::ptrdiff_t size_ = 0;
205 OutputStream(FILE *out) : std::ostream{nullptr}, buf_{out} { rdbuf(&buf_); }
211 class Streambuf :
public std::streambuf {
213 Streambuf(FILE *out) : buf_{out} {}
214 auto buffer() -> OutputBuffer & {
return buf_; }
217 auto overflow(
int ch) ->
int override {
218 if (ch != traits_type::eof()) {
219 buf_.push_back(
static_cast<char>(ch));
220 return traits_type::not_eof(ch);
222 return traits_type::eof();
225 auto xsputn(
char const *s, std::streamsize n) -> std::streamsize
override {
226 buf_.append(std::string_view{s,
static_cast<size_t>(n)});
230 auto sync() ->
int override {
247 template <
class Out>
void operator()(Out &out,
auto const &x) { out << x; }
251template <
class It,
class F>
class PrintRange {
255 PrintRange(It first, It last,
char const *sep, A &&fun)
256 : first_{first}, last_{last}, sep_{sep}, fun_{std::forward<A>(fun)} {}
258 template <
class Out>
friend auto operator<<(Out &out, PrintRange rng) -> Out & {
259 if (rng.first_ != rng.last_) {
260 rng.fun_(out, *rng.first_);
261 for (++rng.first_; rng.first_ != rng.last_; ++rng.first_) {
263 rng.fun_(out, *rng.first_);
273 [[no_unique_address]] F fun_;
276template <
class It,
class F> PrintRange(It, It,
char const *, F &&) -> PrintRange<It, std::unwrap_ref_decay_t<F>>;
279template <
class F>
class PrintFun {
282 template <
class A> PrintFun([[maybe_unused]]
int tag, A &&fun) : fun_{std::forward<A>(fun)} {}
284 template <
class Out>
friend auto operator<<(Out &out, PrintFun x) -> Out & {
293template <
class F> PrintFun(
int, F &&) -> PrintFun<std::unwrap_ref_decay_t<F>>;
299 PrintQuoted(std::string_view str,
bool fstring) : str_{str}, fstring_{fstring} {}
301 template <
class Out>
friend auto operator<<(Out &out, PrintQuoted x) -> Out & {
305 for (
auto c : x.str_) {
308 }
else if (x.fstring_ && c ==
'{') {
310 }
else if (x.fstring_ && c ==
'}') {
312 }
else if (c ==
'\n') {
314 }
else if (c ==
'\t') {
316 }
else if (c ==
'"') {
329 std::string_view str_;
330 bool fstring_ =
false;
339 fill(
size_t n,
char c =
' ') : n_{n}, c_{c} {}
342 std::ranges::fill(out.reserve(
static_cast<std::ptrdiff_t
>(x.n_)), x.c_);
352template <
class F>
auto p_fun(F &&fun) {
353 return Detail::PrintFun(0, std::forward<F>(fun));
357template <
class T,
class F>
auto p_range(T
const &rng,
char const *sep, F &&fun) {
358 using std::begin, std::end;
359 return Detail::PrintRange{begin(rng), end(rng), sep, std::forward<F>(fun)};
363template <
class T>
auto p_range(T
const &rng) {
364 return p_range(rng,
",", Detail::PrintSelf{});
368template <
class T,
class F>
auto p_range(T
const &rng, F &&fun) {
369 return p_range(rng,
",", std::forward<F>(fun));
373template <
class T>
auto p_range(T
const &rng,
char const *sep) {
374 return p_range(rng, sep, Detail::PrintSelf{});
378inline auto p_quoted(std::string_view str,
bool fstring =
false) {
379 return Detail::PrintQuoted{str, fstring};
Create an output buffer that bears some similarities with C++'s iostreams.
Definition print.hh:26
auto size() const -> size_t
Get the number of bytes currently stored in the buffer.
Definition print.hh:57
auto release() -> std::vector< char >
Empty the buffer and return a vector with the previous content.
Definition print.hh:86
void append(T num, int base=10)
Append an integral to the buffer.
Definition print.hh:122
friend auto operator<<(OutputBuffer &out, T num) -> OutputBuffer &
Append the given integral to the buffer.
Definition print.hh:150
auto span() -> std::span< char >
Get a char span of the current buffer content.
Definition print.hh:68
void flush()
Flush the buffer.
Definition print.hh:37
auto empty() const -> bool
Check if the buffer is currently emtpy.
Definition print.hh:60
auto view() const -> std::string_view
Get a string view of the current buffer content.
Definition print.hh:63
void append(char c)
Append a char to the buffer.
Definition print.hh:110
auto c_str() -> char const *
Get a C string with the current buffer content.
Definition print.hh:74
friend auto operator<<(OutputBuffer &out, double value) -> OutputBuffer &
Append the given double to the buffer.
Definition print.hh:168
friend auto operator<<(OutputBuffer &out, char const *str) -> OutputBuffer &
Append the given string to the buffer.
Definition print.hh:178
void pop()
Pop a char from the buffer.
Definition print.hh:119
void append(std::string_view str)
Append a string to the buffer.
Definition print.hh:103
void trim_zero(std::ptrdiff_t len)
Trim trailing zeros.
Definition print.hh:141
friend auto operator<<(OutputBuffer &out, std::string_view str) -> OutputBuffer &
Append the given string to the buffer.
Definition print.hh:162
auto str() const -> std::string
Get a string with the current buffer content.
Definition print.hh:71
auto reserve(std::ptrdiff_t n) -> std::span< char >
Append n bytes at the end of the buffer.
Definition print.hh:132
void endl()
Flush the buffer if it has a predefined minimum size.
Definition print.hh:48
void push_back(char c)
Alias for append(char) to support std::back_inserter.
Definition print.hh:116
OutputBuffer(FILE *out=nullptr)
Construct the buffer with an optional file handle.
Definition print.hh:32
friend auto operator<<(OutputBuffer &out, char c) -> OutputBuffer &
Append the given char to the buffer.
Definition print.hh:156
auto reset() -> OutputBuffer &
Empty the buffer.
Definition print.hh:80
char value_type
The value type of the buffer.
Definition print.hh:29
void append(char const *str)
Append a string to the buffer.
Definition print.hh:96
Output stream with an underlying OutputBuffer.
Definition print.hh:202
auto buffer() -> OutputBuffer &
Get the underlying buffer.
Definition print.hh:208
OutputStream(FILE *out)
Construct the buffer with an optional file handle.
Definition print.hh:205
Helper for iostreams to fill with a fixed number of characters.
Definition print.hh:336
fill(size_t n, char c=' ')
The constructor.
Definition print.hh:339
friend auto operator<<(CppClingo::Util::OutputBuffer &out, fill const &x) -> CppClingo::Util::OutputBuffer &
Operator to print the fill helper.
Definition print.hh:341
auto operator<<(std::ostream &out, Sign sign) -> std::ostream &
Output the given sign.
auto p_quoted(std::string_view str, bool fstring=false)
Quote and print the given string.
Definition print.hh:378
auto p_fun(F &&fun)
Print with a function.
Definition print.hh:352
auto p_range(T const &rng, char const *sep, F &&fun)
Print a range with a separator.
Definition print.hh:357