19 #ifndef RAPIDJSON_DTOA_
20 #define RAPIDJSON_DTOA_
31 RAPIDJSON_DIAG_OFF(effc++)
32 RAPIDJSON_DIAG_OFF(array-bounds)
36 while (rest < wp_w && delta - rest >= ten_kappa &&
37 (rest + ten_kappa < wp_w ||
38 wp_w - rest > rest + ten_kappa - wp_w)) {
47 if (n < 100)
return 2;
48 if (n < 1000)
return 3;
49 if (n < 10000)
return 4;
50 if (n < 100000)
return 5;
51 if (n < 1000000)
return 6;
52 if (n < 10000000)
return 7;
53 if (n < 100000000)
return 8;
61 static const uint32_t kPow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
63 const DiyFp wp_w = Mp - W;
72 case 9: d = p1 / 100000000; p1 %= 100000000;
break;
73 case 8: d = p1 / 10000000; p1 %= 10000000;
break;
74 case 7: d = p1 / 1000000; p1 %= 1000000;
break;
75 case 6: d = p1 / 100000; p1 %= 100000;
break;
76 case 5: d = p1 / 10000; p1 %= 10000;
break;
77 case 4: d = p1 / 1000; p1 %= 1000;
break;
78 case 3: d = p1 / 100; p1 %= 100;
break;
79 case 2: d = p1 / 10; p1 %= 10;
break;
80 case 1: d = p1; p1 = 0;
break;
84 buffer[(*len)++] =
static_cast<char>(
'0' +
static_cast<char>(d));
98 char d =
static_cast<char>(p2 >> -one.
e);
100 buffer[(*len)++] =
static_cast<char>(
'0' + d);
105 int index = -
static_cast<int>(kappa);
106 GrisuRound(buffer, *len, delta, p2, one.
f, wp_w.
f * (index < 9 ? kPow10[-
static_cast<int>(kappa)] : 0));
112 inline void Grisu2(
double value,
char* buffer,
int* length,
int* K) {
119 DiyFp Wp = w_p * c_mk;
120 DiyFp Wm = w_m * c_mk;
123 DigitGen(W, Wp, Wp.
f - Wm.
f, buffer, length, K);
133 *buffer++ =
static_cast<char>(
'0' +
static_cast<char>(K / 100));
145 *buffer++ =
static_cast<char>(
'0' +
static_cast<char>(K));
150 inline char*
Prettify(
char* buffer,
int length,
int k,
int maxDecimalPlaces) {
151 const int kk = length + k;
153 if (0 <= k && kk <= 21) {
155 for (
int i = length; i < kk; i++)
158 buffer[kk + 1] =
'0';
159 return &buffer[kk + 2];
161 else if (0 < kk && kk <= 21) {
163 std::memmove(&buffer[kk + 1], &buffer[kk],
static_cast<size_t>(length - kk));
165 if (0 > k + maxDecimalPlaces) {
168 for (
int i = kk + maxDecimalPlaces; i > kk + 1; i--)
169 if (buffer[i] !=
'0')
170 return &buffer[i + 1];
171 return &buffer[kk + 2];
174 return &buffer[length + 1];
176 else if (-6 < kk && kk <= 0) {
178 const int offset = 2 - kk;
179 std::memmove(&buffer[offset], &buffer[0],
static_cast<size_t>(length));
182 for (
int i = 2; i < offset; i++)
184 if (length - kk > maxDecimalPlaces) {
187 for (
int i = maxDecimalPlaces + 1; i > 2; i--)
188 if (buffer[i] !=
'0')
189 return &buffer[i + 1];
193 return &buffer[length + offset];
195 else if (kk < -maxDecimalPlaces) {
202 else if (length == 1) {
209 std::memmove(&buffer[2], &buffer[1],
static_cast<size_t>(length - 1));
211 buffer[length + 1] =
'e';
216 inline char*
dtoa(
double value,
char* buffer,
int maxDecimalPlaces = 324) {
234 return Prettify(buffer, length, K, maxDecimalPlaces);
245 #endif // RAPIDJSON_DTOA_