aboutsummaryrefslogtreecommitdiff
path: root/external/rapidjson/internal/dtoa.h
diff options
context:
space:
mode:
Diffstat (limited to 'external/rapidjson/internal/dtoa.h')
-rw-r--r--external/rapidjson/internal/dtoa.h42
1 files changed, 35 insertions, 7 deletions
diff --git a/external/rapidjson/internal/dtoa.h b/external/rapidjson/internal/dtoa.h
index d04ae21cc..8d6350e62 100644
--- a/external/rapidjson/internal/dtoa.h
+++ b/external/rapidjson/internal/dtoa.h
@@ -29,6 +29,7 @@ namespace internal {
#ifdef __GNUC__
RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(effc++)
+RAPIDJSON_DIAG_OFF(array-bounds) // some gcc versions generate wrong warnings https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59124
#endif
inline void GrisuRound(char* buffer, int len, uint64_t delta, uint64_t rest, uint64_t ten_kappa, uint64_t wp_w) {
@@ -101,7 +102,8 @@ inline void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buff
kappa--;
if (p2 < delta) {
*K += kappa;
- GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * kPow10[-static_cast<int>(kappa)]);
+ int index = -static_cast<int>(kappa);
+ GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * (index < 9 ? kPow10[-static_cast<int>(kappa)] : 0));
return;
}
}
@@ -145,10 +147,10 @@ inline char* WriteExponent(int K, char* buffer) {
return buffer;
}
-inline char* Prettify(char* buffer, int length, int k) {
+inline char* Prettify(char* buffer, int length, int k, int maxDecimalPlaces) {
const int kk = length + k; // 10^(kk-1) <= v < 10^kk
- if (length <= kk && kk <= 21) {
+ if (0 <= k && kk <= 21) {
// 1234e7 -> 12340000000
for (int i = length; i < kk; i++)
buffer[i] = '0';
@@ -160,7 +162,16 @@ inline char* Prettify(char* buffer, int length, int k) {
// 1234e-2 -> 12.34
std::memmove(&buffer[kk + 1], &buffer[kk], static_cast<size_t>(length - kk));
buffer[kk] = '.';
- return &buffer[length + 1];
+ 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
@@ -170,7 +181,23 @@ inline char* Prettify(char* buffer, int length, int k) {
buffer[1] = '.';
for (int i = 2; i < offset; i++)
buffer[i] = '0';
- return &buffer[length + offset];
+ 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';
+ buffer[1] = '.';
+ buffer[2] = '0';
+ return &buffer[3];
}
else if (length == 1) {
// 1e30
@@ -186,7 +213,8 @@ inline char* Prettify(char* buffer, int length, int k) {
}
}
-inline char* dtoa(double value, char* buffer) {
+inline char* dtoa(double value, char* buffer, int maxDecimalPlaces = 324) {
+ RAPIDJSON_ASSERT(maxDecimalPlaces >= 1);
Double d(value);
if (d.IsZero()) {
if (d.Sign())
@@ -203,7 +231,7 @@ inline char* dtoa(double value, char* buffer) {
}
int length, K;
Grisu2(value, buffer, &length, &K);
- return Prettify(buffer, length, K);
+ return Prettify(buffer, length, K, maxDecimalPlaces);
}
}