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
UCT/include/UCTUtils.h
2022-08-04 13:35:22 +02:00

34 lines
846 B
C++

#pragma once
#include <memory>
#include <string>
#include <stdexcept>
#include <vector>
namespace uct {
namespace save {
struct Template;
} // namespace save
namespace utils {
template<typename ... Args>
std::string format(const std::string& format, Args ... args)
{
int size_s = std::snprintf(nullptr, 0, format.c_str(), args ...) + 1; // Extra space for '\0'
if (size_s <= 0) { throw std::runtime_error("Error during formatting."); }
auto size = static_cast<size_t>(size_s);
std::unique_ptr<char[]> buf(new char[size]);
std::snprintf(buf.get(), size, format.c_str(), args ...);
return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside
}
std::string format(std::string format, const std::vector<std::string>& args);
std::string format(const save::Template& t);
} // namespace utils
} // namespace uct