blob: 718da5f1dea4836c726b27975eed4ef818fcb800 (
plain)
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
|
#include <mbgl/util/string.hpp>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include <cassert>
namespace mbgl {
namespace util {
std::string toString(int32_t t) {
rapidjson::StringBuffer s;
rapidjson::Writer<rapidjson::StringBuffer> writer(s);
writer.Int(t);
return s.GetString();
}
std::string toString(uint32_t t) {
rapidjson::StringBuffer s;
rapidjson::Writer<rapidjson::StringBuffer> writer(s);
writer.Uint(t);
return s.GetString();
}
std::string toString(int64_t t) {
rapidjson::StringBuffer s;
rapidjson::Writer<rapidjson::StringBuffer> writer(s);
writer.Int64(t);
return s.GetString();
}
std::string toString(uint64_t t) {
rapidjson::StringBuffer s;
rapidjson::Writer<rapidjson::StringBuffer> writer(s);
writer.Uint64(t);
return s.GetString();
}
std::string toString(double t, bool decimal) {
rapidjson::StringBuffer s;
rapidjson::Writer<rapidjson::StringBuffer> writer(s);
writer.Double(t);
std::string data = s.GetString();
if (!decimal && data.length() >= 3 && data[data.length() - 1] == '0' && data[data.length() - 2] == '.') {
// Remove trailing ".0" for integers
data.resize(data.length() - 2);
}
return data;
}
std::string toString(const std::exception_ptr& error) {
assert(error);
if (!error) {
return "(null)";
}
try {
std::rethrow_exception(error);
} catch (const std::exception& ex) {
return ex.what();
} catch (...) {
return "Unknown exception type";
}
}
namespace {
template <typename T>
std::string toPaddedHex(T x) {
std::string result;
result.resize(sizeof(T) * 2);
for (int index = sizeof(T) * 2 - 1; index >= 0; index--) {
const int digit = x & 0x0F;
result[index] = '0' + digit + (digit > 9 ? 39 : 0);
x >>= 4;
}
return result;
}
} // namespace
std::string toHex(uint32_t value) {
return toPaddedHex(value);
}
std::string toHex(uint64_t value) {
return toPaddedHex(value);
}
} // namespace util
} // namespace mbgl
|