diff options
author | Devtools Arcadia <[email protected]> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <[email protected]> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/stream/printf.cpp |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/stream/printf.cpp')
-rw-r--r-- | util/stream/printf.cpp | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/util/stream/printf.cpp b/util/stream/printf.cpp new file mode 100644 index 00000000000..f3eeca7afcb --- /dev/null +++ b/util/stream/printf.cpp @@ -0,0 +1,51 @@ +#include "output.h" +#include "printf.h" + +#include <util/generic/scope.h> +#include <util/memory/tempbuf.h> +#include <util/generic/yexception.h> + +size_t Printf(IOutputStream& out, const char* fmt, ...) { + va_list lst; + va_start(lst, fmt); + + Y_DEFER { + va_end(lst); + }; + + return Printf(out, fmt, lst); +} + +static inline size_t TryPrintf(void* ptr, size_t len, IOutputStream& out, const char* fmt, va_list params) { + va_list lst; + va_copy(lst, params); + const int ret = vsnprintf((char*)ptr, len, fmt, lst); + va_end(lst); + + if (ret < 0) { + return len; + } + + if ((size_t)ret < len) { + out.Write(ptr, (size_t)ret); + } + + return (size_t)ret; +} + +size_t Printf(IOutputStream& out, const char* fmt, va_list params) { + size_t guess = 0; + + while (true) { + TTempBuf tmp(guess); + const size_t ret = TryPrintf(tmp.Data(), tmp.Size(), out, fmt, params); + + if (ret < tmp.Size()) { + return ret; + } + + guess = Max(tmp.Size() * 2, ret + 1); + } + + return 0; +} |