-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.h
96 lines (79 loc) · 1.9 KB
/
format.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#pragma once
#include <string>
#include <cstring>
inline void putstr(std::string const& str) {
std::puts(str.c_str());
}
inline void wputstr(std::wstring const& str) {
_putws(str.c_str());
}
namespace std {
inline string to_string(char const* c_str) {
return c_str;
}
inline string to_string(string const& str) {
return str;
}
inline wstring to_wstring(wchar_t const* c_str) {
return c_str;
}
inline wstring to_wstring(wstring const& str) {
return str;
}
}
template<typename... arg_types>
inline std::string formatv(std::string const& fstr, arg_types... args) {
std::string result;
size_t arg_index = 0;
if constexpr(sizeof...(arg_types) > 0) {
std::string converted[] = { std::to_string(args)... };
for(char const& c : fstr) {
if(c == '%') {
result += converted[arg_index++];
} else {
result += c;
}
}
} else {
result = fstr;
}
return result;
}
template<typename... arg_types>
inline std::wstring wformatv(std::wstring const& fstr, arg_types... args) {
std::wstring result;
size_t arg_index = 0;
if constexpr(sizeof...(arg_types) > 0) {
std::wstring converted[] = { std::to_wstring(args)... };
for(wchar_t const& c : fstr) {
if(c == '%') {
result += converted[arg_index++];
} else {
result += c;
}
}
} else {
result = fstr;
}
return result;
}
template<typename... arg_type>
inline void printv(std::string const& fstr, arg_type... args) {
std::string str = formatv(fstr, args...);
putstr(str);
}
template<typename... arg_type>
inline void wprintv(std::wstring const& fstr, arg_type... args) {
std::wstring str = wformatv(fstr, args...);
wputstr(str);
}
template<typename... arg_types>
inline void panicv(std::string const& fstr, arg_types... args) {
printv(fstr, args...);
exit(-1);
}
template<typename... arg_types>
inline void wpanicv(std::wstring const& fstr, arg_types... args) {
wprintv(fstr, args...);
exit(-1);
}