MCPcopy Create free account
hub / github.com/ElementsProject/elements / ConsumeDecimalNumber

Function ConsumeDecimalNumber

src/leveldb/util/logging.cc:51–82  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

49}
50
51bool ConsumeDecimalNumber(Slice* in, uint64_t* val) {
52 // Constants that will be optimized away.
53 constexpr const uint64_t kMaxUint64 = std::numeric_limits<uint64_t>::max();
54 constexpr const char kLastDigitOfMaxUint64 =
55 '0' + static_cast<char>(kMaxUint64 % 10);
56
57 uint64_t value = 0;
58
59 // reinterpret_cast-ing from char* to uint8_t* to avoid signedness.
60 const uint8_t* start = reinterpret_cast<const uint8_t*>(in->data());
61
62 const uint8_t* end = start + in->size();
63 const uint8_t* current = start;
64 for (; current != end; ++current) {
65 const uint8_t ch = *current;
66 if (ch < '0' || ch > '9') break;
67
68 // Overflow check.
69 // kMaxUint64 / 10 is also constant and will be optimized away.
70 if (value > kMaxUint64 / 10 ||
71 (value == kMaxUint64 / 10 && ch > kLastDigitOfMaxUint64)) {
72 return false;
73 }
74
75 value = (value * 10) + (ch - '0');
76 }
77
78 *val = value;
79 const size_t digits_consumed = current - start;
80 in->remove_prefix(digits_consumed);
81 return digits_consumed != 0;
82}
83
84} // namespace leveldb

Callers 6

CheckMethod · 0.85
GetPropertyMethod · 0.85
ParseFileNameFunction · 0.85

Calls 2

dataMethod · 0.45
sizeMethod · 0.45

Tested by 4

CheckMethod · 0.68