This repository has been archived on 2025-02-26. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
LazyBot/include/Format.h
2024-01-26 00:16:45 +01:00

18 lines
567 B
C++

#pragma once
#include <string>
#include <memory>
#include <stdexcept>
template <typename... Args>
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<char[]> buf(new char[size]);
snprintf(buf.get(), static_cast<std::size_t>(size), format.c_str(), args...);
return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside
}