1 // Copyright (C) 2011 Milo Yip
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20
21 #ifndef RAPIDJSON_ITOA_
22 #define RAPIDJSON_ITOA_
23
24 RAPIDJSON_NAMESPACE_BEGIN
25 namespace internal {
26
GetDigitsLut()27 inline const char* GetDigitsLut() {
28 static const char cDigitsLut[200] = {
29 '0','0','0','1','0','2','0','3','0','4','0','5','0','6','0','7','0','8','0','9',
30 '1','0','1','1','1','2','1','3','1','4','1','5','1','6','1','7','1','8','1','9',
31 '2','0','2','1','2','2','2','3','2','4','2','5','2','6','2','7','2','8','2','9',
32 '3','0','3','1','3','2','3','3','3','4','3','5','3','6','3','7','3','8','3','9',
33 '4','0','4','1','4','2','4','3','4','4','4','5','4','6','4','7','4','8','4','9',
34 '5','0','5','1','5','2','5','3','5','4','5','5','5','6','5','7','5','8','5','9',
35 '6','0','6','1','6','2','6','3','6','4','6','5','6','6','6','7','6','8','6','9',
36 '7','0','7','1','7','2','7','3','7','4','7','5','7','6','7','7','7','8','7','9',
37 '8','0','8','1','8','2','8','3','8','4','8','5','8','6','8','7','8','8','8','9',
38 '9','0','9','1','9','2','9','3','9','4','9','5','9','6','9','7','9','8','9','9'
39 };
40 return cDigitsLut;
41 }
42
u32toa(uint32_t value,char * buffer)43 inline char* u32toa(uint32_t value, char* buffer) {
44 const char* cDigitsLut = GetDigitsLut();
45
46 if (value < 10000) {
47 const uint32_t d1 = (value / 100) << 1;
48 const uint32_t d2 = (value % 100) << 1;
49
50 if (value >= 1000)
51 *buffer++ = cDigitsLut[d1];
52 if (value >= 100)
53 *buffer++ = cDigitsLut[d1 + 1];
54 if (value >= 10)
55 *buffer++ = cDigitsLut[d2];
56 *buffer++ = cDigitsLut[d2 + 1];
57 }
58 else if (value < 100000000) {
59 // value = bbbbcccc
60 const uint32_t b = value / 10000;
61 const uint32_t c = value % 10000;
62
63 const uint32_t d1 = (b / 100) << 1;
64 const uint32_t d2 = (b % 100) << 1;
65
66 const uint32_t d3 = (c / 100) << 1;
67 const uint32_t d4 = (c % 100) << 1;
68
69 if (value >= 10000000)
70 *buffer++ = cDigitsLut[d1];
71 if (value >= 1000000)
72 *buffer++ = cDigitsLut[d1 + 1];
73 if (value >= 100000)
74 *buffer++ = cDigitsLut[d2];
75 *buffer++ = cDigitsLut[d2 + 1];
76
77 *buffer++ = cDigitsLut[d3];
78 *buffer++ = cDigitsLut[d3 + 1];
79 *buffer++ = cDigitsLut[d4];
80 *buffer++ = cDigitsLut[d4 + 1];
81 }
82 else {
83 // value = aabbbbcccc in decimal
84
85 const uint32_t a = value / 100000000; // 1 to 42
86 value %= 100000000;
87
88 if (a >= 10) {
89 const unsigned i = a << 1;
90 *buffer++ = cDigitsLut[i];
91 *buffer++ = cDigitsLut[i + 1];
92 }
93 else
94 *buffer++ = static_cast<char>('0' + static_cast<char>(a));
95
96 const uint32_t b = value / 10000; // 0 to 9999
97 const uint32_t c = value % 10000; // 0 to 9999
98
99 const uint32_t d1 = (b / 100) << 1;
100 const uint32_t d2 = (b % 100) << 1;
101
102 const uint32_t d3 = (c / 100) << 1;
103 const uint32_t d4 = (c % 100) << 1;
104
105 *buffer++ = cDigitsLut[d1];
106 *buffer++ = cDigitsLut[d1 + 1];
107 *buffer++ = cDigitsLut[d2];
108 *buffer++ = cDigitsLut[d2 + 1];
109 *buffer++ = cDigitsLut[d3];
110 *buffer++ = cDigitsLut[d3 + 1];
111 *buffer++ = cDigitsLut[d4];
112 *buffer++ = cDigitsLut[d4 + 1];
113 }
114 return buffer;
115 }
116
i32toa(int32_t value,char * buffer)117 inline char* i32toa(int32_t value, char* buffer) {
118 if (value < 0) {
119 *buffer++ = '-';
120 value = -value;
121 }
122
123 return u32toa(static_cast<uint32_t>(value), buffer);
124 }
125
u64toa(uint64_t value,char * buffer)126 inline char* u64toa(uint64_t value, char* buffer) {
127 const char* cDigitsLut = GetDigitsLut();
128 const uint64_t kTen8 = 100000000;
129 const uint64_t kTen9 = kTen8 * 10;
130 const uint64_t kTen10 = kTen8 * 100;
131 const uint64_t kTen11 = kTen8 * 1000;
132 const uint64_t kTen12 = kTen8 * 10000;
133 const uint64_t kTen13 = kTen8 * 100000;
134 const uint64_t kTen14 = kTen8 * 1000000;
135 const uint64_t kTen15 = kTen8 * 10000000;
136 const uint64_t kTen16 = kTen8 * kTen8;
137
138 if (value < kTen8) {
139 uint32_t v = static_cast<uint32_t>(value);
140 if (v < 10000) {
141 const uint32_t d1 = (v / 100) << 1;
142 const uint32_t d2 = (v % 100) << 1;
143
144 if (v >= 1000)
145 *buffer++ = cDigitsLut[d1];
146 if (v >= 100)
147 *buffer++ = cDigitsLut[d1 + 1];
148 if (v >= 10)
149 *buffer++ = cDigitsLut[d2];
150 *buffer++ = cDigitsLut[d2 + 1];
151 }
152 else {
153 // value = bbbbcccc
154 const uint32_t b = v / 10000;
155 const uint32_t c = v % 10000;
156
157 const uint32_t d1 = (b / 100) << 1;
158 const uint32_t d2 = (b % 100) << 1;
159
160 const uint32_t d3 = (c / 100) << 1;
161 const uint32_t d4 = (c % 100) << 1;
162
163 if (value >= 10000000)
164 *buffer++ = cDigitsLut[d1];
165 if (value >= 1000000)
166 *buffer++ = cDigitsLut[d1 + 1];
167 if (value >= 100000)
168 *buffer++ = cDigitsLut[d2];
169 *buffer++ = cDigitsLut[d2 + 1];
170
171 *buffer++ = cDigitsLut[d3];
172 *buffer++ = cDigitsLut[d3 + 1];
173 *buffer++ = cDigitsLut[d4];
174 *buffer++ = cDigitsLut[d4 + 1];
175 }
176 }
177 else if (value < kTen16) {
178 const uint32_t v0 = static_cast<uint32_t>(value / kTen8);
179 const uint32_t v1 = static_cast<uint32_t>(value % kTen8);
180
181 const uint32_t b0 = v0 / 10000;
182 const uint32_t c0 = v0 % 10000;
183
184 const uint32_t d1 = (b0 / 100) << 1;
185 const uint32_t d2 = (b0 % 100) << 1;
186
187 const uint32_t d3 = (c0 / 100) << 1;
188 const uint32_t d4 = (c0 % 100) << 1;
189
190 const uint32_t b1 = v1 / 10000;
191 const uint32_t c1 = v1 % 10000;
192
193 const uint32_t d5 = (b1 / 100) << 1;
194 const uint32_t d6 = (b1 % 100) << 1;
195
196 const uint32_t d7 = (c1 / 100) << 1;
197 const uint32_t d8 = (c1 % 100) << 1;
198
199 if (value >= kTen15)
200 *buffer++ = cDigitsLut[d1];
201 if (value >= kTen14)
202 *buffer++ = cDigitsLut[d1 + 1];
203 if (value >= kTen13)
204 *buffer++ = cDigitsLut[d2];
205 if (value >= kTen12)
206 *buffer++ = cDigitsLut[d2 + 1];
207 if (value >= kTen11)
208 *buffer++ = cDigitsLut[d3];
209 if (value >= kTen10)
210 *buffer++ = cDigitsLut[d3 + 1];
211 if (value >= kTen9)
212 *buffer++ = cDigitsLut[d4];
213 if (value >= kTen8)
214 *buffer++ = cDigitsLut[d4 + 1];
215
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
i64toa(int64_t value,char * buffer)294 inline char* i64toa(int64_t value, char* buffer) {
295 if (value < 0) {
296 *buffer++ = '-';
297 value = -value;
298 }
299
300 return u64toa(static_cast<uint64_t>(value), buffer);
301 }
302
303 } // namespace internal
304 RAPIDJSON_NAMESPACE_END
305
306 #endif // RAPIDJSON_ITOA_
307