From 3e683a243635435c47ee4c60ba2492333a052a72 Mon Sep 17 00:00:00 2001 From: q66 Date: Wed, 1 Feb 2017 19:29:42 +0100 Subject: [PATCH] remove silly intermediate ranges for writef, print directly to stream --- ostd/io.hh | 32 +++++++++++++++----------------- ostd/stream.hh | 47 ++++++----------------------------------------- 2 files changed, 21 insertions(+), 58 deletions(-) diff --git a/ostd/io.hh b/ostd/io.hh index d819209..26ddc8c 100644 --- a/ostd/io.hh +++ b/ostd/io.hh @@ -165,6 +165,17 @@ namespace detail { ) { write_impl(ostd::to_string(v)); } + + /* lightweight output range for direct stdout */ + struct StdoutRange: OutputRange { + StdoutRange() {} + bool put(char c) { + return putchar(c) != EOF; + } + size_t put_n(char const *p, size_t n) { + return fwrite(p, 1, n, stdout); + } + }; } template @@ -181,38 +192,25 @@ inline void write(T const &v, A const &...args) { template inline void writeln(T const &v) { write(v); - putc('\n', stdout); + putchar('\n'); } template inline void writeln(T const &v, A const &...args) { write(v); write(args...); - putc('\n', stdout); + putchar('\n'); } template inline void writef(ConstCharRange fmt, A const &...args) { - char buf[512]; - ptrdiff_t need = format( - detail::FormatOutRange(buf), fmt, args... - ); - if (need < 0) { - return; - } else if (size_t(need) < sizeof(buf)) { - fwrite(buf, 1, need, stdout); - return; - } - std::vector s; - s.reserve(need); - format(detail::UnsafeWritefRange(s.data()), fmt, args...); - fwrite(s.data(), 1, need, stdout); + format(detail::StdoutRange{}, fmt, args...); } template inline void writefln(ConstCharRange fmt, A const &...args) { writef(fmt, args...); - putc('\n', stdout); + putchar('\n'); } } /* namespace ostd */ diff --git a/ostd/stream.hh b/ostd/stream.hh index 3e489ad..8485608 100644 --- a/ostd/stream.hh +++ b/ostd/stream.hh @@ -35,32 +35,6 @@ enum class StreamSeek { template> struct StreamRange; -namespace detail { - template - struct FormatOutRange: OutputRange, char> { - FormatOutRange(char *ibuf): buf(ibuf), idx(0) {} - FormatOutRange(FormatOutRange const &r): buf(r.buf), idx(r.idx) {} - char *buf; - size_t idx; - bool put(char v) { - if (idx < N) { - buf[idx++] = v; - return true; - } - return false; - } - }; - - struct UnsafeWritefRange: OutputRange { - UnsafeWritefRange(char *p): p_ptr(p) {} - bool put(char c) { - *p_ptr++ = c; - return true; - } - char *p_ptr; - }; -} - struct Stream { private: struct StNat {}; @@ -138,21 +112,7 @@ public: } template - bool writef(ConstCharRange fmt, A const &...args) { - char buf[512]; - ptrdiff_t need = format( - detail::FormatOutRange(buf), fmt, args... - ); - if (need < 0) { - return false; - } else if (size_t(need) < sizeof(buf)) { - return write_bytes(buf, need) == size_t(need); - } - std::vector s; - s.reserve(need); - format(detail::UnsafeWritefRange(s.data()), fmt, args...); - return write_bytes(s.data(), need) == size_t(need); - } + bool writef(ConstCharRange fmt, A const &...args); template bool writefln(ConstCharRange fmt, A const &...args) { @@ -246,6 +206,11 @@ inline StreamRange Stream::iter() { return StreamRange(*this); } +template +inline bool Stream::writef(ConstCharRange fmt, A const &...args) { + return format(iter(), fmt, args...) >= 0; +} + } #endif