-
Notifications
You must be signed in to change notification settings - Fork 21
/
format.hpp
136 lines (125 loc) · 3.55 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
// LGPL 3 or higher Robert Burner Schadek rburners@gmail.com
#pragma once
#include <stdexcept>
#include <algorithm>
#include <iostream>
#include <ostream>
#include <sstream>
#include <locale>
#include <iomanip>
#include <type_traits>
#include <cctype>
// yes I know
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
#pragma GCC diagnostic ignored "-Wcast-align"
#pragma GCC diagnostic ignored "-Wold-style-cast"
static inline void setStreamFormat(std::ostream& out, const std::string& form,
size_t s, size_t e) {
// check the flags
if(form[s] == '-') {
out<<std::left;
++s;
} else if(form[s] == '+') {
out<<std::right;
++s;
} else if(form[s] == '0') {
out<<std::setfill('0');
++s;
} else if(form[s] == '#') {
out<<std::showbase;
++s;
}
// if we find no dot only the width will follow
size_t dot = form.substr(s, e-s).find(".");
if(dot != std::string::npos) {
dot += s;
}
std::string width(form.substr(s, dot < e ? dot-s : e-s));
if(!std::all_of(width.begin(), width.end(), [](const char c) {
return c <= '9' && c >= '0';})) {
throw std::invalid_argument("width string did not convert to integer");
} else if(!width.empty()) {
out<<std::setw(std::stoi(width));
}
s = dot < form.size() ? dot+1 : form.size();
if(s < e) {
std::string pre(form.substr(s, e-s));
if(!std::all_of(pre.begin(), pre.end(), [](const char c) {
return c <= '9' && c >= '0';})) {
throw std::invalid_argument("precision string did not convert to "
"integer");
} else if(!pre.empty()) {
out<<std::setprecision(std::stoi(pre));
}
s = e;
}
// lower or upper case
out<<(std::islower(form[s]) ? std::nouppercase : std::uppercase);
out<<std::fixed;
if(form[s] == 'f' || form[s] == 'F') {
out<<std::fixed;
} else if(form[s] == 'a' || form[s] == 'A' || form[s] == 'x' ||
form[s] == 'X' || form[s] == 'p') {
out<<std::hex;
} else if(form[s] == 'e' || form[s] == 'E') {
out<<std::scientific;
} else if(form[s] == 'o') {
out<<std::oct;
} else if(form[s] == 'b') {
out<<std::boolalpha;
}
}
static inline void formImpl(std::ostream& out, const std::string& s,
size_t pos) {
while(pos != s.size()) {
if(s[pos] == '%') {
if(s[pos + 1] == '%') {
++pos;
} else {
throw std::runtime_error(
"invalid format string: missing arguments"
);
}
}
out<<s[pos++];
}
}
template<typename T, typename... Args>
static inline void formImpl(std::ostream& out, const std::string& s,
size_t pos, T value, Args... args) {
while(pos != s.size()) {
if(s[pos] == '%') {
if(s[pos + 1] == '%') {
++pos;
} else {
auto savedFlags(out.flags());
size_t next(s.find_first_of("csdioxbXufFeEaAgGp", pos+1));
setStreamFormat(out, s, pos+1, next);
if(s[next] == 'p') {
//printPointer(out, value);
//out<<*reinterpret_cast<const void**>(&value);
out<<*(const void**)(&value);
} else {
out<<value;
}
out.flags(savedFlags);
pos = next;
// call even when *s == 0 to detect extra arguments
formImpl(out, s, pos+1, args...);
return;
}
}
out<< s[pos++];
}
throw std::logic_error("extra arguments provided to format");
}
template<typename... Args>
inline std::string format(const std::string& str, Args... args) {
std::stringstream ret;
formImpl(ret, str,0, args...);
return ret.str();
}
template<typename... Args>
inline void format(std::ostream& out, const std::string& str, Args... args) {
formImpl(out, str,0, args...);
}