18 #ifndef ABSL_STRINGS_INTERNAL_NUMBERS_TEST_COMMON_H_ 19 #define ABSL_STRINGS_INTERNAL_NUMBERS_TEST_COMMON_H_ 27 namespace strings_internal {
29 template <
typename IntType>
30 inline bool Itoa(IntType
value,
int base, std::string* destination) {
32 if (base <= 1 || base > 36) {
37 destination->push_back(
'0');
41 bool negative = value < 0;
43 const IntType next_value = value / base;
45 int remainder = value > next_value * base ? value - next_value * base
46 : next_value * base -
value;
47 char c = remainder < 10 ?
'0' + remainder :
'A' + remainder - 10;
48 destination->insert(0, 1, c);
53 destination->insert(0, 1,
'-');
66 static const std::array<uint32_test_case, 27> test_cases{{
67 {
"0xffffffff",
true, 16, (std::numeric_limits<uint32_t>::max)()},
68 {
"0x34234324",
true, 16, 0x34234324},
69 {
"34234324",
true, 16, 0x34234324},
71 {
" \t\n 0xffffffff",
true, 16, (std::numeric_limits<uint32_t>::max)()},
72 {
" \f\v 46",
true, 10, 46},
73 {
" \t\n 72717222",
true, 8, 072717222},
74 {
" \t\n 072717222",
true, 8, 072717222},
75 {
" \t\n 072717228",
false, 8, 07271722},
79 {
"34234324",
true, 0, 34234324},
80 {
"4294967295",
true, 0, (std::numeric_limits<uint32_t>::max)()},
81 {
"34234324 \n\t",
true, 10, 34234324},
92 {
"34234324a",
false, 0, 34234324},
93 {
"34234.3",
false, 0, 34234},
95 {
" -123",
false, 0, 0},
96 {
" \t\n -123",
false, 0, 0},
99 {
"4294967296",
false, 0, (std::numeric_limits<uint32_t>::max)()},
100 {
"0x100000000",
false, 0, (std::numeric_limits<uint32_t>::max)()},
101 {
nullptr,
false, 0, 0},
114 static const std::array<uint64_test_case, 34> test_cases{{
115 {
"0x3423432448783446",
true, 16, int64_t{0x3423432448783446}},
116 {
"3423432448783446",
true, 16, int64_t{0x3423432448783446}},
121 {
" \t\n 0xffffffffffffffff",
true, 16,
122 (std::numeric_limits<uint64_t>::max)()},
124 {
"012345670123456701234",
true, 8, int64_t{012345670123456701234}},
125 {
"12345670123456701234",
true, 8, int64_t{012345670123456701234}},
127 {
"12845670123456701234",
false, 8, 0},
130 {
"34234324487834466",
true, 0, int64_t{34234324487834466}},
132 {
" \t\n 18446744073709551615",
true, 0,
133 (std::numeric_limits<uint64_t>::max)()},
135 {
"34234324487834466 \n\t ",
true, 0, int64_t{34234324487834466}},
137 {
" \f\v 46",
true, 10, 46},
149 {
"abc",
false, 0, 0},
150 {
"34234324487834466a",
false, 0, 0},
151 {
"34234487834466.3",
false, 0, 0},
153 {
" -123",
false, 0, 0},
154 {
" \t\n -123",
false, 0, 0},
157 {
"18446744073709551616",
false, 10, 0},
158 {
"18446744073709551616",
false, 0, 0},
159 {
"0x10000000000000000",
false, 16,
160 (std::numeric_limits<uint64_t>::max)()},
161 {
"0X10000000000000000",
false, 16,
162 (std::numeric_limits<uint64_t>::max)()},
163 {
"0x10000000000000000",
false, 0, (std::numeric_limits<uint64_t>::max)()},
164 {
"0X10000000000000000",
false, 0,
165 (std::numeric_limits<uint64_t>::max)()},
167 {
"0x1234",
true, 16, 0x1234},
170 {
"1234",
true, 0, 1234},
171 {
nullptr,
false, 0, 0},
179 #endif // ABSL_STRINGS_INTERNAL_NUMBERS_TEST_COMMON_H_
bool Itoa(IntType value, int base, std::string *destination)
const std::array< uint64_test_case, 34 > & strtouint64_test_cases()
const std::array< uint32_test_case, 27 > & strtouint32_test_cases()