-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathutility.cc
240 lines (200 loc) · 6.27 KB
/
utility.cc
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
237
238
239
240
#include <zipkin/utility.h>
#include <array>
#include <chrono>
#include <climits>
#include <cstdint>
#include <iterator>
#include <limits>
#include <pthread.h>
#include <random>
#include <string>
#include <vector>
#include <zipkin/randutils/randutils.h>
#include <zipkin/rapidjson/document.h>
#include <zipkin/rapidjson/stringbuffer.h>
#include <zipkin/rapidjson/writer.h>
namespace zipkin {
// Wrapper for a seeded random number generator that works with forking.
//
// See https://stackoverflow.com/q/51882689/4447365 and
// https://github.com/opentracing-contrib/nginx-opentracing/issues/52
namespace {
class TlsRandomNumberGenerator {
public:
TlsRandomNumberGenerator() { pthread_atfork(nullptr, nullptr, onFork); }
static std::mt19937_64 &engine() { return base_generator_.engine(); }
private:
static thread_local randutils::mt19937_64_rng base_generator_;
static void onFork() { base_generator_.seed(); }
};
thread_local randutils::mt19937_64_rng
TlsRandomNumberGenerator::base_generator_;
} // namespace
std::mt19937_64 &getTlsRandomEngine() {
static TlsRandomNumberGenerator rng;
return TlsRandomNumberGenerator::engine();
}
uint64_t RandomUtil::generateId() { return getTlsRandomEngine()(); }
bool StringUtil::atoul(const char *str, uint64_t &out, int base) {
if (strlen(str) == 0) {
return false;
}
char *end_ptr;
out = strtoul(str, &end_ptr, base);
if (*end_ptr != '\0' || (out == ULONG_MAX && errno == ERANGE)) {
return false;
} else {
return true;
}
}
bool StringUtil::atoull(const char *str, uint64_t &out, int base) {
if (strlen(str) == 0) {
return false;
}
char *end_ptr;
out = strtoull(str, &end_ptr, base);
if (*end_ptr != '\0' ||
(out == std::numeric_limits<uint64_t>::max() && errno == ERANGE)) {
return false;
} else {
return true;
}
}
uint32_t StringUtil::itoa(char *out, size_t buffer_size, uint64_t i) {
// The maximum size required for an unsigned 64-bit integer is 21 chars
// (including null).
if (buffer_size < 21) {
throw std::invalid_argument("itoa buffer too small");
}
char *current = out;
do {
*current++ = "0123456789"[i % 10];
i /= 10;
} while (i > 0);
for (uint64_t i = 0, j = current - out - 1; i < j; i++, j--) {
char c = out[i];
out[i] = out[j];
out[j] = c;
}
*current = 0;
return current - out;
}
void StringUtil::rtrim(std::string &source) {
std::size_t pos = source.find_last_not_of(" \t\f\v\n\r");
if (pos != std::string::npos) {
source.erase(pos + 1);
} else {
source.clear();
}
}
size_t StringUtil::strlcpy(char *dst, const char *src, size_t size) {
strncpy(dst, src, size - 1);
dst[size - 1] = '\0';
return strlen(src);
}
std::vector<std::string> StringUtil::split(const std::string &source,
char split) {
return StringUtil::split(source, std::string{split});
}
std::vector<std::string> StringUtil::split(const std::string &source,
const std::string &split,
bool keep_empty_string) {
std::vector<std::string> ret;
size_t last_index = 0;
size_t next_index;
if (split.empty()) {
ret.emplace_back(source);
return ret;
}
do {
next_index = source.find(split, last_index);
if (next_index == std::string::npos) {
next_index = source.size();
}
if (next_index != last_index || keep_empty_string) {
ret.emplace_back(subspan(source, last_index, next_index));
}
last_index = next_index + split.size();
} while (next_index != source.size());
return ret;
}
std::string StringUtil::join(const std::vector<std::string> &source,
const std::string &delimiter) {
std::ostringstream buf;
std::copy(source.begin(), source.end(),
std::ostream_iterator<std::string>(buf, delimiter.c_str()));
std::string ret = buf.str();
// copy will always end with an extra delimiter, we remove it here.
return ret.substr(0, ret.length() - delimiter.length());
}
std::string StringUtil::subspan(const std::string &source, size_t start,
size_t end) {
return source.substr(start, end - start);
}
std::string StringUtil::escape(const std::string &source) {
std::string ret;
// Prevent unnecessary allocation by allocating 2x original size.
ret.reserve(source.length() * 2);
for (char c : source) {
switch (c) {
case '\r':
ret += "\\r";
break;
case '\n':
ret += "\\n";
break;
case '\t':
ret += "\\t";
break;
case '"':
ret += "\\\"";
break;
default:
ret += c;
break;
}
}
return ret;
}
bool StringUtil::endsWith(const std::string &source, const std::string &end) {
if (source.length() < end.length()) {
return false;
}
size_t start_position = source.length() - end.length();
return std::equal(source.begin() + start_position, source.end(), end.begin());
}
bool StringUtil::startsWith(const char *source, const std::string &start,
bool case_sensitive) {
if (case_sensitive) {
return strncmp(source, start.c_str(), start.size()) == 0;
} else {
return strncasecmp(source, start.c_str(), start.size()) == 0;
}
}
void JsonUtil::mergeJsons(std::string &target, const std::string &source,
const std::string &field_name) {
rapidjson::Document target_doc, source_doc;
target_doc.Parse(target.c_str());
source_doc.Parse(source.c_str());
target_doc.AddMember(rapidjson::StringRef(field_name.c_str()), source_doc,
target_doc.GetAllocator());
rapidjson::StringBuffer sb;
rapidjson::Writer<rapidjson::StringBuffer> w(sb);
target_doc.Accept(w);
target = sb.GetString();
}
void JsonUtil::addArrayToJson(std::string &target,
const std::vector<std::string> &json_array,
const std::string &field_name) {
std::string stringified_json_array = "[";
if (json_array.size() > 0) {
stringified_json_array += json_array[0];
for (auto it = json_array.begin() + 1; it != json_array.end(); it++) {
stringified_json_array += ",";
stringified_json_array += *it;
}
}
stringified_json_array += "]";
mergeJsons(target, stringified_json_array, field_name);
}
} // namespace zipkin