Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:27:25

0001 // Copyright 2017 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //      https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 //
0015 // This file contains common things needed by numbers_test.cc,
0016 // numbers_legacy_test.cc and numbers_benchmark.cc.
0017 
0018 #ifndef ABSL_STRINGS_INTERNAL_NUMBERS_TEST_COMMON_H_
0019 #define ABSL_STRINGS_INTERNAL_NUMBERS_TEST_COMMON_H_
0020 
0021 #include <array>
0022 #include <cstdint>
0023 #include <limits>
0024 #include <string>
0025 
0026 #include "absl/base/config.h"
0027 
0028 namespace absl {
0029 ABSL_NAMESPACE_BEGIN
0030 namespace strings_internal {
0031 
0032 template <typename IntType>
0033 inline bool Itoa(IntType value, int base, std::string* destination) {
0034   destination->clear();
0035   if (base <= 1 || base > 36) {
0036     return false;
0037   }
0038 
0039   if (value == 0) {
0040     destination->push_back('0');
0041     return true;
0042   }
0043 
0044   bool negative = value < 0;
0045   while (value != 0) {
0046     const IntType next_value = value / base;
0047     // Can't use std::abs here because of problems when IntType is unsigned.
0048     int remainder =
0049         static_cast<int>(value > next_value * base ? value - next_value * base
0050                                                    : next_value * base - value);
0051     char c = remainder < 10 ? '0' + remainder : 'A' + remainder - 10;
0052     destination->insert(0, 1, c);
0053     value = next_value;
0054   }
0055 
0056   if (negative) {
0057     destination->insert(0, 1, '-');
0058   }
0059   return true;
0060 }
0061 
0062 struct uint32_test_case {
0063   const char* str;
0064   bool expect_ok;
0065   int base;  // base to pass to the conversion function
0066   uint32_t expected;
0067 };
0068 
0069 inline const std::array<uint32_test_case, 27>& strtouint32_test_cases() {
0070   static const std::array<uint32_test_case, 27> test_cases{{
0071       {"0xffffffff", true, 16, (std::numeric_limits<uint32_t>::max)()},
0072       {"0x34234324", true, 16, 0x34234324},
0073       {"34234324", true, 16, 0x34234324},
0074       {"0", true, 16, 0},
0075       {" \t\n 0xffffffff", true, 16, (std::numeric_limits<uint32_t>::max)()},
0076       {" \f\v 46", true, 10, 46},  // must accept weird whitespace
0077       {" \t\n 72717222", true, 8, 072717222},
0078       {" \t\n 072717222", true, 8, 072717222},
0079       {" \t\n 072717228", false, 8, 07271722},
0080       {"0", true, 0, 0},
0081 
0082       // Base-10 version.
0083       {"34234324", true, 0, 34234324},
0084       {"4294967295", true, 0, (std::numeric_limits<uint32_t>::max)()},
0085       {"34234324 \n\t", true, 10, 34234324},
0086 
0087       // Unusual base
0088       {"0", true, 3, 0},
0089       {"2", true, 3, 2},
0090       {"11", true, 3, 4},
0091 
0092       // Invalid uints.
0093       {"", false, 0, 0},
0094       {"  ", false, 0, 0},
0095       {"abc", false, 0, 0},  // would be valid hex, but prefix is missing
0096       {"34234324a", false, 0, 34234324},
0097       {"34234.3", false, 0, 34234},
0098       {"-1", false, 0, 0},
0099       {"   -123", false, 0, 0},
0100       {" \t\n -123", false, 0, 0},
0101 
0102       // Out of bounds.
0103       {"4294967296", false, 0, (std::numeric_limits<uint32_t>::max)()},
0104       {"0x100000000", false, 0, (std::numeric_limits<uint32_t>::max)()},
0105       {nullptr, false, 0, 0},
0106   }};
0107   return test_cases;
0108 }
0109 
0110 struct uint64_test_case {
0111   const char* str;
0112   bool expect_ok;
0113   int base;
0114   uint64_t expected;
0115 };
0116 
0117 inline const std::array<uint64_test_case, 34>& strtouint64_test_cases() {
0118   static const std::array<uint64_test_case, 34> test_cases{{
0119       {"0x3423432448783446", true, 16, int64_t{0x3423432448783446}},
0120       {"3423432448783446", true, 16, int64_t{0x3423432448783446}},
0121 
0122       {"0", true, 16, 0},
0123       {"000", true, 0, 0},
0124       {"0", true, 0, 0},
0125       {" \t\n 0xffffffffffffffff", true, 16,
0126        (std::numeric_limits<uint64_t>::max)()},
0127 
0128       {"012345670123456701234", true, 8, int64_t{012345670123456701234}},
0129       {"12345670123456701234", true, 8, int64_t{012345670123456701234}},
0130 
0131       {"12845670123456701234", false, 8, 0},
0132 
0133       // Base-10 version.
0134       {"34234324487834466", true, 0, int64_t{34234324487834466}},
0135 
0136       {" \t\n 18446744073709551615", true, 0,
0137        (std::numeric_limits<uint64_t>::max)()},
0138 
0139       {"34234324487834466 \n\t ", true, 0, int64_t{34234324487834466}},
0140 
0141       {" \f\v 46", true, 10, 46},  // must accept weird whitespace
0142 
0143       // Unusual base
0144       {"0", true, 3, 0},
0145       {"2", true, 3, 2},
0146       {"11", true, 3, 4},
0147 
0148       {"0", true, 0, 0},
0149 
0150       // Invalid uints.
0151       {"", false, 0, 0},
0152       {"  ", false, 0, 0},
0153       {"abc", false, 0, 0},
0154       {"34234324487834466a", false, 0, 0},
0155       {"34234487834466.3", false, 0, 0},
0156       {"-1", false, 0, 0},
0157       {"   -123", false, 0, 0},
0158       {" \t\n -123", false, 0, 0},
0159 
0160       // Out of bounds.
0161       {"18446744073709551616", false, 10, 0},
0162       {"18446744073709551616", false, 0, 0},
0163       {"0x10000000000000000", false, 16,
0164        (std::numeric_limits<uint64_t>::max)()},
0165       {"0X10000000000000000", false, 16,
0166        (std::numeric_limits<uint64_t>::max)()},  // 0X versus 0x.
0167       {"0x10000000000000000", false, 0, (std::numeric_limits<uint64_t>::max)()},
0168       {"0X10000000000000000", false, 0,
0169        (std::numeric_limits<uint64_t>::max)()},  // 0X versus 0x.
0170 
0171       {"0x1234", true, 16, 0x1234},
0172 
0173       // Base-10 string version.
0174       {"1234", true, 0, 1234},
0175       {nullptr, false, 0, 0},
0176   }};
0177   return test_cases;
0178 }
0179 
0180 }  // namespace strings_internal
0181 ABSL_NAMESPACE_END
0182 }  // namespace absl
0183 
0184 #endif  // ABSL_STRINGS_INTERNAL_NUMBERS_TEST_COMMON_H_