numbers.cc 39 KB

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