RapidJSON: include/rapidjson/internal/itoa.h Source File

itoa.h
1 // Tencent is pleased to support the open source community by making RapidJSON available.
2 //
3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
4 //
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
7 //
8 // http://opensource.org/licenses/MIT
9 //
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.
14
15 #ifndef RAPIDJSON_ITOA_
16 #define RAPIDJSON_ITOA_
17
18 #include "../rapidjson.h"
19
20 RAPIDJSON_NAMESPACE_BEGIN
21 namespace internal {
22
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'
35 };
36 return cDigitsLut;
37 }
38
39 inline char* u32toa(uint32_t value, char* buffer) {
40 RAPIDJSON_ASSERT(buffer != 0);
41
42 const char* cDigitsLut = GetDigitsLut();
43
44 if (value < 10000) {
45 const uint32_t d1 = (value / 100) << 1;
46 const uint32_t d2 = (value % 100) << 1;
47
48 if (value >= 1000)
49 *buffer++ = cDigitsLut[d1];
50 if (value >= 100)
51 *buffer++ = cDigitsLut[d1 + 1];
52 if (value >= 10)
53 *buffer++ = cDigitsLut[d2];
54 *buffer++ = cDigitsLut[d2 + 1];
55 }
56 else if (value < 100000000) {
57 // value = bbbbcccc
58 const uint32_t b = value / 10000;
59 const uint32_t c = value % 10000;
60
61 const uint32_t d1 = (b / 100) << 1;
62 const uint32_t d2 = (b % 100) << 1;
63
64 const uint32_t d3 = (c / 100) << 1;
65 const uint32_t d4 = (c % 100) << 1;
66
67 if (value >= 10000000)
68 *buffer++ = cDigitsLut[d1];
69 if (value >= 1000000)
70 *buffer++ = cDigitsLut[d1 + 1];
71 if (value >= 100000)
72 *buffer++ = cDigitsLut[d2];
73 *buffer++ = cDigitsLut[d2 + 1];
74
75 *buffer++ = cDigitsLut[d3];
76 *buffer++ = cDigitsLut[d3 + 1];
77 *buffer++ = cDigitsLut[d4];
78 *buffer++ = cDigitsLut[d4 + 1];
79 }
80 else {
81 // value = aabbbbcccc in decimal
82
83 const uint32_t a = value / 100000000; // 1 to 42
84 value %= 100000000;
85
86 if (a >= 10) {
87 const unsigned i = a << 1;
88 *buffer++ = cDigitsLut[i];
89 *buffer++ = cDigitsLut[i + 1];
90 }
91 else
92 *buffer++ = static_cast<char>('0' + static_cast<char>(a));
93
94 const uint32_t b = value / 10000; // 0 to 9999
95 const uint32_t c = value % 10000; // 0 to 9999
96
97 const uint32_t d1 = (b / 100) << 1;
98 const uint32_t d2 = (b % 100) << 1;
99
100 const uint32_t d3 = (c / 100) << 1;
101 const uint32_t d4 = (c % 100) << 1;
102
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];
111 }
112 return buffer;
113 }
114
115 inline char* i32toa(int32_t value, char* buffer) {
116 RAPIDJSON_ASSERT(buffer != 0);
117 uint32_t u = static_cast<uint32_t>(value);
118 if (value < 0) {
119 *buffer++ = '-';
120 u = ~u + 1;
121 }
122
123 return u32toa(u, buffer);
124 }
125
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;
138
139 if (value < kTen8) {
140 uint32_t v = static_cast<uint32_t>(value);
141 if (v < 10000) {
142 const uint32_t d1 = (v / 100) << 1;
143 const uint32_t d2 = (v % 100) << 1;
144
145 if (v >= 1000)
146 *buffer++ = cDigitsLut[d1];
147 if (v >= 100)
148 *buffer++ = cDigitsLut[d1 + 1];
149 if (v >= 10)
150 *buffer++ = cDigitsLut[d2];
151 *buffer++ = cDigitsLut[d2 + 1];
152 }
153 else {
154 // value = bbbbcccc
155 const uint32_t b = v / 10000;
156 const uint32_t c = v % 10000;
157
158 const uint32_t d1 = (b / 100) << 1;
159 const uint32_t d2 = (b % 100) << 1;
160
161 const uint32_t d3 = (c / 100) << 1;
162 const uint32_t d4 = (c % 100) << 1;
163
164 if (value >= 10000000)
165 *buffer++ = cDigitsLut[d1];
166 if (value >= 1000000)
167 *buffer++ = cDigitsLut[d1 + 1];
168 if (value >= 100000)
169 *buffer++ = cDigitsLut[d2];
170 *buffer++ = cDigitsLut[d2 + 1];
171
172 *buffer++ = cDigitsLut[d3];
173 *buffer++ = cDigitsLut[d3 + 1];
174 *buffer++ = cDigitsLut[d4];
175 *buffer++ = cDigitsLut[d4 + 1];
176 }
177 }
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);
181
182 const uint32_t b0 = v0 / 10000;
183 const uint32_t c0 = v0 % 10000;
184
185 const uint32_t d1 = (b0 / 100) << 1;
186 const uint32_t d2 = (b0 % 100) << 1;
187
188 const uint32_t d3 = (c0 / 100) << 1;
189 const uint32_t d4 = (c0 % 100) << 1;
190
191 const uint32_t b1 = v1 / 10000;
192 const uint32_t c1 = v1 % 10000;
193
194 const uint32_t d5 = (b1 / 100) << 1;
195 const uint32_t d6 = (b1 % 100) << 1;
196
197 const uint32_t d7 = (c1 / 100) << 1;
198 const uint32_t d8 = (c1 % 100) << 1;
199
200 if (value >= kTen15)
201 *buffer++ = cDigitsLut[d1];
202 if (value >= kTen14)
203 *buffer++ = cDigitsLut[d1 + 1];
204 if (value >= kTen13)
205 *buffer++ = cDigitsLut[d2];
206 if (value >= kTen12)
207 *buffer++ = cDigitsLut[d2 + 1];
208 if (value >= kTen11)
209 *buffer++ = cDigitsLut[d3];
210 if (value >= kTen10)
211 *buffer++ = cDigitsLut[d3 + 1];
212 if (value >= kTen9)
213 *buffer++ = cDigitsLut[d4];
214
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];
224 }
225 else {
226 const uint32_t a = static_cast<uint32_t>(value / kTen16); // 1 to 1844
227 value %= kTen16;
228
229 if (a < 10)
230 *buffer++ = static_cast<char>('0' + static_cast<char>(a));
231 else if (a < 100) {
232 const uint32_t i = a << 1;
233 *buffer++ = cDigitsLut[i];
234 *buffer++ = cDigitsLut[i + 1];
235 }
236 else if (a < 1000) {
237 *buffer++ = static_cast<char>('0' + static_cast<char>(a / 100));
238
239 const uint32_t i = (a % 100) << 1;
240 *buffer++ = cDigitsLut[i];
241 *buffer++ = cDigitsLut[i + 1];
242 }
243 else {
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];
250 }
251
252 const uint32_t v0 = static_cast<uint32_t>(value / kTen8);
253 const uint32_t v1 = static_cast<uint32_t>(value % kTen8);
254
255 const uint32_t b0 = v0 / 10000;
256 const uint32_t c0 = v0 % 10000;
257
258 const uint32_t d1 = (b0 / 100) << 1;
259 const uint32_t d2 = (b0 % 100) << 1;
260
261 const uint32_t d3 = (c0 / 100) << 1;
262 const uint32_t d4 = (c0 % 100) << 1;
263
264 const uint32_t b1 = v1 / 10000;
265 const uint32_t c1 = v1 % 10000;
266
267 const uint32_t d5 = (b1 / 100) << 1;
268 const uint32_t d6 = (b1 % 100) << 1;
269
270 const uint32_t d7 = (c1 / 100) << 1;
271 const uint32_t d8 = (c1 % 100) << 1;
272
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];
289 }
290
291 return buffer;
292 }
293
294 inline char* i64toa(int64_t value, char* buffer) {
295 RAPIDJSON_ASSERT(buffer != 0);
296 uint64_t u = static_cast<uint64_t>(value);
297 if (value < 0) {
298 *buffer++ = '-';
299 u = ~u + 1;
300 }
301
302 return u64toa(u, buffer);
303 }
304
305 } // namespace internal
306 RAPIDJSON_NAMESPACE_END
307
308 #endif // RAPIDJSON_ITOA_
RAPIDJSON_ASSERT
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:406