summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/mbgl/util/string.hpp2
-rw-r--r--src/mbgl/util/string.cpp19
-rw-r--r--test/util/string.test.cpp11
3 files changed, 32 insertions, 0 deletions
diff --git a/include/mbgl/util/string.hpp b/include/mbgl/util/string.hpp
index 1cca386e21..f8a63acdf9 100644
--- a/include/mbgl/util/string.hpp
+++ b/include/mbgl/util/string.hpp
@@ -53,6 +53,8 @@ std::string toString(float, bool decimal = false);
std::string toString(double, bool decimal = false);
std::string toString(long double, bool decimal = false);
+std::string toHex(size_t);
+
inline std::string toString(std::exception_ptr error) {
assert(error);
diff --git a/src/mbgl/util/string.cpp b/src/mbgl/util/string.cpp
index b20b3fb7ed..06ccb56968 100644
--- a/src/mbgl/util/string.cpp
+++ b/src/mbgl/util/string.cpp
@@ -15,6 +15,25 @@ std::string toString(double num, bool decimal) {
std::string toString(long double num, bool decimal) {
return dtoa(num, decimal);
}
+
+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;
+}
+
+}
+
+std::string toHex(size_t value) {
+ return toPaddedHex(value);
}
} // namespace util
diff --git a/test/util/string.test.cpp b/test/util/string.test.cpp
index 08e8e9f759..91e1b93685 100644
--- a/test/util/string.test.cpp
+++ b/test/util/string.test.cpp
@@ -2,6 +2,8 @@
#include <mbgl/util/string.hpp>
+#include <cstdint>
+
using namespace mbgl;
TEST(ToString, FloatingPoint) {
@@ -16,3 +18,12 @@ TEST(ToString, FloatingPoint) {
EXPECT_EQ("12340000000", util::toString(12340000000.0));
EXPECT_EQ("12340000000.0", util::toString(12340000000.0, true));
}
+
+TEST(ToHex, SIZE_T) {
+#if INTPTR_MAX == INT32_MAX
+ EXPECT_EQ("a715b247", util::toHex((size_t)0xa715b247));
+#elif INTPTR_MAX == INT64_MAX
+ EXPECT_EQ("a715b247df38cc29", util::toHex((size_t)0xa715b247df38cc29));
+#endif
+
+}