/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 | 6.10M | const size_t max_length) { |
32 | 6.10M | string result; |
33 | | |
34 | 6.10M | char quote = '"'; |
35 | 6.10M | switch (quotes_type) { |
36 | 141 | case QuotesType::kSingleQuotes: |
37 | 141 | quote = '\''; |
38 | 141 | break; |
39 | 6.10M | case QuotesType::kDoubleQuotes: |
40 | 6.10M | quote = '"'; |
41 | 6.10M | break; |
42 | 6.10M | } |
43 | | |
44 | 6.10M | result.push_back(quote); |
45 | 6.10M | 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 | 6.10M | int64_t bytes_left = static_cast<int64_t>(n - 1); |
49 | | |
50 | 177M | for (const char* p = data; p != end; ++p, --bytes_left171M ) { |
51 | 171M | uint8_t c = static_cast<uint8_t>(*p); |
52 | 171M | if (c == quote) { |
53 | 30.0k | result.push_back('\\'); |
54 | 30.0k | result.push_back(quote); |
55 | 171M | } else if (c == '\\') { |
56 | 22.9k | result.append("\\\\"); |
57 | 171M | } else if (isgraph(c) || c == ' '15.4M ) { |
58 | 156M | result.push_back(c); |
59 | 156M | } else { |
60 | 14.9M | result.append(StringPrintf("\\x%02x", c)); |
61 | 14.9M | } |
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 | 171M | if (result.size() >= max_length && bytes_left > 13 ) { |
65 | 3 | result.append(Substitute("<...$0 bytes skipped>", bytes_left)); |
66 | 3 | break; |
67 | 3 | } |
68 | 171M | } |
69 | 6.10M | result.push_back(quote); |
70 | 6.10M | return result; |
71 | 6.10M | } |
72 | | |
73 | 6.08M | string FormatBytesAsStr(const string& s, QuotesType quotes_type, size_t max_length) { |
74 | 6.08M | return FormatBytesAsStr(s.c_str(), s.size(), quotes_type, max_length); |
75 | 6.08M | } |
76 | | |
77 | 16.8k | string FormatSliceAsStr(const Slice& s, QuotesType quotes_type, size_t max_length) { |
78 | 16.8k | return FormatBytesAsStr(s.cdata(), s.size(), quotes_type, max_length); |
79 | 16.8k | } |
80 | | |
81 | | std::string FormatSliceAsStr( |
82 | | const yb::Slice& slice, |
83 | | BinaryOutputFormat output_format, |
84 | | QuotesType quote_type, |
85 | 64 | size_t max_length) { |
86 | 64 | switch (output_format) { |
87 | 64 | case BinaryOutputFormat::kEscaped: |
88 | 64 | 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 | 64 | } |
96 | 0 | FATAL_INVALID_ENUM_VALUE(BinaryOutputFormat, output_format); |
97 | 0 | } |
98 | | |
99 | | } // namespace yb |