Coverage Report

Created: 2025-07-18 07:11

/src/leveldb/util/logging.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style license that can be
3
// found in the LICENSE file. See the AUTHORS file for names of contributors.
4
5
#include "util/logging.h"
6
7
#include <cstdarg>
8
#include <cstdio>
9
#include <cstdlib>
10
#include <limits>
11
12
#include "leveldb/env.h"
13
#include "leveldb/slice.h"
14
15
namespace leveldb {
16
17
6.25k
void AppendNumberTo(std::string* str, uint64_t num) {
18
6.25k
  char buf[30];
19
6.25k
  std::snprintf(buf, sizeof(buf), "%llu", static_cast<unsigned long long>(num));
20
6.25k
  str->append(buf);
21
6.25k
}
22
23
128k
void AppendEscapedStringTo(std::string* str, const Slice& value) {
24
64.8M
  for (size_t i = 0; i < value.size(); i++) {
25
64.7M
    char c = value[i];
26
64.7M
    if (c >= ' ' && c <= '~') {
27
6.36M
      str->push_back(c);
28
58.3M
    } else {
29
58.3M
      char buf[10];
30
58.3M
      std::snprintf(buf, sizeof(buf), "\\x%02x",
31
58.3M
                    static_cast<unsigned int>(c) & 0xff);
32
58.3M
      str->append(buf);
33
58.3M
    }
34
64.7M
  }
35
128k
}
36
37
0
std::string NumberToString(uint64_t num) {
38
0
  std::string r;
39
0
  AppendNumberTo(&r, num);
40
0
  return r;
41
0
}
42
43
128k
std::string EscapeString(const Slice& value) {
44
128k
  std::string r;
45
128k
  AppendEscapedStringTo(&r, value);
46
128k
  return r;
47
128k
}
48
49
3.42M
bool ConsumeDecimalNumber(Slice* in, uint64_t* val) {
50
  // Constants that will be optimized away.
51
3.42M
  constexpr const uint64_t kMaxUint64 = std::numeric_limits<uint64_t>::max();
52
3.42M
  constexpr const char kLastDigitOfMaxUint64 =
53
3.42M
      '0' + static_cast<char>(kMaxUint64 % 10);
54
55
3.42M
  uint64_t value = 0;
56
57
  // reinterpret_cast-ing from char* to uint8_t* to avoid signedness.
58
3.42M
  const uint8_t* start = reinterpret_cast<const uint8_t*>(in->data());
59
60
3.42M
  const uint8_t* end = start + in->size();
61
3.42M
  const uint8_t* current = start;
62
20.5M
  for (; current != end; ++current) {
63
20.1M
    const uint8_t ch = *current;
64
20.1M
    if (ch < '0' || ch > '9') break;
65
66
    // Overflow check.
67
    // kMaxUint64 / 10 is also constant and will be optimized away.
68
17.1M
    if (value > kMaxUint64 / 10 ||
69
17.1M
        (value == kMaxUint64 / 10 && ch > kLastDigitOfMaxUint64)) {
70
143
      return false;
71
143
    }
72
73
17.1M
    value = (value * 10) + (ch - '0');
74
17.1M
  }
75
76
3.42M
  *val = value;
77
3.42M
  const size_t digits_consumed = current - start;
78
3.42M
  in->remove_prefix(digits_consumed);
79
3.42M
  return digits_consumed != 0;
80
3.42M
}
81
82
}  // namespace leveldb