-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.h
39 lines (35 loc) · 1.2 KB
/
util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#pragma once
class StringHelper
{
private:
template<typename T, typename std::enable_if<std::is_same<std::remove_cv_t<std::remove_reference_t<T>>, std::string>::value>::type* = nullptr>
static auto convert(T value)
{
return std::forward<T>(value).c_str();
}
template<typename T, typename std::enable_if<!std::is_same<std::remove_cv_t<std::remove_reference_t<T>>, std::string>::value>::type* = nullptr>
static auto convert(T value)
{
return std::forward<T>(value);
}
template<typename ... Args>
static std::string strformat(const std::string &fmt, Args ... args)
{
int len = std::snprintf(nullptr, 0, fmt.c_str(), std::forward<Args>(args) ...);
if (len < 0)
{
// 異常系(想定外)
return "";
}
size_t bufSize = len + sizeof(char);
std::vector<char> buf(bufSize);
std::snprintf(buf.data(), bufSize, fmt.c_str(), args ...);
return std::string(buf.data(), buf.data() + len);
}
public:
template<typename ... Args>
static std::string strprintf(const std::string &fmt, Args ... args)
{
return strformat(fmt, convert(std::forward<Args>(args)) ...);
}
};