-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_to_string.cpp
236 lines (204 loc) · 6.57 KB
/
format_to_string.cpp
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <type_traits>
#include <string>
#include <string_view>
namespace details
{
// iteration over the variadic template arguments.
// Normal case:
template<typename FuncT, typename HeadT, typename... ArgsT>
void template_for_each(FuncT func, HeadT&& head, ArgsT&&... rest)
{
func(std::forward<HeadT>(head));
template_for_each(func, rest...);
}
// Terminating case:
template<typename FuncT>
void template_for_each(FuncT) {}
// This overload is optional: if it's not present, `strlen` is always called,
// which is usually OK, but... Why not avoid that if length is known at compile-time?
template<size_t N>
std::string_view to_string_view(const char (&s)[N])
{
// C-style arrays have terminating '\0' that we don't need.
return {s, N-1};
}
// the template specification is crafted to force compiler to look at this overload
// *after* the previous one.
// (borrowed here https://android.googlesource.com/platform/external/qemu/+/emu-master-dev/android/android-emu-base/android/base/StringView.h#106)
template<typename CharT, typename = std::enable_if_t<std::is_same_v<CharT, char>>>
std::string_view to_string_view(const CharT* const &s)
{
return s;
}
// implementation for string and string_view are trivial:
template<typename = char>
std::string_view to_string_view(const std::string& s){
return s;
}
template<typename = char>
std::string_view to_string_view(const std::string_view& s){
return s;
}
}
// concatenate string-like arguments with no more than one allocation:
template<typename... ArgsT>
std::string concat(ArgsT&&... args)
{
using namespace details;
// First, compute the total size of all strings combined:
size_t totalSize = 0;
template_for_each([&totalSize](auto&& arg) {
using ArgT = std::remove_reference_t<decltype(arg)>;
totalSize += to_string_view(std::forward<ArgT>(arg)).size();
}, std::forward<ArgsT>(args)...);
std::string result;
// ... and allocate enough memory (+1 byte for '\0')
result.reserve(totalSize + 1);
// then iterate again and append each argument to the accumulator
template_for_each([&result](auto&& arg) {
using ArgT = std::remove_reference_t<decltype(arg)>;
result += to_string_view(std::forward<ArgT>(arg));
}, std::forward<ArgsT>(args)...);
return result;
}
// Utilities for formatting
namespace details
{
struct marker_info {
std::string_view literal;
int arg_number;
std::string_view rest;
};
// this can be done a bit clearer with C++20, but I only have C++17 compiler installed right now
constexpr marker_info find_marker(const std::string_view& format)
{
for (auto i = 0; i < format.size(); ++i)
{
if (format[i] == '%')
{
auto literal_end = i++;
auto c = format[i++];
if (c == '%')
{
return {
format.substr(0, literal_end + 1),
-1,
format.substr(i)
};
}
else if ('0' <= c && c <= '9')
{
return {
format.substr(0, literal_end),
c - '0',
format.substr(i)
};
}
}
}
return { format, -1, format.substr(0,0) };
}
template<typename Func>
constexpr void foreach_marker(std::string_view fmt, Func func)
{
while(!fmt.empty())
{
auto [ literal, arg_number, rest ] = find_marker(fmt);
func(literal, arg_number);
fmt = rest;
}
}
template<int N>
std::string_view nth(int) {
return "";
}
template<int N = 0, typename ArgT, typename... ArgsT>
std::string_view nth(int i, ArgT&& arg, ArgsT&&... args)
{
if (i == N)
{
return to_string_view(std::forward<ArgT>(arg));
}
return nth<N+1, ArgsT...>(i, std::forward<ArgsT>(args)...);
}
}
// Simple formatting facility - replaces markers `%0`..`%9` with corresponding string-ish arguments.
// Doesn't do any advanced validation (but it's not hard to implement one).
// Only allocates memory once.
template<typename... ArgsT>
std::string format(const std::string_view& fmt, ArgsT&&... args)
{
using namespace details;
// compute length of all the strings, including substitutions.
size_t total_length = 0;
foreach_marker(fmt, [&total_length, &args...](auto&& s, int arg_id) {
total_length += s.size();
if (arg_id >= 0)
{
total_length += nth(arg_id, std::forward<ArgsT>(args)...).size();
}
});
std::string result;
result.reserve(total_length);
// concatenate
foreach_marker(fmt, [&result, &args...](auto&& s, int arg_id) {
result.append(s);
if (arg_id >= 0)
{
result.append(nth(arg_id, std::forward<ArgsT>(args)...));
}
});
return result;
}
// overriding new/delete to verify no more than one allocation per `concat`/`format` is done
#include <cstdlib>
static int alloc_count = 0;
static int free_count = 0;
void* operator new(size_t sz)
{
alloc_count++;
return std::malloc(sz);
}
void* operator new[](size_t sz)
{
alloc_count++;
return std::malloc(sz);
}
void operator delete(void* p)
{
free_count++;
std::free(p);
}
void operator delete[](void* p)
{
free_count++;
std::free(p);
}
// simple test program
#include <cstdio>
#include <cassert>
int main()
{
using namespace std::literals;
{
// using different kinds of literals here.
// We want to get a string long enough for SSO not to apply.
const char* bang = "!!!!";
const std::string hello = "Hello"s;
auto s = concat(hello," dear ", "world"sv, bang);
std::fprintf(stderr, "%s\n", s.c_str());
}
assert(alloc_count == free_count);
assert(alloc_count == 1);
alloc_count = free_count = 0;
{
// using different kinds of literals here.
// We want to get a string long enough for SSO not to apply.
const char* bang = "!!!!";
const std::string hello = "Hello"s;
auto s = format("- %0, %1 %3%2...", hello, "dear", bang, "world"sv);
std::fprintf(stderr, "%s\n", s.c_str());
}
assert(alloc_count == free_count);
assert(alloc_count == 1);
}