Monero
Loading...
Searching...
No Matches
itoa.h
Go to the documentation of this file.
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. All rights reserved.
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
21namespace internal {
22
23inline 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
39inline 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
115inline 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
126inline 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 if (value >= kTen8)
215 *buffer++ = cDigitsLut[d4 + 1];
216
217 *buffer++ = cDigitsLut[d5];
218 *buffer++ = cDigitsLut[d5 + 1];
219 *buffer++ = cDigitsLut[d6];
220 *buffer++ = cDigitsLut[d6 + 1];
221 *buffer++ = cDigitsLut[d7];
222 *buffer++ = cDigitsLut[d7 + 1];
223 *buffer++ = cDigitsLut[d8];
224 *buffer++ = cDigitsLut[d8 + 1];
225 }
226 else {
227 const uint32_t a = static_cast<uint32_t>(value / kTen16); // 1 to 1844
228 value %= kTen16;
229
230 if (a < 10)
231 *buffer++ = static_cast<char>('0' + static_cast<char>(a));
232 else if (a < 100) {
233 const uint32_t i = a << 1;
234 *buffer++ = cDigitsLut[i];
235 *buffer++ = cDigitsLut[i + 1];
236 }
237 else if (a < 1000) {
238 *buffer++ = static_cast<char>('0' + static_cast<char>(a / 100));
239
240 const uint32_t i = (a % 100) << 1;
241 *buffer++ = cDigitsLut[i];
242 *buffer++ = cDigitsLut[i + 1];
243 }
244 else {
245 const uint32_t i = (a / 100) << 1;
246 const uint32_t j = (a % 100) << 1;
247 *buffer++ = cDigitsLut[i];
248 *buffer++ = cDigitsLut[i + 1];
249 *buffer++ = cDigitsLut[j];
250 *buffer++ = cDigitsLut[j + 1];
251 }
252
253 const uint32_t v0 = static_cast<uint32_t>(value / kTen8);
254 const uint32_t v1 = static_cast<uint32_t>(value % kTen8);
255
256 const uint32_t b0 = v0 / 10000;
257 const uint32_t c0 = v0 % 10000;
258
259 const uint32_t d1 = (b0 / 100) << 1;
260 const uint32_t d2 = (b0 % 100) << 1;
261
262 const uint32_t d3 = (c0 / 100) << 1;
263 const uint32_t d4 = (c0 % 100) << 1;
264
265 const uint32_t b1 = v1 / 10000;
266 const uint32_t c1 = v1 % 10000;
267
268 const uint32_t d5 = (b1 / 100) << 1;
269 const uint32_t d6 = (b1 % 100) << 1;
270
271 const uint32_t d7 = (c1 / 100) << 1;
272 const uint32_t d8 = (c1 % 100) << 1;
273
274 *buffer++ = cDigitsLut[d1];
275 *buffer++ = cDigitsLut[d1 + 1];
276 *buffer++ = cDigitsLut[d2];
277 *buffer++ = cDigitsLut[d2 + 1];
278 *buffer++ = cDigitsLut[d3];
279 *buffer++ = cDigitsLut[d3 + 1];
280 *buffer++ = cDigitsLut[d4];
281 *buffer++ = cDigitsLut[d4 + 1];
282 *buffer++ = cDigitsLut[d5];
283 *buffer++ = cDigitsLut[d5 + 1];
284 *buffer++ = cDigitsLut[d6];
285 *buffer++ = cDigitsLut[d6 + 1];
286 *buffer++ = cDigitsLut[d7];
287 *buffer++ = cDigitsLut[d7 + 1];
288 *buffer++ = cDigitsLut[d8];
289 *buffer++ = cDigitsLut[d8 + 1];
290 }
291
292 return buffer;
293}
294
295inline char* i64toa(int64_t value, char* buffer) {
296 RAPIDJSON_ASSERT(buffer != 0);
297 uint64_t u = static_cast<uint64_t>(value);
298 if (value < 0) {
299 *buffer++ = '-';
300 u = ~u + 1;
301 }
302
303 return u64toa(u, buffer);
304}
305
306} // namespace internal
308
309#endif // RAPIDJSON_ITOA_
#define v0(p)
Definition aesb.c:116
#define v1(p)
Definition aesb.c:117
cryptonote::block b
Definition block.cpp:40
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition rapidjson.h:411
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition rapidjson.h:121
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition rapidjson.h:124
Definition d2.py:1
Definition document.h:406
char * u64toa(uint64_t value, char *buffer)
Definition itoa.h:126
char * i64toa(int64_t value, char *buffer)
Definition itoa.h:295
const char * GetDigitsLut()
Definition itoa.h:23
char * u32toa(uint32_t value, char *buffer)
Definition itoa.h:39
char * i32toa(int32_t value, char *buffer)
Definition itoa.h:115
const GenericPointer< typename T::ValueType > T2 value
Definition pointer.h:1225
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1124
common definitions and configuration
signed __int64 int64_t
Definition stdint.h:135
unsigned int uint32_t
Definition stdint.h:126
signed int int32_t
Definition stdint.h:123
unsigned __int64 uint64_t
Definition stdint.h:136