summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2018-10-18 21:02:17 +0200
committerKonstantin Käfer <mail@kkaefer.com>2018-10-23 12:23:40 +0200
commit7692be5d1571f2c02303aec1eb27432f634d0db2 (patch)
tree89c7e2f4fa6dacd5e6f883edf7e390020742982f /src
parent59c5754fa8225d8f1f674b2589d4be29e0716dd6 (diff)
downloadqtlocation-mapboxgl-7692be5d1571f2c02303aec1eb27432f634d0db2.tar.gz
[core] refactor util::toString to use RapidJSON's stringification
Diffstat (limited to 'src')
-rw-r--r--src/mbgl/util/string.cpp62
1 files changed, 55 insertions, 7 deletions
diff --git a/src/mbgl/util/string.cpp b/src/mbgl/util/string.cpp
index 06ccb56968..2f737e7436 100644
--- a/src/mbgl/util/string.cpp
+++ b/src/mbgl/util/string.cpp
@@ -1,19 +1,67 @@
#include <mbgl/util/string.hpp>
-#include <mbgl/util/dtoa.hpp>
+
+#include <rapidjson/writer.h>
+#include <rapidjson/stringbuffer.h>
+
+#include <cassert>
namespace mbgl {
namespace util {
-std::string toString(float num, bool decimal) {
- return dtoa(num, decimal);
+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(double num, bool decimal) {
- return dtoa(num, decimal);
+std::string toString(int64_t t) {
+ rapidjson::StringBuffer s;
+ rapidjson::Writer<rapidjson::StringBuffer> writer(s);
+ writer.Int64(t);
+ return s.GetString();
}
-std::string toString(long double num, bool decimal) {
- return dtoa(num, decimal);
+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(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 {