| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | // This file contains string processing functions related to |
| 16 | // numeric values. |
| 17 |
| 18 | #include "absl/strings/numbers.h" |
| 19 |
| 20 | #include <algorithm> |
| 21 | #include <cassert> |
| 22 | #include <cfloat> // for DBL_DIG and FLT_DIG |
| 23 | #include <cmath> // for HUGE_VAL |
| 24 | #include <cstdint> |
| 25 | #include <cstdio> |
| 26 | #include <cstdlib> |
| 27 | #include <cstring> |
| 28 | #include <iterator> |
| 29 | #include <limits> |
| 30 | #include <memory> |
| 31 | #include <utility> |
| 32 |
| 33 | #include "absl/base/attributes.h" |
| 34 | #include "absl/base/internal/raw_logging.h" |
| 35 | #include "absl/numeric/bits.h" |
| 36 | #include "absl/strings/ascii.h" |
| 37 | #include "absl/strings/charconv.h" |
| 38 | #include "absl/strings/escaping.h" |
| 39 | #include "absl/strings/internal/memutil.h" |
| 40 | #include "absl/strings/match.h" |
| 41 | #include "absl/strings/str_cat.h" |
| 42 |
| 43 | namespace absl { |
| 44 | ABSL_NAMESPACE_BEGIN |
| 45 |
| 46 | bool SimpleAtof(absl::string_view str, float* out) { |
| 47 | *out = 0.0; |
| 48 | str = StripAsciiWhitespace(str); |
| 49 | // std::from_chars doesn't accept an initial +, but SimpleAtof does, so if one |
| 50 | // is present, skip it, while avoiding accepting "+-0" as valid. |
| 51 | if (!str.empty() && str[0] == '+') { |
| 52 | str.remove_prefix(1); |
| 53 | if (!str.empty() && str[0] == '-') { |
| 54 | return false; |
| 55 | } |
| 56 | } |
| 57 | auto result = absl::from_chars(str.data(), str.data() + str.size(), *out); |
| 58 | if (result.ec == std::errc::invalid_argument) { |
| 59 | return false; |
| 60 | } |
| 61 | if (result.ptr != str.data() + str.size()) { |
| 62 | // not all non-whitespace characters consumed |
| 63 | return false; |
| 64 | } |
| 65 | // from_chars() with DR 3081's current wording will return max() on |
| 66 | // overflow. SimpleAtof returns infinity instead. |
| 67 | if (result.ec == std::errc::result_out_of_range) { |
| 68 | if (*out > 1.0) { |
| 69 | *out = std::numeric_limits<float>::infinity(); |
| 70 | } else if (*out < -1.0) { |
| 71 | *out = -std::numeric_limits<float>::infinity(); |
| 72 | } |
| 73 | } |
| 74 | return true; |
| 75 | } |
| 76 |
| 77 | bool SimpleAtod(absl::string_view str, double* out) { |
| 78 | *out = 0.0; |
| 79 | str = StripAsciiWhitespace(str); |
| 80 | // std::from_chars doesn't accept an initial +, but SimpleAtod does, so if one |
| 81 | // is present, skip it, while avoiding accepting "+-0" as valid. |
| 82 | if (!str.empty() && str[0] == '+') { |
| 83 | str.remove_prefix(1); |
| 84 | if (!str.empty() && str[0] == '-') { |
| 85 | return false; |
| 86 | } |
| 87 | } |
| 88 | auto result = absl::from_chars(str.data(), str.data() + str.size(), *out); |
| 89 | if (result.ec == std::errc::invalid_argument) { |
| 90 | return false; |
| 91 | } |
| 92 | if (result.ptr != str.data() + str.size()) { |
| 93 | // not all non-whitespace characters consumed |
| 94 | return false; |
| 95 | } |
| 96 | // from_chars() with DR 3081's current wording will return max() on |
| 97 | // overflow. SimpleAtod returns infinity instead. |
| 98 | if (result.ec == std::errc::result_out_of_range) { |
| 99 | if (*out > 1.0) { |
| 100 | *out = std::numeric_limits<double>::infinity(); |
| 101 | } else if (*out < -1.0) { |
| 102 | *out = -std::numeric_limits<double>::infinity(); |
| 103 | } |
| 104 | } |
| 105 | return true; |
| 106 | } |
| 107 |
| 108 | bool SimpleAtob(absl::string_view str, bool* out) { |
| 109 | ABSL_RAW_CHECK(out != nullptr, "Output pointer must not be nullptr."); |
| 110 | if (EqualsIgnoreCase(str, "true") || EqualsIgnoreCase(str, "t") || |
| 111 | EqualsIgnoreCase(str, "yes") || EqualsIgnoreCase(str, "y") || |
| 112 | EqualsIgnoreCase(str, "1")) { |
| 113 | *out = true; |
| 114 | return true; |
| 115 | } |
| 116 | if (EqualsIgnoreCase(str, "false") || EqualsIgnoreCase(str, "f") || |
| 117 | EqualsIgnoreCase(str, "no") || EqualsIgnoreCase(str, "n") || |
| 118 | EqualsIgnoreCase(str, "0")) { |
| 119 | *out = false; |
| 120 | return true; |
| 121 | } |
| 122 | return false; |
| 123 | } |
| 124 |
| 125 | // ---------------------------------------------------------------------- |
| 126 | // FastIntToBuffer() overloads |
| 127 | // |
| 128 | // Like the Fast*ToBuffer() functions above, these are intended for speed. |
| 129 | // Unlike the Fast*ToBuffer() functions, however, these functions write |
| 130 | // their output to the beginning of the buffer. The caller is responsible |
| 131 | // for ensuring that the buffer has enough space to hold the output. |
| 132 | // |
| 133 | // Returns a pointer to the end of the string (i.e. the null character |
| 134 | // terminating the string). |
| 135 | // ---------------------------------------------------------------------- |
| 136 |
| 137 | namespace { |
| 138 |
| 139 | // Used to optimize printing a decimal number's final digit. |
| 140 | const char one_ASCII_final_digits[10][2] { |
| 141 | {'0', 0}, {'1', 0}, {'2', 0}, {'3', 0}, {'4', 0}, |
| 142 | {'5', 0}, {'6', 0}, {'7', 0}, {'8', 0}, {'9', 0}, |
| 143 | }; |
| 144 |
| 145 | } // namespace |
| 146 |
| 147 | char* numbers_internal::FastIntToBuffer(uint32_t i, char* buffer) { |
| 148 | uint32_t digits; |
| 149 | // The idea of this implementation is to trim the number of divides to as few |
| 150 | // as possible, and also reducing memory stores and branches, by going in |
| 151 | // steps of two digits at a time rather than one whenever possible. |
| 152 | // The huge-number case is first, in the hopes that the compiler will output |
| 153 | // that case in one branch-free block of code, and only output conditional |
| 154 | // branches into it from below. |
| 155 | if (i >= 1000000000) { // >= 1,000,000,000 |
| 156 | digits = i / 100000000; // 100,000,000 |
| 157 | i -= digits * 100000000; |
| 158 | PutTwoDigits(digits, buffer); |
| 159 | buffer += 2; |
| 160 | lt100_000_000: |
| 161 | digits = i / 1000000; // 1,000,000 |
| 162 | i -= digits * 1000000; |
| 163 | PutTwoDigits(digits, buffer); |
| 164 | buffer += 2; |
| 165 | lt1_000_000: |
| 166 | digits = i / 10000; // 10,000 |
| 167 | i -= digits * 10000; |
| 168 | PutTwoDigits(digits, buffer); |
| 169 | buffer += 2; |
| 170 | lt10_000: |
| 171 | digits = i / 100; |
| 172 | i -= digits * 100; |
| 173 | PutTwoDigits(digits, buffer); |
| 174 | buffer += 2; |
| 175 | lt100: |
| 176 | digits = i; |
| 177 | PutTwoDigits(digits, buffer); |
| 178 | buffer += 2; |
| 179 | *buffer = 0; |
| 180 | return buffer; |
| 181 | } |
| 182 |
| 183 | if (i < 100) { |
| 184 | digits = i; |
| 185 | if (i >= 10) goto lt100; |
| 186 | memcpy(buffer, one_ASCII_final_digits[i], 2); |
| 187 | return buffer + 1; |
| 188 | } |
| 189 | if (i < 10000) { // 10,000 |
| 190 | if (i >= 1000) goto lt10_000; |
| 191 | digits = i / 100; |
| 192 | i -= digits * 100; |
| 193 | *buffer++ = '0' + static_cast<char>(digits); |
| 194 | goto lt100; |
| 195 | } |
| 196 | if (i < 1000000) { // 1,000,000 |
| 197 | if (i >= 100000) goto lt1_000_000; |
| 198 | digits = i / 10000; // 10,000 |
| 199 | i -= digits * 10000; |
| 200 | *buffer++ = '0' + static_cast<char>(digits); |
| 201 | goto lt10_000; |
| 202 | } |
| 203 | if (i < 100000000) { // 100,000,000 |
| 204 | if (i >= 10000000) goto lt100_000_000; |
| 205 | digits = i / 1000000; // 1,000,000 |
| 206 | i -= digits * 1000000; |
| 207 | *buffer++ = '0' + static_cast<char>(digits); |
| 208 | goto lt1_000_000; |
| 209 | } |
| 210 | // we already know that i < 1,000,000,000 |
| 211 | digits = i / 100000000; // 100,000,000 |
| 212 | i -= digits * 100000000; |
| 213 | *buffer++ = '0' + static_cast<char>(digits); |
| 214 | goto lt100_000_000; |
| 215 | } |
| 216 |
| 217 | char* numbers_internal::FastIntToBuffer(int32_t i, char* buffer) { |
| 218 | uint32_t u = static_cast<uint32_t>(i); |
| 219 | if (i < 0) { |
| 220 | *buffer++ = '-'; |
| 221 | // We need to do the negation in modular (i.e., "unsigned") |
| 222 | // arithmetic; MSVC++ apprently warns for plain "-u", so |
| 223 | // we write the equivalent expression "0 - u" instead. |
| 224 | u = 0 - u; |
| 225 | } |
| 226 | return numbers_internal::FastIntToBuffer(u, buffer); |
| 227 | } |
| 228 |
| 229 | char* numbers_internal::FastIntToBuffer(uint64_t i, char* buffer) { |
| 230 | uint32_t u32 = static_cast<uint32_t>(i); |
| 231 | if (u32 == i) return numbers_internal::FastIntToBuffer(u32, buffer); |
| 232 |
| 233 | // Here we know i has at least 10 decimal digits. |
| 234 | uint64_t top_1to11 = i / 1000000000; |
| 235 | u32 = static_cast<uint32_t>(i - top_1to11 * 1000000000); |
| 236 | uint32_t top_1to11_32 = static_cast<uint32_t>(top_1to11); |
| 237 |
| 238 | if (top_1to11_32 == top_1to11) { |
| 239 | buffer = numbers_internal::FastIntToBuffer(top_1to11_32, buffer); |
| 240 | } else { |
| 241 | // top_1to11 has more than 32 bits too; print it in two steps. |
| 242 | uint32_t top_8to9 = static_cast<uint32_t>(top_1to11 / 100); |
| 243 | uint32_t mid_2 = static_cast<uint32_t>(top_1to11 - top_8to9 * 100); |
| 244 | buffer = numbers_internal::FastIntToBuffer(top_8to9, buffer); |
| 245 | PutTwoDigits(mid_2, buffer); |
| 246 | buffer += 2; |
| 247 | } |
| 248 |
| 249 | // We have only 9 digits now, again the maximum uint32_t can handle fully. |
| 250 | uint32_t digits = u32 / 10000000; // 10,000,000 |
| 251 | u32 -= digits * 10000000; |
| 252 | PutTwoDigits(digits, buffer); |
| 253 | buffer += 2; |
| 254 | digits = u32 / 100000; // 100,000 |
| 255 | u32 -= digits * 100000; |
| 256 | PutTwoDigits(digits, buffer); |
| 257 | buffer += 2; |
| 258 | digits = u32 / 1000; // 1,000 |
| 259 | u32 -= digits * 1000; |
| 260 | PutTwoDigits(digits, buffer); |
| 261 | buffer += 2; |
| 262 | digits = u32 / 10; |
| 263 | u32 -= digits * 10; |
| 264 | PutTwoDigits(digits, buffer); |
| 265 | buffer += 2; |
| 266 | memcpy(buffer, one_ASCII_final_digits[u32], 2); |
| 267 | return buffer + 1; |
| 268 | } |
| 269 |
| 270 | char* numbers_internal::FastIntToBuffer(int64_t i, char* buffer) { |
| 271 | uint64_t u = static_cast<uint64_t>(i); |
| 272 | if (i < 0) { |
| 273 | *buffer++ = '-'; |
| 274 | u = 0 - u; |
| 275 | } |
| 276 | return numbers_internal::FastIntToBuffer(u, buffer); |
| 277 | } |
| 278 |
| 279 | // Given a 128-bit number expressed as a pair of uint64_t, high half first, |
| 280 | // return that number multiplied by the given 32-bit value. If the result is |
| 281 | // too large to fit in a 128-bit number, divide it by 2 until it fits. |
| 282 | static std::pair<uint64_t, uint64_t> Mul32(std::pair<uint64_t, uint64_t> num, |
| 283 | uint32_t mul) { |
| 284 | uint64_t bits0_31 = num.second & 0xFFFFFFFF; |
| 285 | uint64_t bits32_63 = num.second >> 32; |
| 286 | uint64_t bits64_95 = num.first & 0xFFFFFFFF; |
| 287 | uint64_t bits96_127 = num.first >> 32; |
| 288 |
| 289 | // The picture so far: each of these 64-bit values has only the lower 32 bits |
| 290 | // filled in. |
| 291 | // bits96_127: [ 00000000 xxxxxxxx ] |
| 292 | // bits64_95: [ 00000000 xxxxxxxx ] |
| 293 | // bits32_63: [ 00000000 xxxxxxxx ] |
| 294 | // bits0_31: [ 00000000 xxxxxxxx ] |
| 295 |
| 296 | bits0_31 *= mul; |
| 297 | bits32_63 *= mul; |
| 298 | bits64_95 *= mul; |
| 299 | bits96_127 *= mul; |
| 300 |
| 301 | // Now the top halves may also have value, though all 64 of their bits will |
| 302 | // never be set at the same time, since they are a result of a 32x32 bit |
| 303 | // multiply. This makes the carry calculation slightly easier. |
| 304 | // bits96_127: [ mmmmmmmm | mmmmmmmm ] |
| 305 | // bits64_95: [ | mmmmmmmm mmmmmmmm | ] |
| 306 | // bits32_63: | [ mmmmmmmm | mmmmmmmm ] |
| 307 | // bits0_31: | [ | mmmmmmmm mmmmmmmm ] |
| 308 | // eventually: [ bits128_up | ...bits64_127.... | ..bits0_63... ] |
| 309 |
| 310 | uint64_t bits0_63 = bits0_31 + (bits32_63 << 32); |
| 311 | uint64_t bits64_127 = bits64_95 + (bits96_127 << 32) + (bits32_63 >> 32) + |
| 312 | (bits0_63 < bits0_31); |
| 313 | uint64_t bits128_up = (bits96_127 >> 32) + (bits64_127 < bits64_95); |
| 314 | if (bits128_up == 0) return {bits64_127, bits0_63}; |
| 315 |
| 316 | auto shift = static_cast<unsigned>(bit_width(bits128_up)); |
| 317 | uint64_t lo = (bits0_63 >> shift) + (bits64_127 << (64 - shift)); |
| 318 | uint64_t hi = (bits64_127 >> shift) + (bits128_up << (64 - shift)); |
| 319 | return {hi, lo}; |
| 320 | } |
| 321 |
| 322 | // Compute num * 5 ^ expfive, and return the first 128 bits of the result, |
| 323 | // where the first bit is always a one. So PowFive(1, 0) starts 0b100000, |
| 324 | // PowFive(1, 1) starts 0b101000, PowFive(1, 2) starts 0b110010, etc. |
| 325 | static std::pair<uint64_t, uint64_t> PowFive(uint64_t num, int expfive) { |
| 326 | std::pair<uint64_t, uint64_t> result = {num, 0}; |
| 327 | while (expfive >= 13) { |
| 328 | // 5^13 is the highest power of five that will fit in a 32-bit integer. |
| 329 | result = Mul32(result, 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5); |
| 330 | expfive -= 13; |
| 331 | } |
| 332 | constexpr uint32_t powers_of_five[13] = { |
| 333 | 1, |
| 334 | 5, |
| 335 | 5 * 5, |
| 336 | 5 * 5 * 5, |
| 337 | 5 * 5 * 5 * 5, |
| 338 | 5 * 5 * 5 * 5 * 5, |
| 339 | 5 * 5 * 5 * 5 * 5 * 5, |
| 340 | 5 * 5 * 5 * 5 * 5 * 5 * 5, |
| 341 | 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, |
| 342 | 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, |
| 343 | 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, |
| 344 | 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, |
| 345 | 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5}; |
| 346 | result = Mul32(result, powers_of_five[expfive & 15]); |
| 347 | int shift = countl_zero(result.first); |
| 348 | if (shift != 0) { |
| 349 | result.first = (result.first << shift) + (result.second >> (64 - shift)); |
| 350 | result.second = (result.second << shift); |
| 351 | } |
| 352 | return result; |
| 353 | } |
| 354 |
| 355 | struct ExpDigits { |
| 356 | int32_t exponent; |
| 357 | char digits[6]; |
| 358 | }; |
| 359 |
| 360 | // SplitToSix converts value, a positive double-precision floating-point number, |
| 361 | // into a base-10 exponent and 6 ASCII digits, where the first digit is never |
| 362 | // zero. For example, SplitToSix(1) returns an exponent of zero and a digits |
| 363 | // array of {'1', '0', '0', '0', '0', '0'}. If value is exactly halfway between |
| 364 | // two possible representations, e.g. value = 100000.5, then "round to even" is |
| 365 | // performed. |
| 366 | static ExpDigits SplitToSix(const double value) { |
| 367 | ExpDigits exp_dig; |
| 368 | int exp = 5; |
| 369 | double d = value; |
| 370 | // First step: calculate a close approximation of the output, where the |
| 371 | // value d will be between 100,000 and 999,999, representing the digits |
| 372 | // in the output ASCII array, and exp is the base-10 exponent. It would be |
| 373 | // faster to use a table here, and to look up the base-2 exponent of value, |
| 374 | // however value is an IEEE-754 64-bit number, so the table would have 2,000 |
| 375 | // entries, which is not cache-friendly. |
| 376 | if (d >= 999999.5) { |
| 377 | if (d >= 1e+261) exp += 256, d *= 1e-256; |
| 378 | if (d >= 1e+133) exp += 128, d *= 1e-128; |
| 379 | if (d >= 1e+69) exp += 64, d *= 1e-64; |
| 380 | if (d >= 1e+37) exp += 32, d *= 1e-32; |
| 381 | if (d >= 1e+21) exp += 16, d *= 1e-16; |
| 382 | if (d >= 1e+13) exp += 8, d *= 1e-8; |
| 383 | if (d >= 1e+9) exp += 4, d *= 1e-4; |
| 384 | if (d >= 1e+7) exp += 2, d *= 1e-2; |
| 385 | if (d >= 1e+6) exp += 1, d *= 1e-1; |
| 386 | } else { |
| 387 | if (d < 1e-250) exp -= 256, d *= 1e256; |
| 388 | if (d < 1e-122) exp -= 128, d *= 1e128; |
| 389 | if (d < 1e-58) exp -= 64, d *= 1e64; |
| 390 | if (d < 1e-26) exp -= 32, d *= 1e32; |
| 391 | if (d < 1e-10) exp -= 16, d *= 1e16; |
| 392 | if (d < 1e-2) exp -= 8, d *= 1e8; |
| 393 | if (d < 1e+2) exp -= 4, d *= 1e4; |
| 394 | if (d < 1e+4) exp -= 2, d *= 1e2; |
| 395 | if (d < 1e+5) exp -= 1, d *= 1e1; |
| 396 | } |
| 397 | // At this point, d is in the range [99999.5..999999.5) and exp is in the |
| 398 | // range [-324..308]. Since we need to round d up, we want to add a half |
| 399 | // and truncate. |
| 400 | // However, the technique above may have lost some precision, due to its |
| 401 | // repeated multiplication by constants that each may be off by half a bit |
| 402 | // of precision. This only matters if we're close to the edge though. |
| 403 | // Since we'd like to know if the fractional part of d is close to a half, |
| 404 | // we multiply it by 65536 and see if the fractional part is close to 32768. |
| 405 | // (The number doesn't have to be a power of two,but powers of two are faster) |
| 406 | uint64_t d64k = d * 65536; |
| 407 | uint32_t dddddd; // A 6-digit decimal integer. |
| 408 | if ((d64k % 65536) == 32767 || (d64k % 65536) == 32768) { |
| 409 | // OK, it's fairly likely that precision was lost above, which is |
| 410 | // not a surprise given only 52 mantissa bits are available. Therefore |
| 411 | // redo the calculation using 128-bit numbers. (64 bits are not enough). |
| 412 |
| 413 | // Start out with digits rounded down; maybe add one below. |
| 414 | dddddd = static_cast<uint32_t>(d64k / 65536); |
| 415 |
| 416 | // mantissa is a 64-bit integer representing M.mmm... * 2^63. The actual |
| 417 | // value we're representing, of course, is M.mmm... * 2^exp2. |
| 418 | int exp2; |
| 419 | double m = std::frexp(value, &exp2); |
| 420 | uint64_t mantissa = m * (32768.0 * 65536.0 * 65536.0 * 65536.0); |
| 421 | // std::frexp returns an m value in the range [0.5, 1.0), however we |
| 422 | // can't multiply it by 2^64 and convert to an integer because some FPUs |
| 423 | // throw an exception when converting an number higher than 2^63 into an |
| 424 | // integer - even an unsigned 64-bit integer! Fortunately it doesn't matter |
| 425 | // since m only has 52 significant bits anyway. |
| 426 | mantissa <<= 1; |
| 427 | exp2 -= 64; // not needed, but nice for debugging |
| 428 |
| 429 | // OK, we are here to compare: |
| 430 | // (dddddd + 0.5) * 10^(exp-5) vs. mantissa * 2^exp2 |
| 431 | // so we can round up dddddd if appropriate. Those values span the full |
| 432 | // range of 600 orders of magnitude of IEE 64-bit floating-point. |
| 433 | // Fortunately, we already know they are very close, so we don't need to |
| 434 | // track the base-2 exponent of both sides. This greatly simplifies the |
| 435 | // the math since the 2^exp2 calculation is unnecessary and the power-of-10 |
| 436 | // calculation can become a power-of-5 instead. |
| 437 |
| 438 | std::pair<uint64_t, uint64_t> edge, val; |
| 439 | if (exp >= 6) { |
| 440 | // Compare (dddddd + 0.5) * 5 ^ (exp - 5) to mantissa |
| 441 | // Since we're tossing powers of two, 2 * dddddd + 1 is the |
| 442 | // same as dddddd + 0.5 |
| 443 | edge = PowFive(2 * dddddd + 1, exp - 5); |
| 444 |
| 445 | val.first = mantissa; |
| 446 | val.second = 0; |
| 447 | } else { |
| 448 | // We can't compare (dddddd + 0.5) * 5 ^ (exp - 5) to mantissa as we did |
| 449 | // above because (exp - 5) is negative. So we compare (dddddd + 0.5) to |
| 450 | // mantissa * 5 ^ (5 - exp) |
| 451 | edge = PowFive(2 * dddddd + 1, 0); |
| 452 |
| 453 | val = PowFive(mantissa, 5 - exp); |
| 454 | } |
| 455 | // printf("exp=%d %016lx %016lx vs %016lx %016lx\n", exp, val.first, |
| 456 | // val.second, edge.first, edge.second); |
| 457 | if (val > edge) { |
| 458 | dddddd++; |
| 459 | } else if (val == edge) { |
| 460 | dddddd += (dddddd & 1); |
| 461 | } |
| 462 | } else { |
| 463 | // Here, we are not close to the edge. |
| 464 | dddddd = static_cast<uint32_t>((d64k + 32768) / 65536); |
| 465 | } |
| 466 | if (dddddd == 1000000) { |
| 467 | dddddd = 100000; |
| 468 | exp += 1; |
| 469 | } |
| 470 | exp_dig.exponent = exp; |
| 471 |
| 472 | uint32_t two_digits = dddddd / 10000; |
| 473 | dddddd -= two_digits * 10000; |
| 474 | numbers_internal::PutTwoDigits(two_digits, &exp_dig.digits[0]); |
| 475 |
| 476 | two_digits = dddddd / 100; |
| 477 | dddddd -= two_digits * 100; |
| 478 | numbers_internal::PutTwoDigits(two_digits, &exp_dig.digits[2]); |
| 479 |
| 480 | numbers_internal::PutTwoDigits(dddddd, &exp_dig.digits[4]); |
| 481 | return exp_dig; |
| 482 | } |
| 483 |
| 484 | // Helper function for fast formatting of floating-point. |
| 485 | // The result is the same as "%g", a.k.a. "%.6g". |
| 486 | size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) { |
| 487 | static_assert(std::numeric_limits<float>::is_iec559, |
| 488 | "IEEE-754/IEC-559 support only"); |
| 489 |
| 490 | char* out = buffer; // we write data to out, incrementing as we go, but |
| 491 | // FloatToBuffer always returns the address of the buffer |
| 492 | // passed in. |
| 493 |
| 494 | if (std::isnan(d)) { |
| 495 | strcpy(out, "nan"); // NOLINT(runtime/printf) |
| 496 | return 3; |
| 497 | } |
| 498 | if (d == 0) { // +0 and -0 are handled here |
| 499 | if (std::signbit(d)) *out++ = '-'; |
| 500 | *out++ = '0'; |
| 501 | *out = 0; |
| 502 | return static_cast<size_t>(out - buffer); |
| 503 | } |
| 504 | if (d < 0) { |
| 505 | *out++ = '-'; |
| 506 | d = -d; |
| 507 | } |
| 508 | if (d > std::numeric_limits<double>::max()) { |
| 509 | strcpy(out, "inf"); // NOLINT(runtime/printf) |
| 510 | return static_cast<size_t>(out + 3 - buffer); |
| 511 | } |
| 512 |
| 513 | auto exp_dig = SplitToSix(d); |
| 514 | int exp = exp_dig.exponent; |
| 515 | const char* digits = exp_dig.digits; |
| 516 | out[0] = '0'; |
| 517 | out[1] = '.'; |
| 518 | switch (exp) { |
| 519 | case 5: |
| 520 | memcpy(out, &digits[0], 6), out += 6; |
| 521 | *out = 0; |
| 522 | return static_cast<size_t>(out - buffer); |
| 523 | case 4: |
| 524 | memcpy(out, &digits[0], 5), out += 5; |
| 525 | if (digits[5] != '0') { |
| 526 | *out++ = '.'; |
| 527 | *out++ = digits[5]; |
| 528 | } |
| 529 | *out = 0; |
| 530 | return static_cast<size_t>(out - buffer); |
| 531 | case 3: |
| 532 | memcpy(out, &digits[0], 4), out += 4; |
| 533 | if ((digits[5] | digits[4]) != '0') { |
| 534 | *out++ = '.'; |
| 535 | *out++ = digits[4]; |
| 536 | if (digits[5] != '0') *out++ = digits[5]; |
| 537 | } |
| 538 | *out = 0; |
| 539 | return static_cast<size_t>(out - buffer); |
| 540 | case 2: |
| 541 | memcpy(out, &digits[0], 3), out += 3; |
| 542 | *out++ = '.'; |
| 543 | memcpy(out, &digits[3], 3); |
| 544 | out += 3; |
| 545 | while (out[-1] == '0') --out; |
| 546 | if (out[-1] == '.') --out; |
| 547 | *out = 0; |
| 548 | return static_cast<size_t>(out - buffer); |
| 549 | case 1: |
| 550 | memcpy(out, &digits[0], 2), out += 2; |
| 551 | *out++ = '.'; |
| 552 | memcpy(out, &digits[2], 4); |
| 553 | out += 4; |
| 554 | while (out[-1] == '0') --out; |
| 555 | if (out[-1] == '.') --out; |
| 556 | *out = 0; |
| 557 | return static_cast<size_t>(out - buffer); |
| 558 | case 0: |
| 559 | memcpy(out, &digits[0], 1), out += 1; |
| 560 | *out++ = '.'; |
| 561 | memcpy(out, &digits[1], 5); |
| 562 | out += 5; |
| 563 | while (out[-1] == '0') --out; |
| 564 | if (out[-1] == '.') --out; |
| 565 | *out = 0; |
| 566 | return static_cast<size_t>(out - buffer); |
| 567 | case -4: |
| 568 | out[2] = '0'; |
| 569 | ++out; |
| 570 | ABSL_FALLTHROUGH_INTENDED; |
| 571 | case -3: |
| 572 | out[2] = '0'; |
| 573 | ++out; |
| 574 | ABSL_FALLTHROUGH_INTENDED; |
| 575 | case -2: |
| 576 | out[2] = '0'; |
| 577 | ++out; |
| 578 | ABSL_FALLTHROUGH_INTENDED; |
| 579 | case -1: |
| 580 | out += 2; |
| 581 | memcpy(out, &digits[0], 6); |
| 582 | out += 6; |
| 583 | while (out[-1] == '0') --out; |
| 584 | *out = 0; |
| 585 | return static_cast<size_t>(out - buffer); |
| 586 | } |
| 587 | assert(exp < -4 || exp >= 6); |
| 588 | out[0] = digits[0]; |
| 589 | assert(out[1] == '.'); |
| 590 | out += 2; |
| 591 | memcpy(out, &digits[1], 5), out += 5; |
| 592 | while (out[-1] == '0') --out; |
| 593 | if (out[-1] == '.') --out; |
| 594 | *out++ = 'e'; |
| 595 | if (exp > 0) { |
| 596 | *out++ = '+'; |
| 597 | } else { |
| 598 | *out++ = '-'; |
| 599 | exp = -exp; |
| 600 | } |
| 601 | if (exp > 99) { |
| 602 | int dig1 = exp / 100; |
| 603 | exp -= dig1 * 100; |
| 604 | *out++ = '0' + static_cast<char>(dig1); |
| 605 | } |
| 606 | PutTwoDigits(static_cast<uint32_t>(exp), out); |
| 607 | out += 2; |
| 608 | *out = 0; |
| 609 | return static_cast<size_t>(out - buffer); |
| 610 | } |
| 611 |
| 612 | namespace { |
| 613 | // Represents integer values of digits. |
| 614 | // Uses 36 to indicate an invalid character since we support |
| 615 | // bases up to 36. |
| 616 | static const int8_t kAsciiToInt[256] = { |
| 617 | 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, // 16 36s. |
| 618 | 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, |
| 619 | 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 0, 1, 2, 3, 4, 5, |
| 620 | 6, 7, 8, 9, 36, 36, 36, 36, 36, 36, 36, 10, 11, 12, 13, 14, 15, 16, 17, |
| 621 | 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, |
| 622 | 36, 36, 36, 36, 36, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, |
| 623 | 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 36, 36, 36, 36, 36, 36, |
| 624 | 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, |
| 625 | 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, |
| 626 | 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, |
| 627 | 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, |
| 628 | 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, |
| 629 | 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, |
| 630 | 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36}; |
| 631 |
| 632 | // Parse the sign and optional hex or oct prefix in text. |
| 633 | inline bool safe_parse_sign_and_base(absl::string_view* text /*inout*/, |
| 634 | int* base_ptr /*inout*/, |
| 635 | bool* negative_ptr /*output*/) { |
| 636 | if (text->data() == nullptr) { |
| 637 | return false; |
| 638 | } |
| 639 |
| 640 | const char* start = text->data(); |
| 641 | const char* end = start + text->size(); |
| 642 | int base = *base_ptr; |
| 643 |
| 644 | // Consume whitespace. |
| 645 | while (start < end && |
| 646 | absl::ascii_isspace(static_cast<unsigned char>(start[0]))) { |
| 647 | ++start; |
| 648 | } |
| 649 | while (start < end && |
| 650 | absl::ascii_isspace(static_cast<unsigned char>(end[-1]))) { |
| 651 | --end; |
| 652 | } |
| 653 | if (start >= end) { |
| 654 | return false; |
| 655 | } |
| 656 |
| 657 | // Consume sign. |
| 658 | *negative_ptr = (start[0] == '-'); |
| 659 | if (*negative_ptr || start[0] == '+') { |
| 660 | ++start; |
| 661 | if (start >= end) { |
| 662 | return false; |
| 663 | } |
| 664 | } |
| 665 |
| 666 | // Consume base-dependent prefix. |
| 667 | // base 0: "0x" -> base 16, "0" -> base 8, default -> base 10 |
| 668 | // base 16: "0x" -> base 16 |
| 669 | // Also validate the base. |
| 670 | if (base == 0) { |
| 671 | if (end - start >= 2 && start[0] == '0' && |
| 672 | (start[1] == 'x' || start[1] == 'X')) { |
| 673 | base = 16; |
| 674 | start += 2; |
| 675 | if (start >= end) { |
| 676 | // "0x" with no digits after is invalid. |
| 677 | return false; |
| 678 | } |
| 679 | } else if (end - start >= 1 && start[0] == '0') { |
| 680 | base = 8; |
| 681 | start += 1; |
| 682 | } else { |
| 683 | base = 10; |
| 684 | } |
| 685 | } else if (base == 16) { |
| 686 | if (end - start >= 2 && start[0] == '0' && |
| 687 | (start[1] == 'x' || start[1] == 'X')) { |
| 688 | start += 2; |
| 689 | if (start >= end) { |
| 690 | // "0x" with no digits after is invalid. |
| 691 | return false; |
| 692 | } |
| 693 | } |
| 694 | } else if (base >= 2 && base <= 36) { |
| 695 | // okay |
| 696 | } else { |
| 697 | return false; |
| 698 | } |
| 699 | *text = absl::string_view(start, static_cast<size_t>(end - start)); |
| 700 | *base_ptr = base; |
| 701 | return true; |
| 702 | } |
| 703 |
| 704 | // Consume digits. |
| 705 | // |
| 706 | // The classic loop: |
| 707 | // |
| 708 | // for each digit |
| 709 | // value = value * base + digit |
| 710 | // value *= sign |
| 711 | // |
| 712 | // The classic loop needs overflow checking. It also fails on the most |
| 713 | // negative integer, -2147483648 in 32-bit two's complement representation. |
| 714 | // |
| 715 | // My improved loop: |
| 716 | // |
| 717 | // if (!negative) |
| 718 | // for each digit |
| 719 | // value = value * base |
| 720 | // value = value + digit |
| 721 | // else |
| 722 | // for each digit |
| 723 | // value = value * base |
| 724 | // value = value - digit |
| 725 | // |
| 726 | // Overflow checking becomes simple. |
| 727 |
| 728 | // Lookup tables per IntType: |
| 729 | // vmax/base and vmin/base are precomputed because division costs at least 8ns. |
| 730 | // TODO(junyer): Doing this per base instead (i.e. an array of structs, not a |
| 731 | // struct of arrays) would probably be better in terms of d-cache for the most |
| 732 | // commonly used bases. |
| 733 | template <typename IntType> |
| 734 | struct LookupTables { |
| 735 | ABSL_CONST_INIT static const IntType kVmaxOverBase[]; |
| 736 | ABSL_CONST_INIT static const IntType kVminOverBase[]; |
| 737 | }; |
| 738 |
| 739 | // An array initializer macro for X/base where base in [0, 36]. |
| 740 | // However, note that lookups for base in [0, 1] should never happen because |
| 741 | // base has been validated to be in [2, 36] by safe_parse_sign_and_base(). |
| 742 | #define X_OVER_BASE_INITIALIZER(X) \ |
| 743 | { \ |
| 744 | 0, 0, X / 2, X / 3, X / 4, X / 5, X / 6, X / 7, X / 8, X / 9, X / 10, \ |
| 745 | X / 11, X / 12, X / 13, X / 14, X / 15, X / 16, X / 17, X / 18, \ |
| 746 | X / 19, X / 20, X / 21, X / 22, X / 23, X / 24, X / 25, X / 26, \ |
| 747 | X / 27, X / 28, X / 29, X / 30, X / 31, X / 32, X / 33, X / 34, \ |
| 748 | X / 35, X / 36, \ |
| 749 | } |
| 750 |
| 751 | // This kVmaxOverBase is generated with |
| 752 | // for (int base = 2; base < 37; ++base) { |
| 753 | // absl::uint128 max = std::numeric_limits<absl::uint128>::max(); |
| 754 | // auto result = max / base; |
| 755 | // std::cout << " MakeUint128(" << absl::Uint128High64(result) << "u, " |
| 756 | // << absl::Uint128Low64(result) << "u),\n"; |
| 757 | // } |
| 758 | // See https://godbolt.org/z/aneYsb |
| 759 | // |
| 760 | // uint128& operator/=(uint128) is not constexpr, so hardcode the resulting |
| 761 | // array to avoid a static initializer. |
| 762 | template <> |
| 763 | ABSL_CONST_INIT const uint128 LookupTables<uint128>::kVmaxOverBase[] = { |
| 764 | 0, |
| 765 | 0, |
| 766 | MakeUint128(9223372036854775807u, 18446744073709551615u), |
| 767 | MakeUint128(6148914691236517205u, 6148914691236517205u), |
| 768 | MakeUint128(4611686018427387903u, 18446744073709551615u), |
| 769 | MakeUint128(3689348814741910323u, 3689348814741910323u), |
| 770 | MakeUint128(3074457345618258602u, 12297829382473034410u), |
| 771 | MakeUint128(2635249153387078802u, 5270498306774157604u), |
| 772 | MakeUint128(2305843009213693951u, 18446744073709551615u), |
| 773 | MakeUint128(2049638230412172401u, 14347467612885206812u), |
| 774 | MakeUint128(1844674407370955161u, 11068046444225730969u), |
| 775 | MakeUint128(1676976733973595601u, 8384883669867978007u), |
| 776 | MakeUint128(1537228672809129301u, 6148914691236517205u), |
| 777 | MakeUint128(1418980313362273201u, 4256940940086819603u), |
| 778 | MakeUint128(1317624576693539401u, 2635249153387078802u), |
| 779 | MakeUint128(1229782938247303441u, 1229782938247303441u), |
| 780 | MakeUint128(1152921504606846975u, 18446744073709551615u), |
| 781 | MakeUint128(1085102592571150095u, 1085102592571150095u), |
| 782 | MakeUint128(1024819115206086200u, 16397105843297379214u), |
| 783 | MakeUint128(970881267037344821u, 16504981539634861972u), |
| 784 | MakeUint128(922337203685477580u, 14757395258967641292u), |
| 785 | MakeUint128(878416384462359600u, 14054662151397753612u), |
| 786 | MakeUint128(838488366986797800u, 13415813871788764811u), |
| 787 | MakeUint128(802032351030850070u, 4812194106185100421u), |
| 788 | MakeUint128(768614336404564650u, 12297829382473034410u), |
| 789 | MakeUint128(737869762948382064u, 11805916207174113034u), |
| 790 | MakeUint128(709490156681136600u, 11351842506898185609u), |
| 791 | MakeUint128(683212743470724133u, 17080318586768103348u), |
| 792 | MakeUint128(658812288346769700u, 10540996613548315209u), |
| 793 | MakeUint128(636094623231363848u, 15266270957552732371u), |
| 794 | MakeUint128(614891469123651720u, 9838263505978427528u), |
| 795 | MakeUint128(595056260442243600u, 9520900167075897608u), |
| 796 | MakeUint128(576460752303423487u, 18446744073709551615u), |
| 797 | MakeUint128(558992244657865200u, 8943875914525843207u), |
| 798 | MakeUint128(542551296285575047u, 9765923333140350855u), |
| 799 | MakeUint128(527049830677415760u, 8432797290838652167u), |
| 800 | MakeUint128(512409557603043100u, 8198552921648689607u), |
| 801 | }; |
| 802 |
| 803 | // This kVmaxOverBase generated with |
| 804 | // for (int base = 2; base < 37; ++base) { |
| 805 | // absl::int128 max = std::numeric_limits<absl::int128>::max(); |
| 806 | // auto result = max / base; |
| 807 | // std::cout << "\tMakeInt128(" << absl::Int128High64(result) << ", " |
| 808 | // << absl::Int128Low64(result) << "u),\n"; |
| 809 | // } |
| 810 | // See https://godbolt.org/z/7djYWz |
| 811 | // |
| 812 | // int128& operator/=(int128) is not constexpr, so hardcode the resulting array |
| 813 | // to avoid a static initializer. |
| 814 | template <> |
| 815 | ABSL_CONST_INIT const int128 LookupTables<int128>::kVmaxOverBase[] = { |
| 816 | 0, |
| 817 | 0, |
| 818 | MakeInt128(4611686018427387903, 18446744073709551615u), |
| 819 | MakeInt128(3074457345618258602, 12297829382473034410u), |
| 820 | MakeInt128(2305843009213693951, 18446744073709551615u), |
| 821 | MakeInt128(1844674407370955161, 11068046444225730969u), |
| 822 | MakeInt128(1537228672809129301, 6148914691236517205u), |
| 823 | MakeInt128(1317624576693539401, 2635249153387078802u), |
| 824 | MakeInt128(1152921504606846975, 18446744073709551615u), |
| 825 | MakeInt128(1024819115206086200, 16397105843297379214u), |
| 826 | MakeInt128(922337203685477580, 14757395258967641292u), |
| 827 | MakeInt128(838488366986797800, 13415813871788764811u), |
| 828 | MakeInt128(768614336404564650, 12297829382473034410u), |
| 829 | MakeInt128(709490156681136600, 11351842506898185609u), |
| 830 | MakeInt128(658812288346769700, 10540996613548315209u), |
| 831 | MakeInt128(614891469123651720, 9838263505978427528u), |
| 832 | MakeInt128(576460752303423487, 18446744073709551615u), |
| 833 | MakeInt128(542551296285575047, 9765923333140350855u), |
| 834 | MakeInt128(512409557603043100, 8198552921648689607u), |
| 835 | MakeInt128(485440633518672410, 17475862806672206794u), |
| 836 | MakeInt128(461168601842738790, 7378697629483820646u), |
| 837 | MakeInt128(439208192231179800, 7027331075698876806u), |
| 838 | MakeInt128(419244183493398900, 6707906935894382405u), |
| 839 | MakeInt128(401016175515425035, 2406097053092550210u), |
| 840 | MakeInt128(384307168202282325, 6148914691236517205u), |
| 841 | MakeInt128(368934881474191032, 5902958103587056517u), |
| 842 | MakeInt128(354745078340568300, 5675921253449092804u), |
| 843 | MakeInt128(341606371735362066, 17763531330238827482u), |
| 844 | MakeInt128(329406144173384850, 5270498306774157604u), |
| 845 | MakeInt128(318047311615681924, 7633135478776366185u), |
| 846 | MakeInt128(307445734561825860, 4919131752989213764u), |
| 847 | MakeInt128(297528130221121800, 4760450083537948804u), |
| 848 | MakeInt128(288230376151711743, 18446744073709551615u), |
| 849 | MakeInt128(279496122328932600, 4471937957262921603u), |
| 850 | MakeInt128(271275648142787523, 14106333703424951235u), |
| 851 | MakeInt128(263524915338707880, 4216398645419326083u), |
| 852 | MakeInt128(256204778801521550, 4099276460824344803u), |
| 853 | }; |
| 854 |
| 855 | // This kVminOverBase generated with |
| 856 | // for (int base = 2; base < 37; ++base) { |
| 857 | // absl::int128 min = std::numeric_limits<absl::int128>::min(); |
| 858 | // auto result = min / base; |
| 859 | // std::cout << "\tMakeInt128(" << absl::Int128High64(result) << ", " |
| 860 | // << absl::Int128Low64(result) << "u),\n"; |
| 861 | // } |
| 862 | // |
| 863 | // See https://godbolt.org/z/7djYWz |
| 864 | // |
| 865 | // int128& operator/=(int128) is not constexpr, so hardcode the resulting array |
| 866 | // to avoid a static initializer. |
| 867 | template <> |
| 868 | ABSL_CONST_INIT const int128 LookupTables<int128>::kVminOverBase[] = { |
| 869 | 0, |
| 870 | 0, |
| 871 | MakeInt128(-4611686018427387904, 0u), |
| 872 | MakeInt128(-3074457345618258603, 6148914691236517206u), |
| 873 | MakeInt128(-2305843009213693952, 0u), |
| 874 | MakeInt128(-1844674407370955162, 7378697629483820647u), |
| 875 | MakeInt128(-1537228672809129302, 12297829382473034411u), |
| 876 | MakeInt128(-1317624576693539402, 15811494920322472814u), |
| 877 | MakeInt128(-1152921504606846976, 0u), |
| 878 | MakeInt128(-1024819115206086201, 2049638230412172402u), |
| 879 | MakeInt128(-922337203685477581, 3689348814741910324u), |
| 880 | MakeInt128(-838488366986797801, 5030930201920786805u), |
| 881 | MakeInt128(-768614336404564651, 6148914691236517206u), |
| 882 | MakeInt128(-709490156681136601, 7094901566811366007u), |
| 883 | MakeInt128(-658812288346769701, 7905747460161236407u), |
| 884 | MakeInt128(-614891469123651721, 8608480567731124088u), |
| 885 | MakeInt128(-576460752303423488, 0u), |
| 886 | MakeInt128(-542551296285575048, 8680820740569200761u), |
| 887 | MakeInt128(-512409557603043101, 10248191152060862009u), |
| 888 | MakeInt128(-485440633518672411, 970881267037344822u), |
| 889 | MakeInt128(-461168601842738791, 11068046444225730970u), |
| 890 | MakeInt128(-439208192231179801, 11419412998010674810u), |
| 891 | MakeInt128(-419244183493398901, 11738837137815169211u), |
| 892 | MakeInt128(-401016175515425036, 16040647020617001406u), |
| 893 | MakeInt128(-384307168202282326, 12297829382473034411u), |
| 894 | MakeInt128(-368934881474191033, 12543785970122495099u), |
| 895 | MakeInt128(-354745078340568301, 12770822820260458812u), |
| 896 | MakeInt128(-341606371735362067, 683212743470724134u), |
| 897 | MakeInt128(-329406144173384851, 13176245766935394012u), |
| 898 | MakeInt128(-318047311615681925, 10813608594933185431u), |
| 899 | MakeInt128(-307445734561825861, 13527612320720337852u), |
| 900 | MakeInt128(-297528130221121801, 13686293990171602812u), |
| 901 | MakeInt128(-288230376151711744, 0u), |
| 902 | MakeInt128(-279496122328932601, 13974806116446630013u), |
| 903 | MakeInt128(-271275648142787524, 4340410370284600381u), |
| 904 | MakeInt128(-263524915338707881, 14230345428290225533u), |
| 905 | MakeInt128(-256204778801521551, 14347467612885206813u), |
| 906 | }; |
| 907 |
| 908 | template <typename IntType> |
| 909 | ABSL_CONST_INIT const IntType LookupTables<IntType>::kVmaxOverBase[] = |
| 910 | X_OVER_BASE_INITIALIZER(std::numeric_limits<IntType>::max()); |
| 911 |
| 912 | template <typename IntType> |
| 913 | ABSL_CONST_INIT const IntType LookupTables<IntType>::kVminOverBase[] = |
| 914 | X_OVER_BASE_INITIALIZER(std::numeric_limits<IntType>::min()); |
| 915 |
| 916 | #undef X_OVER_BASE_INITIALIZER |
| 917 |
| 918 | template <typename IntType> |
| 919 | inline bool safe_parse_positive_int(absl::string_view text, int base, |
| 920 | IntType* value_p) { |
| 921 | IntType value = 0; |
| 922 | const IntType vmax = std::numeric_limits<IntType>::max(); |
| 923 | assert(vmax > 0); |
| 924 | assert(base >= 0); |
| 925 | const IntType base_inttype = static_cast<IntType>(base); |
| 926 | assert(vmax >= base_inttype); |
| 927 | const IntType vmax_over_base = LookupTables<IntType>::kVmaxOverBase[base]; |
| 928 | assert(base < 2 || |
| 929 | std::numeric_limits<IntType>::max() / base_inttype == vmax_over_base); |
| 930 | const char* start = text.data(); |
| 931 | const char* end = start + text.size(); |
| 932 | // loop over digits |
| 933 | for (; start < end; ++start) { |
| 934 | unsigned char c = static_cast<unsigned char>(start[0]); |
| 935 | IntType digit = static_cast<IntType>(kAsciiToInt[c]); |
| 936 | if (digit >= base_inttype) { |
| 937 | *value_p = value; |
| 938 | return false; |
| 939 | } |
| 940 | if (value > vmax_over_base) { |
| 941 | *value_p = vmax; |
| 942 | return false; |
| 943 | } |
| 944 | value *= base_inttype; |
| 945 | if (value > vmax - digit) { |
| 946 | *value_p = vmax; |
| 947 | return false; |
| 948 | } |
| 949 | value += digit; |
| 950 | } |
| 951 | *value_p = value; |
| 952 | return true; |
| 953 | } |
| 954 |
| 955 | template <typename IntType> |
| 956 | inline bool safe_parse_negative_int(absl::string_view text, int base, |
| 957 | IntType* value_p) { |
| 958 | IntType value = 0; |
| 959 | const IntType vmin = std::numeric_limits<IntType>::min(); |
| 960 | assert(vmin < 0); |
| 961 | assert(vmin <= 0 - base); |
| 962 | IntType vmin_over_base = LookupTables<IntType>::kVminOverBase[base]; |
| 963 | assert(base < 2 || |
| 964 | std::numeric_limits<IntType>::min() / base == vmin_over_base); |
| 965 | // 2003 c++ standard [expr.mul] |
| 966 | // "... the sign of the remainder is implementation-defined." |
| 967 | // Although (vmin/base)*base + vmin%base is always vmin. |
| 968 | // 2011 c++ standard tightens the spec but we cannot rely on it. |
| 969 | // TODO(junyer): Handle this in the lookup table generation. |
| 970 | if (vmin % base > 0) { |
| 971 | vmin_over_base += 1; |
| 972 | } |
| 973 | const char* start = text.data(); |
| 974 | const char* end = start + text.size(); |
| 975 | // loop over digits |
| 976 | for (; start < end; ++start) { |
| 977 | unsigned char c = static_cast<unsigned char>(start[0]); |
| 978 | int digit = kAsciiToInt[c]; |
| 979 | if (digit >= base) { |
| 980 | *value_p = value; |
| 981 | return false; |
| 982 | } |
| 983 | if (value < vmin_over_base) { |
| 984 | *value_p = vmin; |
| 985 | return false; |
| 986 | } |
| 987 | value *= base; |
| 988 | if (value < vmin + digit) { |
| 989 | *value_p = vmin; |
| 990 | return false; |
| 991 | } |
| 992 | value -= digit; |
| 993 | } |
| 994 | *value_p = value; |
| 995 | return true; |
| 996 | } |
| 997 |
| 998 | // Input format based on POSIX.1-2008 strtol |
| 999 | // http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtol.html |
| 1000 | template <typename IntType> |
| 1001 | inline bool safe_int_internal(absl::string_view text, IntType* value_p, |
| 1002 | int base) { |
| 1003 | *value_p = 0; |
| 1004 | bool negative; |
| 1005 | if (!safe_parse_sign_and_base(&text, &base, &negative)) { |
| 1006 | return false; |
| 1007 | } |
| 1008 | if (!negative) { |
| 1009 | return safe_parse_positive_int(text, base, value_p); |
| 1010 | } else { |
| 1011 | return safe_parse_negative_int(text, base, value_p); |
| 1012 | } |
| 1013 | } |
| 1014 |
| 1015 | template <typename IntType> |
| 1016 | inline bool safe_uint_internal(absl::string_view text, IntType* value_p, |
| 1017 | int base) { |
| 1018 | *value_p = 0; |
| 1019 | bool negative; |
| 1020 | if (!safe_parse_sign_and_base(&text, &base, &negative) || negative) { |
| 1021 | return false; |
| 1022 | } |
| 1023 | return safe_parse_positive_int(text, base, value_p); |
| 1024 | } |
| 1025 | } // anonymous namespace |
| 1026 |
| 1027 | namespace numbers_internal { |
| 1028 |
| 1029 | // Digit conversion. |
| 1030 | ABSL_CONST_INIT ABSL_DLL const char kHexChar[] = |
| 1031 | "0123456789abcdef"; |
| 1032 |
| 1033 | ABSL_CONST_INIT ABSL_DLL const char kHexTable[513] = |
| 1034 | "000102030405060708090a0b0c0d0e0f" |
| 1035 | "101112131415161718191a1b1c1d1e1f" |
| 1036 | "202122232425262728292a2b2c2d2e2f" |
| 1037 | "303132333435363738393a3b3c3d3e3f" |
| 1038 | "404142434445464748494a4b4c4d4e4f" |
| 1039 | "505152535455565758595a5b5c5d5e5f" |
| 1040 | "606162636465666768696a6b6c6d6e6f" |
| 1041 | "707172737475767778797a7b7c7d7e7f" |
| 1042 | "808182838485868788898a8b8c8d8e8f" |
| 1043 | "909192939495969798999a9b9c9d9e9f" |
| 1044 | "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf" |
| 1045 | "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf" |
| 1046 | "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf" |
| 1047 | "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf" |
| 1048 | "e0e1e2e3e4e5e6e7e8e9eaebecedeeef" |
| 1049 | "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"; |
| 1050 |
| 1051 | ABSL_CONST_INIT ABSL_DLL const char two_ASCII_digits[100][2] = { |
| 1052 | {'0', '0'}, {'0', '1'}, {'0', '2'}, {'0', '3'}, {'0', '4'}, {'0', '5'}, |
| 1053 | {'0', '6'}, {'0', '7'}, {'0', '8'}, {'0', '9'}, {'1', '0'}, {'1', '1'}, |
| 1054 | {'1', '2'}, {'1', '3'}, {'1', '4'}, {'1', '5'}, {'1', '6'}, {'1', '7'}, |
| 1055 | {'1', '8'}, {'1', '9'}, {'2', '0'}, {'2', '1'}, {'2', '2'}, {'2', '3'}, |
| 1056 | {'2', '4'}, {'2', '5'}, {'2', '6'}, {'2', '7'}, {'2', '8'}, {'2', '9'}, |
| 1057 | {'3', '0'}, {'3', '1'}, {'3', '2'}, {'3', '3'}, {'3', '4'}, {'3', '5'}, |
| 1058 | {'3', '6'}, {'3', '7'}, {'3', '8'}, {'3', '9'}, {'4', '0'}, {'4', '1'}, |
| 1059 | {'4', '2'}, {'4', '3'}, {'4', '4'}, {'4', '5'}, {'4', '6'}, {'4', '7'}, |
| 1060 | {'4', '8'}, {'4', '9'}, {'5', '0'}, {'5', '1'}, {'5', '2'}, {'5', '3'}, |
| 1061 | {'5', '4'}, {'5', '5'}, {'5', '6'}, {'5', '7'}, {'5', '8'}, {'5', '9'}, |
| 1062 | {'6', '0'}, {'6', '1'}, {'6', '2'}, {'6', '3'}, {'6', '4'}, {'6', '5'}, |
| 1063 | {'6', '6'}, {'6', '7'}, {'6', '8'}, {'6', '9'}, {'7', '0'}, {'7', '1'}, |
| 1064 | {'7', '2'}, {'7', '3'}, {'7', '4'}, {'7', '5'}, {'7', '6'}, {'7', '7'}, |
| 1065 | {'7', '8'}, {'7', '9'}, {'8', '0'}, {'8', '1'}, {'8', '2'}, {'8', '3'}, |
| 1066 | {'8', '4'}, {'8', '5'}, {'8', '6'}, {'8', '7'}, {'8', '8'}, {'8', '9'}, |
| 1067 | {'9', '0'}, {'9', '1'}, {'9', '2'}, {'9', '3'}, {'9', '4'}, {'9', '5'}, |
| 1068 | {'9', '6'}, {'9', '7'}, {'9', '8'}, {'9', '9'}}; |
| 1069 |
| 1070 | bool safe_strto32_base(absl::string_view text, int32_t* value, int base) { |
| 1071 | return safe_int_internal<int32_t>(text, value, base); |
| 1072 | } |
| 1073 |
| 1074 | bool safe_strto64_base(absl::string_view text, int64_t* value, int base) { |
| 1075 | return safe_int_internal<int64_t>(text, value, base); |
| 1076 | } |
| 1077 |
| 1078 | bool safe_strto128_base(absl::string_view text, int128* value, int base) { |
| 1079 | return safe_int_internal<absl::int128>(text, value, base); |
| 1080 | } |
| 1081 |
| 1082 | bool safe_strtou32_base(absl::string_view text, uint32_t* value, int base) { |
| 1083 | return safe_uint_internal<uint32_t>(text, value, base); |
| 1084 | } |
| 1085 |
| 1086 | bool safe_strtou64_base(absl::string_view text, uint64_t* value, int base) { |
| 1087 | return safe_uint_internal<uint64_t>(text, value, base); |
| 1088 | } |
| 1089 |
| 1090 | bool safe_strtou128_base(absl::string_view text, uint128* value, int base) { |
| 1091 | return safe_uint_internal<absl::uint128>(text, value, base); |
| 1092 | } |
| 1093 |
| 1094 | } // namespace numbers_internal |
| 1095 | ABSL_NAMESPACE_END |
| 1096 | } // namespace absl |
| 1097 |