YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/bytes_formatter.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
14
#include "yb/util/bytes_formatter.h"
15
16
#include "yb/gutil/stringprintf.h"
17
#include "yb/gutil/strings/substitute.h"
18
19
#include "yb/util/cast.h"
20
#include "yb/util/enums.h"
21
#include "yb/util/format.h"
22
23
using std::string;
24
using strings::Substitute;
25
26
namespace yb {
27
28
string FormatBytesAsStr(const char* data,
29
                        const size_t n,
30
                        const QuotesType quotes_type,
31
159k
                        const size_t max_length) {
32
159k
  string result;
33
34
159k
  char quote = '"';
35
159k
  switch (quotes_type) {
36
76
    case QuotesType::kSingleQuotes:
37
76
      quote = '\'';
38
76
      break;
39
158k
    case QuotesType::kDoubleQuotes:
40
158k
      quote = '"';
41
158k
      break;
42
159k
  }
43
44
159k
  result.push_back(quote);
45
159k
  const char* end = data + n;
46
47
  // Not including the current character we're looking at. Cast to a signed int to avoid underflow.
48
159k
  int64_t bytes_left = static_cast<int64_t>(n - 1);
49
50
123M
  for (const char* p = data; p != end; ++p, --bytes_left) {
51
123M
    uint8_t c = static_cast<uint8_t>(*p);
52
123M
    if (c == quote) {
53
4
      result.push_back('\\');
54
4
      result.push_back(quote);
55
123M
    } else if (c == '\\') {
56
2
      result.append("\\\\");
57
123M
    } else if (isgraph(c) || c == ' ') {
58
123M
      result.push_back(c);
59
235
    } else {
60
235
      result.append(StringPrintf("\\x%02x", c));
61
235
    }
62
    // See if we went above the max size. Don't bother if there is only one byte left to print,
63
    // so that we can always say "bytes".
64
123M
    if (result.size() >= max_length && bytes_left > 1) {
65
3
      result.append(Substitute("<...$0 bytes skipped>", bytes_left));
66
3
      break;
67
3
    }
68
123M
  }
69
159k
  result.push_back(quote);
70
159k
  return result;
71
159k
}
72
73
159k
string FormatBytesAsStr(const string& s, QuotesType quotes_type, size_t max_length) {
74
159k
  return FormatBytesAsStr(s.c_str(), s.size(), quotes_type, max_length);
75
159k
}
76
77
0
string FormatSliceAsStr(const Slice& s, QuotesType quotes_type, size_t max_length) {
78
0
  return FormatBytesAsStr(s.cdata(), s.size(), quotes_type, max_length);
79
0
}
80
81
std::string FormatSliceAsStr(
82
    const yb::Slice& slice,
83
    BinaryOutputFormat output_format,
84
    QuotesType quote_type,
85
0
    size_t max_length) {
86
0
  switch (output_format) {
87
0
    case BinaryOutputFormat::kEscaped:
88
0
      return FormatSliceAsStr(slice, quote_type, max_length);
89
0
    case BinaryOutputFormat::kHex:
90
0
      return slice.ToDebugHexString();
91
0
    case BinaryOutputFormat::kEscapedAndHex:
92
0
      return Format(
93
0
          "$0 ($1)",
94
0
          FormatSliceAsStr(slice, quote_type, max_length), slice.ToDebugHexString());
95
0
  }
96
0
  FATAL_INVALID_ENUM_VALUE(BinaryOutputFormat, output_format);
97
0
}
98
99
}  // namespace yb