1 // Tencent is pleased to support the open source community by making RapidJSON available.
3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
5 // Licensed under the MIT License (the "License"); you may not use this file except
6 // in compliance with the License. You may obtain a copy of the License at
8 // http://opensource.org/licenses/MIT
10 // Unless required by applicable law or agreed to in writing, software distributed
11 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13 // specific language governing permissions and limitations under the License.
15 #ifndef RAPIDJSON_ITOA_
16 #define RAPIDJSON_ITOA_
18 #include "../rapidjson.h"
20 RAPIDJSON_NAMESPACE_BEGIN
23 inline const char* GetDigitsLut() {
24 static const char cDigitsLut[200] = {
25 '0','0','0','1','0','2','0','3','0','4','0','5','0','6','0','7','0','8','0','9',
26 '1','0','1','1','1','2','1','3','1','4','1','5','1','6','1','7','1','8','1','9',
27 '2','0','2','1','2','2','2','3','2','4','2','5','2','6','2','7','2','8','2','9',
28 '3','0','3','1','3','2','3','3','3','4','3','5','3','6','3','7','3','8','3','9',
29 '4','0','4','1','4','2','4','3','4','4','4','5','4','6','4','7','4','8','4','9',
30 '5','0','5','1','5','2','5','3','5','4','5','5','5','6','5','7','5','8','5','9',
31 '6','0','6','1','6','2','6','3','6','4','6','5','6','6','6','7','6','8','6','9',
32 '7','0','7','1','7','2','7','3','7','4','7','5','7','6','7','7','7','8','7','9',
33 '8','0','8','1','8','2','8','3','8','4','8','5','8','6','8','7','8','8','8','9',
34 '9','0','9','1','9','2','9','3','9','4','9','5','9','6','9','7','9','8','9','9'
39 inline char* u32toa(uint32_t value, char* buffer) {
40 RAPIDJSON_ASSERT(buffer != 0);
42 const char* cDigitsLut = GetDigitsLut();
45 const uint32_t d1 = (value / 100) << 1;
46 const uint32_t d2 = (value % 100) << 1;
49 *buffer++ = cDigitsLut[d1];
51 *buffer++ = cDigitsLut[d1 + 1];
53 *buffer++ = cDigitsLut[d2];
54 *buffer++ = cDigitsLut[d2 + 1];
56 else if (value < 100000000) {
58 const uint32_t b = value / 10000;
59 const uint32_t c = value % 10000;
61 const uint32_t d1 = (b / 100) << 1;
62 const uint32_t d2 = (b % 100) << 1;
64 const uint32_t d3 = (c / 100) << 1;
65 const uint32_t d4 = (c % 100) << 1;
67 if (value >= 10000000)
68 *buffer++ = cDigitsLut[d1];
70 *buffer++ = cDigitsLut[d1 + 1];
72 *buffer++ = cDigitsLut[d2];
73 *buffer++ = cDigitsLut[d2 + 1];
75 *buffer++ = cDigitsLut[d3];
76 *buffer++ = cDigitsLut[d3 + 1];
77 *buffer++ = cDigitsLut[d4];
78 *buffer++ = cDigitsLut[d4 + 1];
81 // value = aabbbbcccc in decimal
83 const uint32_t a = value / 100000000; // 1 to 42
87 const unsigned i = a << 1;
88 *buffer++ = cDigitsLut[i];
89 *buffer++ = cDigitsLut[i + 1];
92 *buffer++ = static_cast<char>('0' + static_cast<char>(a));
94 const uint32_t b = value / 10000; // 0 to 9999
95 const uint32_t c = value % 10000; // 0 to 9999
97 const uint32_t d1 = (b / 100) << 1;
98 const uint32_t d2 = (b % 100) << 1;
100 const uint32_t d3 = (c / 100) << 1;
101 const uint32_t d4 = (c % 100) << 1;
103 *buffer++ = cDigitsLut[d1];
104 *buffer++ = cDigitsLut[d1 + 1];
105 *buffer++ = cDigitsLut[d2];
106 *buffer++ = cDigitsLut[d2 + 1];
107 *buffer++ = cDigitsLut[d3];
108 *buffer++ = cDigitsLut[d3 + 1];
109 *buffer++ = cDigitsLut[d4];
110 *buffer++ = cDigitsLut[d4 + 1];
115 inline char* i32toa(int32_t value, char* buffer) {
116 RAPIDJSON_ASSERT(buffer != 0);
117 uint32_t u = static_cast<uint32_t>(value);
123 return u32toa(u, buffer);
126 inline char* u64toa(uint64_t value, char* buffer) {
127 RAPIDJSON_ASSERT(buffer != 0);
128 const char* cDigitsLut = GetDigitsLut();
129 const uint64_t kTen8 = 100000000;
130 const uint64_t kTen9 = kTen8 * 10;
131 const uint64_t kTen10 = kTen8 * 100;
132 const uint64_t kTen11 = kTen8 * 1000;
133 const uint64_t kTen12 = kTen8 * 10000;
134 const uint64_t kTen13 = kTen8 * 100000;
135 const uint64_t kTen14 = kTen8 * 1000000;
136 const uint64_t kTen15 = kTen8 * 10000000;
137 const uint64_t kTen16 = kTen8 * kTen8;
140 uint32_t v = static_cast<uint32_t>(value);
142 const uint32_t d1 = (v / 100) << 1;
143 const uint32_t d2 = (v % 100) << 1;
146 *buffer++ = cDigitsLut[d1];
148 *buffer++ = cDigitsLut[d1 + 1];
150 *buffer++ = cDigitsLut[d2];
151 *buffer++ = cDigitsLut[d2 + 1];
155 const uint32_t b = v / 10000;
156 const uint32_t c = v % 10000;
158 const uint32_t d1 = (b / 100) << 1;
159 const uint32_t d2 = (b % 100) << 1;
161 const uint32_t d3 = (c / 100) << 1;
162 const uint32_t d4 = (c % 100) << 1;
164 if (value >= 10000000)
165 *buffer++ = cDigitsLut[d1];
166 if (value >= 1000000)
167 *buffer++ = cDigitsLut[d1 + 1];
169 *buffer++ = cDigitsLut[d2];
170 *buffer++ = cDigitsLut[d2 + 1];
172 *buffer++ = cDigitsLut[d3];
173 *buffer++ = cDigitsLut[d3 + 1];
174 *buffer++ = cDigitsLut[d4];
175 *buffer++ = cDigitsLut[d4 + 1];
178 else if (value < kTen16) {
179 const uint32_t v0 = static_cast<uint32_t>(value / kTen8);
180 const uint32_t v1 = static_cast<uint32_t>(value % kTen8);
182 const uint32_t b0 = v0 / 10000;
183 const uint32_t c0 = v0 % 10000;
185 const uint32_t d1 = (b0 / 100) << 1;
186 const uint32_t d2 = (b0 % 100) << 1;
188 const uint32_t d3 = (c0 / 100) << 1;
189 const uint32_t d4 = (c0 % 100) << 1;
191 const uint32_t b1 = v1 / 10000;
192 const uint32_t c1 = v1 % 10000;
194 const uint32_t d5 = (b1 / 100) << 1;
195 const uint32_t d6 = (b1 % 100) << 1;
197 const uint32_t d7 = (c1 / 100) << 1;
198 const uint32_t d8 = (c1 % 100) << 1;
201 *buffer++ = cDigitsLut[d1];
203 *buffer++ = cDigitsLut[d1 + 1];
205 *buffer++ = cDigitsLut[d2];
207 *buffer++ = cDigitsLut[d2 + 1];
209 *buffer++ = cDigitsLut[d3];
211 *buffer++ = cDigitsLut[d3 + 1];
213 *buffer++ = cDigitsLut[d4];
215 *buffer++ = cDigitsLut[d4 + 1];
216 *buffer++ = cDigitsLut[d5];
217 *buffer++ = cDigitsLut[d5 + 1];
218 *buffer++ = cDigitsLut[d6];
219 *buffer++ = cDigitsLut[d6 + 1];
220 *buffer++ = cDigitsLut[d7];
221 *buffer++ = cDigitsLut[d7 + 1];
222 *buffer++ = cDigitsLut[d8];
223 *buffer++ = cDigitsLut[d8 + 1];
226 const uint32_t a = static_cast<uint32_t>(value / kTen16); // 1 to 1844
230 *buffer++ = static_cast<char>('0' + static_cast<char>(a));
232 const uint32_t i = a << 1;
233 *buffer++ = cDigitsLut[i];
234 *buffer++ = cDigitsLut[i + 1];
237 *buffer++ = static_cast<char>('0' + static_cast<char>(a / 100));
239 const uint32_t i = (a % 100) << 1;
240 *buffer++ = cDigitsLut[i];
241 *buffer++ = cDigitsLut[i + 1];
244 const uint32_t i = (a / 100) << 1;
245 const uint32_t j = (a % 100) << 1;
246 *buffer++ = cDigitsLut[i];
247 *buffer++ = cDigitsLut[i + 1];
248 *buffer++ = cDigitsLut[j];
249 *buffer++ = cDigitsLut[j + 1];
252 const uint32_t v0 = static_cast<uint32_t>(value / kTen8);
253 const uint32_t v1 = static_cast<uint32_t>(value % kTen8);
255 const uint32_t b0 = v0 / 10000;
256 const uint32_t c0 = v0 % 10000;
258 const uint32_t d1 = (b0 / 100) << 1;
259 const uint32_t d2 = (b0 % 100) << 1;
261 const uint32_t d3 = (c0 / 100) << 1;
262 const uint32_t d4 = (c0 % 100) << 1;
264 const uint32_t b1 = v1 / 10000;
265 const uint32_t c1 = v1 % 10000;
267 const uint32_t d5 = (b1 / 100) << 1;
268 const uint32_t d6 = (b1 % 100) << 1;
270 const uint32_t d7 = (c1 / 100) << 1;
271 const uint32_t d8 = (c1 % 100) << 1;
273 *buffer++ = cDigitsLut[d1];
274 *buffer++ = cDigitsLut[d1 + 1];
275 *buffer++ = cDigitsLut[d2];
276 *buffer++ = cDigitsLut[d2 + 1];
277 *buffer++ = cDigitsLut[d3];
278 *buffer++ = cDigitsLut[d3 + 1];
279 *buffer++ = cDigitsLut[d4];
280 *buffer++ = cDigitsLut[d4 + 1];
281 *buffer++ = cDigitsLut[d5];
282 *buffer++ = cDigitsLut[d5 + 1];
283 *buffer++ = cDigitsLut[d6];
284 *buffer++ = cDigitsLut[d6 + 1];
285 *buffer++ = cDigitsLut[d7];
286 *buffer++ = cDigitsLut[d7 + 1];
287 *buffer++ = cDigitsLut[d8];
288 *buffer++ = cDigitsLut[d8 + 1];
294 inline char* i64toa(int64_t value, char* buffer) {
295 RAPIDJSON_ASSERT(buffer != 0);
296 uint64_t u = static_cast<uint64_t>(value);
302 return u64toa(u, buffer);
305 } // namespace internal
306 RAPIDJSON_NAMESPACE_END
308 #endif // RAPIDJSON_ITOA_