-
Notifications
You must be signed in to change notification settings - Fork 457
/
Copy pathFormat.hpp
156 lines (134 loc) · 3.42 KB
/
Format.hpp
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#pragma once
#include <tuple>
#include <type_traits>
#include <string>
#include <inttypes.h>
#include <cctype>
using namespace std;
namespace detail
{
char g_buf[2000] = {};
inline void FormatArg(char*& buf, int i)
{
_itoa(i, buf, 10);
buf += strlen(buf);
}
inline void FormatArg(char*& buf, double i)
{
auto r = sprintf(buf, "%.6f", i);
buf += r;
}
inline void FormatArg(char*& buf, int64_t i)
{
auto r = sprintf(buf, "%"PRId64, i);
buf += r;
}
inline void FormatArg(char*& buf, uint64_t i)
{
auto r = sprintf(buf, "%"PRIu64, i);
buf += r;
}
inline void FormatArg(char*& buf, const char* p)
{
int len = strlen(p);
memcpy(buf, p, len);
buf += len;
}
inline void FormatArg(char*& buf, string& s)
{
memcpy(buf, s.data(), s.size());
buf += s.size();
}
template<size_t k, typename Tuple>
typename std::enable_if < (k == std::tuple_size<Tuple>::value)>::type
inline GetArgByIndex(size_t, Tuple&, char*&)
{
throw std::invalid_argument("arg index out of range");
}
template<size_t k = 0, typename Tuple>
typename std::enable_if < (k < std::tuple_size<Tuple>::value)>::type
inline GetArgByIndex(size_t index, Tuple& tp, char*& p)
{
if (k == index)
{
FormatArg(p, std::get<k>(tp));
}
else
{
GetArgByIndex<k + 1>(index, tp, p);
}
}
inline int GetIndex(char*& p)
{
char temp[3] = {};
int i = 0;
while (*p != '}'&&*p != '\0')
{
if (i >= 2)
throw std::invalid_argument("index is out of range.");
if (std::isdigit(*p))
{
//push digit
temp[i++] = *p;
char next = *(p + 1);
if (std::isdigit(next))
{
temp[i++] = next;
p += 2;
continue;
}
//validate arg
if (!std::isspace(next) && next != '}')
{
throw std::invalid_argument("invalid argument.");
}
}
p++;
}
return i == 0 ? -1 : std::atoi(temp);
}
}
template<typename... Args>
inline string format(const string& src, Args... args)
{
return format((char*) src.c_str(), args...);
}
template<typename... Args>
inline string format(char* src, Args... args)
{
using namespace detail;
char* buf = g_buf;
auto tp = std::tuple<Args...>(args...);
char* p = src;
char* original = p;
int len = strlen(src) + 1;
int last = 0;
while (true)
{
if (*p == '{')
{
//copy content befor {
last = p - original;
memcpy(buf, original, last);
buf += last;
//format args
int index = GetIndex(p);
if (index >= 0)
{
GetArgByIndex<0>(index, tp, Fun, buf);
}
//skip }
original = p + 1;
}
else if(*p=='\0')
{
last = p - original;
memcpy(buf, original, last);
break;
}
p++;
}
string s = g_buf;
memset(g_buf, 0, buf - g_buf);
return s;
}