Do llformat even better!

This commit is contained in:
Lirusaito
2019-02-20 21:39:06 -05:00
parent a2701faa2b
commit 4f87e82a19

View File

@@ -37,26 +37,24 @@
#include <cstdarg> #include <cstdarg>
constexpr auto smallsize = 1024;
// common used function with va_list argument // common used function with va_list argument
// wrapper for vsnprintf to be called from llformatXXX functions. // wrapper for vsnprintf to be called from llformatXXX functions.
static void va_format(std::string& out, const char *fmt, va_list va) static void va_format(std::string& charvector, const char *fmt, va_list va)
{ {
constexpr auto smallsize = 1024;
std::vector<char> charvector(smallsize); // Evolves into charveleon
va_list va2; va_list va2;
va_copy(va2, va); va_copy(va2, va);
const auto size = std::vsnprintf(charvector.data(), charvector.size(), fmt, va); const auto size = std::vsnprintf(charvector.data(), charvector.size(), fmt, va);
if (size >= smallsize) if (size >= smallsize) // Evolve into charveleon
{ {
charvector.resize(1+size); // Use the String Stone charvector.resize(1+size); // Use the String Stone
std::vsnprintf(charvector.data(), charvector.size(), fmt, va2); std::vsnprintf(charvector.data(), charvector.size(), fmt, va2);
} }
out.assign(charvector.data());
} }
std::string llformat(const char *fmt, ...) std::string llformat(const char *fmt, ...)
{ {
std::string res; std::string res(smallsize, '\0');
va_list va; va_list va;
va_start(va, fmt); va_start(va, fmt);
va_format(res, fmt, va); va_format(res, fmt, va);
@@ -66,7 +64,7 @@ std::string llformat(const char *fmt, ...)
std::string llformat_to_utf8(const char *fmt, ...) std::string llformat_to_utf8(const char *fmt, ...)
{ {
std::string res; std::string res(smallsize, '\0');
va_list va; va_list va;
va_start(va, fmt); va_start(va, fmt);
va_format(res, fmt, va); va_format(res, fmt, va);