Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:28:08

0001 // Tencent is pleased to support the open source community by making RapidJSON available.
0002 //
0003 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
0004 //
0005 // Licensed under the MIT License (the "License"); you may not use this file except
0006 // in compliance with the License. You may obtain a copy of the License at
0007 //
0008 // http://opensource.org/licenses/MIT
0009 //
0010 // Unless required by applicable law or agreed to in writing, software distributed
0011 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
0012 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
0013 // specific language governing permissions and limitations under the License.
0014 
0015 #ifndef RAPIDJSON_ITOA_
0016 #define RAPIDJSON_ITOA_
0017 
0018 #include "../rapidjson.h"
0019 
0020 RAPIDJSON_NAMESPACE_BEGIN
0021 namespace internal {
0022 
0023 inline const char* GetDigitsLut() {
0024     static const char cDigitsLut[200] = {
0025         '0','0','0','1','0','2','0','3','0','4','0','5','0','6','0','7','0','8','0','9',
0026         '1','0','1','1','1','2','1','3','1','4','1','5','1','6','1','7','1','8','1','9',
0027         '2','0','2','1','2','2','2','3','2','4','2','5','2','6','2','7','2','8','2','9',
0028         '3','0','3','1','3','2','3','3','3','4','3','5','3','6','3','7','3','8','3','9',
0029         '4','0','4','1','4','2','4','3','4','4','4','5','4','6','4','7','4','8','4','9',
0030         '5','0','5','1','5','2','5','3','5','4','5','5','5','6','5','7','5','8','5','9',
0031         '6','0','6','1','6','2','6','3','6','4','6','5','6','6','6','7','6','8','6','9',
0032         '7','0','7','1','7','2','7','3','7','4','7','5','7','6','7','7','7','8','7','9',
0033         '8','0','8','1','8','2','8','3','8','4','8','5','8','6','8','7','8','8','8','9',
0034         '9','0','9','1','9','2','9','3','9','4','9','5','9','6','9','7','9','8','9','9'
0035     };
0036     return cDigitsLut;
0037 }
0038 
0039 inline char* u32toa(uint32_t value, char* buffer) {
0040     RAPIDJSON_ASSERT(buffer != 0);
0041 
0042     const char* cDigitsLut = GetDigitsLut();
0043 
0044     if (value < 10000) {
0045         const uint32_t d1 = (value / 100) << 1;
0046         const uint32_t d2 = (value % 100) << 1;
0047 
0048         if (value >= 1000)
0049             *buffer++ = cDigitsLut[d1];
0050         if (value >= 100)
0051             *buffer++ = cDigitsLut[d1 + 1];
0052         if (value >= 10)
0053             *buffer++ = cDigitsLut[d2];
0054         *buffer++ = cDigitsLut[d2 + 1];
0055     }
0056     else if (value < 100000000) {
0057         // value = bbbbcccc
0058         const uint32_t b = value / 10000;
0059         const uint32_t c = value % 10000;
0060 
0061         const uint32_t d1 = (b / 100) << 1;
0062         const uint32_t d2 = (b % 100) << 1;
0063 
0064         const uint32_t d3 = (c / 100) << 1;
0065         const uint32_t d4 = (c % 100) << 1;
0066 
0067         if (value >= 10000000)
0068             *buffer++ = cDigitsLut[d1];
0069         if (value >= 1000000)
0070             *buffer++ = cDigitsLut[d1 + 1];
0071         if (value >= 100000)
0072             *buffer++ = cDigitsLut[d2];
0073         *buffer++ = cDigitsLut[d2 + 1];
0074 
0075         *buffer++ = cDigitsLut[d3];
0076         *buffer++ = cDigitsLut[d3 + 1];
0077         *buffer++ = cDigitsLut[d4];
0078         *buffer++ = cDigitsLut[d4 + 1];
0079     }
0080     else {
0081         // value = aabbbbcccc in decimal
0082 
0083         const uint32_t a = value / 100000000; // 1 to 42
0084         value %= 100000000;
0085 
0086         if (a >= 10) {
0087             const unsigned i = a << 1;
0088             *buffer++ = cDigitsLut[i];
0089             *buffer++ = cDigitsLut[i + 1];
0090         }
0091         else
0092             *buffer++ = static_cast<char>('0' + static_cast<char>(a));
0093 
0094         const uint32_t b = value / 10000; // 0 to 9999
0095         const uint32_t c = value % 10000; // 0 to 9999
0096 
0097         const uint32_t d1 = (b / 100) << 1;
0098         const uint32_t d2 = (b % 100) << 1;
0099 
0100         const uint32_t d3 = (c / 100) << 1;
0101         const uint32_t d4 = (c % 100) << 1;
0102 
0103         *buffer++ = cDigitsLut[d1];
0104         *buffer++ = cDigitsLut[d1 + 1];
0105         *buffer++ = cDigitsLut[d2];
0106         *buffer++ = cDigitsLut[d2 + 1];
0107         *buffer++ = cDigitsLut[d3];
0108         *buffer++ = cDigitsLut[d3 + 1];
0109         *buffer++ = cDigitsLut[d4];
0110         *buffer++ = cDigitsLut[d4 + 1];
0111     }
0112     return buffer;
0113 }
0114 
0115 inline char* i32toa(int32_t value, char* buffer) {
0116     RAPIDJSON_ASSERT(buffer != 0);
0117     uint32_t u = static_cast<uint32_t>(value);
0118     if (value < 0) {
0119         *buffer++ = '-';
0120         u = ~u + 1;
0121     }
0122 
0123     return u32toa(u, buffer);
0124 }
0125 
0126 inline char* u64toa(uint64_t value, char* buffer) {
0127     RAPIDJSON_ASSERT(buffer != 0);
0128     const char* cDigitsLut = GetDigitsLut();
0129     const uint64_t  kTen8 = 100000000;
0130     const uint64_t  kTen9 = kTen8 * 10;
0131     const uint64_t kTen10 = kTen8 * 100;
0132     const uint64_t kTen11 = kTen8 * 1000;
0133     const uint64_t kTen12 = kTen8 * 10000;
0134     const uint64_t kTen13 = kTen8 * 100000;
0135     const uint64_t kTen14 = kTen8 * 1000000;
0136     const uint64_t kTen15 = kTen8 * 10000000;
0137     const uint64_t kTen16 = kTen8 * kTen8;
0138 
0139     if (value < kTen8) {
0140         uint32_t v = static_cast<uint32_t>(value);
0141         if (v < 10000) {
0142             const uint32_t d1 = (v / 100) << 1;
0143             const uint32_t d2 = (v % 100) << 1;
0144 
0145             if (v >= 1000)
0146                 *buffer++ = cDigitsLut[d1];
0147             if (v >= 100)
0148                 *buffer++ = cDigitsLut[d1 + 1];
0149             if (v >= 10)
0150                 *buffer++ = cDigitsLut[d2];
0151             *buffer++ = cDigitsLut[d2 + 1];
0152         }
0153         else {
0154             // value = bbbbcccc
0155             const uint32_t b = v / 10000;
0156             const uint32_t c = v % 10000;
0157 
0158             const uint32_t d1 = (b / 100) << 1;
0159             const uint32_t d2 = (b % 100) << 1;
0160 
0161             const uint32_t d3 = (c / 100) << 1;
0162             const uint32_t d4 = (c % 100) << 1;
0163 
0164             if (value >= 10000000)
0165                 *buffer++ = cDigitsLut[d1];
0166             if (value >= 1000000)
0167                 *buffer++ = cDigitsLut[d1 + 1];
0168             if (value >= 100000)
0169                 *buffer++ = cDigitsLut[d2];
0170             *buffer++ = cDigitsLut[d2 + 1];
0171 
0172             *buffer++ = cDigitsLut[d3];
0173             *buffer++ = cDigitsLut[d3 + 1];
0174             *buffer++ = cDigitsLut[d4];
0175             *buffer++ = cDigitsLut[d4 + 1];
0176         }
0177     }
0178     else if (value < kTen16) {
0179         const uint32_t v0 = static_cast<uint32_t>(value / kTen8);
0180         const uint32_t v1 = static_cast<uint32_t>(value % kTen8);
0181 
0182         const uint32_t b0 = v0 / 10000;
0183         const uint32_t c0 = v0 % 10000;
0184 
0185         const uint32_t d1 = (b0 / 100) << 1;
0186         const uint32_t d2 = (b0 % 100) << 1;
0187 
0188         const uint32_t d3 = (c0 / 100) << 1;
0189         const uint32_t d4 = (c0 % 100) << 1;
0190 
0191         const uint32_t b1 = v1 / 10000;
0192         const uint32_t c1 = v1 % 10000;
0193 
0194         const uint32_t d5 = (b1 / 100) << 1;
0195         const uint32_t d6 = (b1 % 100) << 1;
0196 
0197         const uint32_t d7 = (c1 / 100) << 1;
0198         const uint32_t d8 = (c1 % 100) << 1;
0199 
0200         if (value >= kTen15)
0201             *buffer++ = cDigitsLut[d1];
0202         if (value >= kTen14)
0203             *buffer++ = cDigitsLut[d1 + 1];
0204         if (value >= kTen13)
0205             *buffer++ = cDigitsLut[d2];
0206         if (value >= kTen12)
0207             *buffer++ = cDigitsLut[d2 + 1];
0208         if (value >= kTen11)
0209             *buffer++ = cDigitsLut[d3];
0210         if (value >= kTen10)
0211             *buffer++ = cDigitsLut[d3 + 1];
0212         if (value >= kTen9)
0213             *buffer++ = cDigitsLut[d4];
0214 
0215         *buffer++ = cDigitsLut[d4 + 1];
0216         *buffer++ = cDigitsLut[d5];
0217         *buffer++ = cDigitsLut[d5 + 1];
0218         *buffer++ = cDigitsLut[d6];
0219         *buffer++ = cDigitsLut[d6 + 1];
0220         *buffer++ = cDigitsLut[d7];
0221         *buffer++ = cDigitsLut[d7 + 1];
0222         *buffer++ = cDigitsLut[d8];
0223         *buffer++ = cDigitsLut[d8 + 1];
0224     }
0225     else {
0226         const uint32_t a = static_cast<uint32_t>(value / kTen16); // 1 to 1844
0227         value %= kTen16;
0228 
0229         if (a < 10)
0230             *buffer++ = static_cast<char>('0' + static_cast<char>(a));
0231         else if (a < 100) {
0232             const uint32_t i = a << 1;
0233             *buffer++ = cDigitsLut[i];
0234             *buffer++ = cDigitsLut[i + 1];
0235         }
0236         else if (a < 1000) {
0237             *buffer++ = static_cast<char>('0' + static_cast<char>(a / 100));
0238 
0239             const uint32_t i = (a % 100) << 1;
0240             *buffer++ = cDigitsLut[i];
0241             *buffer++ = cDigitsLut[i + 1];
0242         }
0243         else {
0244             const uint32_t i = (a / 100) << 1;
0245             const uint32_t j = (a % 100) << 1;
0246             *buffer++ = cDigitsLut[i];
0247             *buffer++ = cDigitsLut[i + 1];
0248             *buffer++ = cDigitsLut[j];
0249             *buffer++ = cDigitsLut[j + 1];
0250         }
0251 
0252         const uint32_t v0 = static_cast<uint32_t>(value / kTen8);
0253         const uint32_t v1 = static_cast<uint32_t>(value % kTen8);
0254 
0255         const uint32_t b0 = v0 / 10000;
0256         const uint32_t c0 = v0 % 10000;
0257 
0258         const uint32_t d1 = (b0 / 100) << 1;
0259         const uint32_t d2 = (b0 % 100) << 1;
0260 
0261         const uint32_t d3 = (c0 / 100) << 1;
0262         const uint32_t d4 = (c0 % 100) << 1;
0263 
0264         const uint32_t b1 = v1 / 10000;
0265         const uint32_t c1 = v1 % 10000;
0266 
0267         const uint32_t d5 = (b1 / 100) << 1;
0268         const uint32_t d6 = (b1 % 100) << 1;
0269 
0270         const uint32_t d7 = (c1 / 100) << 1;
0271         const uint32_t d8 = (c1 % 100) << 1;
0272 
0273         *buffer++ = cDigitsLut[d1];
0274         *buffer++ = cDigitsLut[d1 + 1];
0275         *buffer++ = cDigitsLut[d2];
0276         *buffer++ = cDigitsLut[d2 + 1];
0277         *buffer++ = cDigitsLut[d3];
0278         *buffer++ = cDigitsLut[d3 + 1];
0279         *buffer++ = cDigitsLut[d4];
0280         *buffer++ = cDigitsLut[d4 + 1];
0281         *buffer++ = cDigitsLut[d5];
0282         *buffer++ = cDigitsLut[d5 + 1];
0283         *buffer++ = cDigitsLut[d6];
0284         *buffer++ = cDigitsLut[d6 + 1];
0285         *buffer++ = cDigitsLut[d7];
0286         *buffer++ = cDigitsLut[d7 + 1];
0287         *buffer++ = cDigitsLut[d8];
0288         *buffer++ = cDigitsLut[d8 + 1];
0289     }
0290 
0291     return buffer;
0292 }
0293 
0294 inline char* i64toa(int64_t value, char* buffer) {
0295     RAPIDJSON_ASSERT(buffer != 0);
0296     uint64_t u = static_cast<uint64_t>(value);
0297     if (value < 0) {
0298         *buffer++ = '-';
0299         u = ~u + 1;
0300     }
0301 
0302     return u64toa(u, buffer);
0303 }
0304 
0305 } // namespace internal
0306 RAPIDJSON_NAMESPACE_END
0307 
0308 #endif // RAPIDJSON_ITOA_