summaryrefslogtreecommitdiff
path: root/src/mbgl/util/dtoa.cpp
blob: 6ca3e19c3d2188e37760067fb178aee12aa4d433 (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
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
#include "dtoa.hpp"

// Clang/C2 on Windows 64-bits can't parse rapidjson's dtoa
// and it was causing the compiler to crash.
#if !defined(_WINDOWS)
#include <rapidjson/internal/dtoa.h>
#endif

namespace mbgl {
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());
    return data;
}

#else

std::string dtoa(double value) {
    return std::to_string(value);
}

#endif

} // namespace util
} // namespace mbgl