| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #pragma once |
|
|
| #include <memory> |
| #include <sstream> |
| #include <string> |
| #include <vector> |
|
|
| namespace fastertransformer { |
|
|
| template<typename... Args> |
| inline std::string fmtstr(const std::string& format, Args... args) |
| { |
| |
| |
|
|
| |
| #if defined(_MSC_VER) |
| #pragma warning(push) |
| #pragma warning(warning(disable : 4996)) |
| #elif defined(__GNUC__) || defined(__clang__) |
| #pragma GCC diagnostic push |
| #pragma GCC diagnostic ignored "-Wformat-security" |
| #endif |
| int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) + 1; |
| if (size_s <= 0) { |
| throw std::runtime_error("Error during formatting."); |
| } |
| auto size = static_cast<size_t>(size_s); |
| auto buf = std::make_unique<char[]>(size); |
| std::snprintf(buf.get(), size, format.c_str(), args...); |
| #if defined(_MSC_VER) |
| #pragma warning(pop) |
| #elif defined(__GNUC__) || defined(__clang__) |
| #pragma GCC diagnostic pop |
| #endif |
| return std::string(buf.get(), buf.get() + size - 1); |
| } |
| } |
|
|