diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2018-10-17 15:45:35 +0200 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2018-10-23 12:23:40 +0200 |
commit | decbde16165cf3e4455edd0c5caeac991d0c01e6 (patch) | |
tree | 3acadad0daeb1ec134fc872e1a90eac2d1e86325 | |
parent | ea51b11dccdc117c7dc43ffd3b3d0f969f59799f (diff) | |
download | qtlocation-mapboxgl-decbde16165cf3e4455edd0c5caeac991d0c01e6.tar.gz |
[core] deduplicate dtoa() from RapidJSON and add tests
-rw-r--r-- | cmake/test-files.txt | 1 | ||||
-rw-r--r-- | src/mbgl/util/dtoa.cpp | 97 | ||||
-rw-r--r-- | test/util/string.test.cpp | 13 |
3 files changed, 21 insertions, 90 deletions
diff --git a/cmake/test-files.txt b/cmake/test-files.txt index 89283e2aea..4c7029f9f0 100644 --- a/cmake/test-files.txt +++ b/cmake/test-files.txt @@ -144,6 +144,7 @@ test/util/peer.test.cpp test/util/position.test.cpp test/util/projection.test.cpp test/util/run_loop.test.cpp +test/util/string.test.cpp test/util/text_conversions.test.cpp test/util/thread.test.cpp test/util/thread_local.test.cpp diff --git a/src/mbgl/util/dtoa.cpp b/src/mbgl/util/dtoa.cpp index 0e3aef6117..fce1f122b7 100644 --- a/src/mbgl/util/dtoa.cpp +++ b/src/mbgl/util/dtoa.cpp @@ -13,99 +13,16 @@ namespace util { #if !defined(_WINDOWS) -namespace { - -// From https://github.com/miloyip/rapidjson/blob/master/include/rapidjson/internal/dtoa.h - -char* Prettify(char* buffer, int length, int k) { - constexpr int maxDecimalPlaces = 324; - const int kk = length + k; // 10^(kk-1) <= v < 10^kk - - if (0 <= k && kk <= 21) { - // 1234e7 -> 12340000000 - for (int i = length; i < kk; i++) - buffer[i] = '0'; - return &buffer[kk]; - } - else if (0 < kk && kk <= 21) { - // 1234e-2 -> 12.34 - std::memmove(&buffer[kk + 1], &buffer[kk], static_cast<size_t>(length - kk)); - buffer[kk] = '.'; - if (0 > k + maxDecimalPlaces) { - // When maxDecimalPlaces = 2, 1.2345 -> 1.23, 1.102 -> 1.1 - // Remove extra trailing zeros (at least one) after truncation. - for (int i = kk + maxDecimalPlaces; i > kk + 1; i--) - if (buffer[i] != '0') - return &buffer[i + 1]; - return &buffer[kk + 2]; // Reserve one zero - } - else - return &buffer[length + 1]; - } - else if (-6 < kk && kk <= 0) { - // 1234e-6 -> 0.001234 - const int offset = 2 - kk; - std::memmove(&buffer[offset], &buffer[0], static_cast<size_t>(length)); - buffer[0] = '0'; - buffer[1] = '.'; - for (int i = 2; i < offset; i++) - buffer[i] = '0'; - if (length - kk > maxDecimalPlaces) { - // When maxDecimalPlaces = 2, 0.123 -> 0.12, 0.102 -> 0.1 - // Remove extra trailing zeros (at least one) after truncation. - for (int i = maxDecimalPlaces + 1; i > 2; i--) - if (buffer[i] != '0') - return &buffer[i + 1]; - return &buffer[3]; // Reserve one zero - } - else - return &buffer[length + offset]; - } - else if (kk < -maxDecimalPlaces) { - // Truncate to zero - buffer[0] = '0'; - return &buffer[1]; - } - else if (length == 1) { - // 1e30 - buffer[1] = 'e'; - return rapidjson::internal::WriteExponent(kk - 1, &buffer[2]); - } - else { - // 1234e30 -> 1.234e33 - std::memmove(&buffer[2], &buffer[1], static_cast<size_t>(length - 1)); - buffer[1] = '.'; - buffer[length + 1] = 'e'; - return rapidjson::internal::WriteExponent(kk - 1, &buffer[0 + length + 2]); - } -} - -} // namespace - -char* dtoa(double value, char* buffer) { - rapidjson::internal::Double d(value); - if (d.IsZero()) { - if (d.Sign()) - *buffer++ = '-'; // -0.0, Issue #289 - buffer[0] = '0'; - return &buffer[1]; - } - else { - if (value < 0) { - *buffer++ = '-'; - value = -value; - } - int length, K; - rapidjson::internal::Grisu2(value, buffer, &length, &K); - return Prettify(buffer, length, K); - } -} - std::string dtoa(double value) { std::string data; data.resize(25); - auto end = dtoa(value, const_cast<char*>(data.data())); - data.resize(end - data.data()); + auto end = rapidjson::internal::dtoa(value, const_cast<char*>(data.data())); + auto length = end - data.data(); + if (length >= 3 && end[-1] == '0' && end[-2] == '.') { + // Remove trailing ".0" for integers + length -= 2; + } + data.resize(length); return data; } diff --git a/test/util/string.test.cpp b/test/util/string.test.cpp new file mode 100644 index 0000000000..06e6ca9e9e --- /dev/null +++ b/test/util/string.test.cpp @@ -0,0 +1,13 @@ +#include <mbgl/test/util.hpp> + +#include <mbgl/util/string.hpp> + +using namespace mbgl; + +TEST(ToString, FloatingPoint) { + EXPECT_EQ("0", util::toString(0.0)); + EXPECT_EQ("1.33", util::toString(1.33)); + EXPECT_EQ("-1", util::toString(-1.0)); + EXPECT_EQ("12340000000", util::toString(12340000000.0)); + EXPECT_EQ("12340000000", util::toString(12340000000.0)); +} |