diff --git a/include/misc/Format.h b/include/misc/Format.h new file mode 100644 index 0000000..4c23dc9 --- /dev/null +++ b/include/misc/Format.h @@ -0,0 +1,23 @@ +#pragma once + +#include +#include +#include + +namespace td { +namespace utils { + +template +std::string format(const std::string& format, Args... args){ + int size = snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0' + if (size <= 0){ + throw std::runtime_error("Error during formatting."); + } + std::unique_ptr buf(new char[size]); + snprintf(buf.get(), size, format.c_str(), args...); + return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside +} + +} // namespace utils +} // namespace td +