File indexing completed on 2025-01-18 09:27:25
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
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
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;
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},
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
0083 {"34234324", true, 0, 34234324},
0084 {"4294967295", true, 0, (std::numeric_limits<uint32_t>::max)()},
0085 {"34234324 \n\t", true, 10, 34234324},
0086
0087
0088 {"0", true, 3, 0},
0089 {"2", true, 3, 2},
0090 {"11", true, 3, 4},
0091
0092
0093 {"", false, 0, 0},
0094 {" ", false, 0, 0},
0095 {"abc", false, 0, 0},
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
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
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},
0142
0143
0144 {"0", true, 3, 0},
0145 {"2", true, 3, 2},
0146 {"11", true, 3, 4},
0147
0148 {"0", true, 0, 0},
0149
0150
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
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)()},
0167 {"0x10000000000000000", false, 0, (std::numeric_limits<uint64_t>::max)()},
0168 {"0X10000000000000000", false, 0,
0169 (std::numeric_limits<uint64_t>::max)()},
0170
0171 {"0x1234", true, 16, 0x1234},
0172
0173
0174 {"1234", true, 0, 1234},
0175 {nullptr, false, 0, 0},
0176 }};
0177 return test_cases;
0178 }
0179
0180 }
0181 ABSL_NAMESPACE_END
0182 }
0183
0184 #endif