1
1
// Tencent is pleased to support the open source community by making RapidJSON available.
2
- //
2
+ //
3
3
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
4
4
//
5
5
// Licensed under the MIT License (the "License"); you may not use this file except
6
6
// in compliance with the License. You may obtain a copy of the License at
7
7
//
8
8
// http://opensource.org/licenses/MIT
9
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
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
13
// specific language governing permissions and limitations under the License.
14
14
15
15
#ifndef RAPIDJSON_ITOA_
@@ -37,12 +37,14 @@ inline const char* GetDigitsLut() {
37
37
}
38
38
39
39
inline char * u32toa (uint32_t value, char * buffer) {
40
+ RAPIDJSON_ASSERT (buffer != 0 );
41
+
40
42
const char * cDigitsLut = GetDigitsLut ();
41
43
42
44
if (value < 10000 ) {
43
45
const uint32_t d1 = (value / 100 ) << 1 ;
44
46
const uint32_t d2 = (value % 100 ) << 1 ;
45
-
47
+
46
48
if (value >= 1000 )
47
49
*buffer++ = cDigitsLut[d1];
48
50
if (value >= 100 )
@@ -55,32 +57,32 @@ inline char* u32toa(uint32_t value, char* buffer) {
55
57
// value = bbbbcccc
56
58
const uint32_t b = value / 10000 ;
57
59
const uint32_t c = value % 10000 ;
58
-
60
+
59
61
const uint32_t d1 = (b / 100 ) << 1 ;
60
62
const uint32_t d2 = (b % 100 ) << 1 ;
61
-
63
+
62
64
const uint32_t d3 = (c / 100 ) << 1 ;
63
65
const uint32_t d4 = (c % 100 ) << 1 ;
64
-
66
+
65
67
if (value >= 10000000 )
66
68
*buffer++ = cDigitsLut[d1];
67
69
if (value >= 1000000 )
68
70
*buffer++ = cDigitsLut[d1 + 1 ];
69
71
if (value >= 100000 )
70
72
*buffer++ = cDigitsLut[d2];
71
73
*buffer++ = cDigitsLut[d2 + 1 ];
72
-
74
+
73
75
*buffer++ = cDigitsLut[d3];
74
76
*buffer++ = cDigitsLut[d3 + 1 ];
75
77
*buffer++ = cDigitsLut[d4];
76
78
*buffer++ = cDigitsLut[d4 + 1 ];
77
79
}
78
80
else {
79
81
// value = aabbbbcccc in decimal
80
-
82
+
81
83
const uint32_t a = value / 100000000 ; // 1 to 42
82
84
value %= 100000000 ;
83
-
85
+
84
86
if (a >= 10 ) {
85
87
const unsigned i = a << 1 ;
86
88
*buffer++ = cDigitsLut[i];
@@ -91,13 +93,13 @@ inline char* u32toa(uint32_t value, char* buffer) {
91
93
92
94
const uint32_t b = value / 10000 ; // 0 to 9999
93
95
const uint32_t c = value % 10000 ; // 0 to 9999
94
-
96
+
95
97
const uint32_t d1 = (b / 100 ) << 1 ;
96
98
const uint32_t d2 = (b % 100 ) << 1 ;
97
-
99
+
98
100
const uint32_t d3 = (c / 100 ) << 1 ;
99
101
const uint32_t d4 = (c % 100 ) << 1 ;
100
-
102
+
101
103
*buffer++ = cDigitsLut[d1];
102
104
*buffer++ = cDigitsLut[d1 + 1 ];
103
105
*buffer++ = cDigitsLut[d2];
@@ -111,6 +113,7 @@ inline char* u32toa(uint32_t value, char* buffer) {
111
113
}
112
114
113
115
inline char * i32toa (int32_t value, char * buffer) {
116
+ RAPIDJSON_ASSERT (buffer != 0 );
114
117
uint32_t u = static_cast <uint32_t >(value);
115
118
if (value < 0 ) {
116
119
*buffer++ = ' -' ;
@@ -121,6 +124,7 @@ inline char* i32toa(int32_t value, char* buffer) {
121
124
}
122
125
123
126
inline char * u64toa (uint64_t value, char * buffer) {
127
+ RAPIDJSON_ASSERT (buffer != 0 );
124
128
const char * cDigitsLut = GetDigitsLut ();
125
129
const uint64_t kTen8 = 100000000 ;
126
130
const uint64_t kTen9 = kTen8 * 10 ;
@@ -131,13 +135,13 @@ inline char* u64toa(uint64_t value, char* buffer) {
131
135
const uint64_t kTen14 = kTen8 * 1000000 ;
132
136
const uint64_t kTen15 = kTen8 * 10000000 ;
133
137
const uint64_t kTen16 = kTen8 * kTen8 ;
134
-
138
+
135
139
if (value < kTen8 ) {
136
140
uint32_t v = static_cast <uint32_t >(value);
137
141
if (v < 10000 ) {
138
142
const uint32_t d1 = (v / 100 ) << 1 ;
139
143
const uint32_t d2 = (v % 100 ) << 1 ;
140
-
144
+
141
145
if (v >= 1000 )
142
146
*buffer++ = cDigitsLut[d1];
143
147
if (v >= 100 )
@@ -150,21 +154,21 @@ inline char* u64toa(uint64_t value, char* buffer) {
150
154
// value = bbbbcccc
151
155
const uint32_t b = v / 10000 ;
152
156
const uint32_t c = v % 10000 ;
153
-
157
+
154
158
const uint32_t d1 = (b / 100 ) << 1 ;
155
159
const uint32_t d2 = (b % 100 ) << 1 ;
156
-
160
+
157
161
const uint32_t d3 = (c / 100 ) << 1 ;
158
162
const uint32_t d4 = (c % 100 ) << 1 ;
159
-
163
+
160
164
if (value >= 10000000 )
161
165
*buffer++ = cDigitsLut[d1];
162
166
if (value >= 1000000 )
163
167
*buffer++ = cDigitsLut[d1 + 1 ];
164
168
if (value >= 100000 )
165
169
*buffer++ = cDigitsLut[d2];
166
170
*buffer++ = cDigitsLut[d2 + 1 ];
167
-
171
+
168
172
*buffer++ = cDigitsLut[d3];
169
173
*buffer++ = cDigitsLut[d3 + 1 ];
170
174
*buffer++ = cDigitsLut[d4];
@@ -174,22 +178,22 @@ inline char* u64toa(uint64_t value, char* buffer) {
174
178
else if (value < kTen16 ) {
175
179
const uint32_t v0 = static_cast <uint32_t >(value / kTen8 );
176
180
const uint32_t v1 = static_cast <uint32_t >(value % kTen8 );
177
-
181
+
178
182
const uint32_t b0 = v0 / 10000 ;
179
183
const uint32_t c0 = v0 % 10000 ;
180
-
184
+
181
185
const uint32_t d1 = (b0 / 100 ) << 1 ;
182
186
const uint32_t d2 = (b0 % 100 ) << 1 ;
183
-
187
+
184
188
const uint32_t d3 = (c0 / 100 ) << 1 ;
185
189
const uint32_t d4 = (c0 % 100 ) << 1 ;
186
190
187
191
const uint32_t b1 = v1 / 10000 ;
188
192
const uint32_t c1 = v1 % 10000 ;
189
-
193
+
190
194
const uint32_t d5 = (b1 / 100 ) << 1 ;
191
195
const uint32_t d6 = (b1 % 100 ) << 1 ;
192
-
196
+
193
197
const uint32_t d7 = (c1 / 100 ) << 1 ;
194
198
const uint32_t d8 = (c1 % 100 ) << 1 ;
195
199
@@ -209,7 +213,7 @@ inline char* u64toa(uint64_t value, char* buffer) {
209
213
*buffer++ = cDigitsLut[d4];
210
214
if (value >= kTen8 )
211
215
*buffer++ = cDigitsLut[d4 + 1 ];
212
-
216
+
213
217
*buffer++ = cDigitsLut[d5];
214
218
*buffer++ = cDigitsLut[d5 + 1 ];
215
219
*buffer++ = cDigitsLut[d6];
@@ -222,7 +226,7 @@ inline char* u64toa(uint64_t value, char* buffer) {
222
226
else {
223
227
const uint32_t a = static_cast <uint32_t >(value / kTen16 ); // 1 to 1844
224
228
value %= kTen16 ;
225
-
229
+
226
230
if (a < 10 )
227
231
*buffer++ = static_cast <char >(' 0' + static_cast <char >(a));
228
232
else if (a < 100 ) {
@@ -232,7 +236,7 @@ inline char* u64toa(uint64_t value, char* buffer) {
232
236
}
233
237
else if (a < 1000 ) {
234
238
*buffer++ = static_cast <char >(' 0' + static_cast <char >(a / 100 ));
235
-
239
+
236
240
const uint32_t i = (a % 100 ) << 1 ;
237
241
*buffer++ = cDigitsLut[i];
238
242
*buffer++ = cDigitsLut[i + 1 ];
@@ -245,28 +249,28 @@ inline char* u64toa(uint64_t value, char* buffer) {
245
249
*buffer++ = cDigitsLut[j];
246
250
*buffer++ = cDigitsLut[j + 1 ];
247
251
}
248
-
252
+
249
253
const uint32_t v0 = static_cast <uint32_t >(value / kTen8 );
250
254
const uint32_t v1 = static_cast <uint32_t >(value % kTen8 );
251
-
255
+
252
256
const uint32_t b0 = v0 / 10000 ;
253
257
const uint32_t c0 = v0 % 10000 ;
254
-
258
+
255
259
const uint32_t d1 = (b0 / 100 ) << 1 ;
256
260
const uint32_t d2 = (b0 % 100 ) << 1 ;
257
-
261
+
258
262
const uint32_t d3 = (c0 / 100 ) << 1 ;
259
263
const uint32_t d4 = (c0 % 100 ) << 1 ;
260
-
264
+
261
265
const uint32_t b1 = v1 / 10000 ;
262
266
const uint32_t c1 = v1 % 10000 ;
263
-
267
+
264
268
const uint32_t d5 = (b1 / 100 ) << 1 ;
265
269
const uint32_t d6 = (b1 % 100 ) << 1 ;
266
-
270
+
267
271
const uint32_t d7 = (c1 / 100 ) << 1 ;
268
272
const uint32_t d8 = (c1 % 100 ) << 1 ;
269
-
273
+
270
274
*buffer++ = cDigitsLut[d1];
271
275
*buffer++ = cDigitsLut[d1 + 1 ];
272
276
*buffer++ = cDigitsLut[d2];
@@ -284,11 +288,12 @@ inline char* u64toa(uint64_t value, char* buffer) {
284
288
*buffer++ = cDigitsLut[d8];
285
289
*buffer++ = cDigitsLut[d8 + 1 ];
286
290
}
287
-
291
+
288
292
return buffer;
289
293
}
290
294
291
295
inline char * i64toa (int64_t value, char * buffer) {
296
+ RAPIDJSON_ASSERT (buffer != 0 );
292
297
uint64_t u = static_cast <uint64_t >(value);
293
298
if (value < 0 ) {
294
299
*buffer++ = ' -' ;
0 commit comments