-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathformat.hh
43 lines (35 loc) · 791 Bytes
/
format.hh
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
#ifndef FORMAT_HH
#define FORMAT_HH
#include <stdexcept>
#include <sstream>
#include <string>
void format(std::ostringstream &ss, const char *s)
{
while (*s) {
if (*s == '$' || *s == '#')
throw std::runtime_error("too few arguments provided to format");
ss << *s++;
}
}
template<typename t, typename... Args>
void format(std::ostringstream &ss, const char *s, t &value, Args... args)
{
while (*s) {
if (*s == '$' || *s == '#') {
if (*s == '#') ss << "0x" << std::hex;
ss << value;
format(ss, ++s, args...);
return;
}
ss << *s++;
}
throw std::runtime_error("too many arguments provided to format");
}
template<typename... Args>
std::string format(const char *s, Args... args)
{
std::ostringstream ss;
format(ss, s, args...);
return ss.str();
}
#endif