/Users/deen/code/yugabyte-db/src/yb/util/slice.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | #include "yb/util/slice.h" |
33 | | |
34 | | #include "yb/util/status.h" |
35 | | #include "yb/util/status_format.h" |
36 | | |
37 | | DEFINE_int32(non_graph_characters_percentage_to_use_hexadecimal_rendering, 10, |
38 | | "Non graph charaters percentage to use hexadecimal rendering"); |
39 | | |
40 | | namespace yb { |
41 | | |
42 | 21.2k | Status Slice::check_size(size_t expected_size) const { |
43 | 21.2k | if (PREDICT_FALSE(size() != expected_size)) { |
44 | 0 | return STATUS(Corruption, StringPrintf("Unexpected Slice size. " |
45 | 0 | "Expected %zu but got %zu.", expected_size, size()), ToDebugString(100)); |
46 | 0 | } |
47 | 21.2k | return Status::OK(); |
48 | 21.2k | } |
49 | | |
50 | 7.52M | void Slice::CopyToBuffer(std::string* buffer) const { |
51 | 7.52M | buffer->assign(cdata(), size()); |
52 | 7.52M | } |
53 | | |
54 | | // Return a string that contains the copy of the referenced data. |
55 | 430M | std::string Slice::ToBuffer() const { |
56 | 430M | return std::string(cdata(), size()); |
57 | 430M | } |
58 | | |
59 | 9.91k | std::string Slice::ToString(bool hex) const { |
60 | 9.91k | return hex ? ToDebugHexString()8.27k : ToString()1.63k ; |
61 | 9.91k | } |
62 | | |
63 | 846k | std::string Slice::ToDebugHexString() const { |
64 | 846k | std::string result; |
65 | 846k | char buf[10]; |
66 | 25.4M | for (auto i = begin_; i != end_; ++i24.5M ) { |
67 | 24.5M | snprintf(buf, sizeof(buf), "%02X", *i); |
68 | 24.5M | result += buf; |
69 | 24.5M | } |
70 | 846k | return result; |
71 | 846k | } |
72 | | |
73 | 10.4k | std::string Slice::ToDebugString(size_t max_len) const { |
74 | 10.4k | size_t bytes_to_print = size(); |
75 | 10.4k | bool abbreviated = false; |
76 | 10.4k | if (max_len != 0 && bytes_to_print > max_len14 ) { |
77 | 2 | bytes_to_print = max_len; |
78 | 2 | abbreviated = true; |
79 | 2 | } |
80 | | |
81 | 10.4k | size_t num_not_graph = 0; |
82 | 30.8k | for (size_t i = 0; i < bytes_to_print; i++20.3k ) { |
83 | 20.3k | if (!isgraph(begin_[i])) { |
84 | 9.39k | ++num_not_graph; |
85 | 9.39k | } |
86 | 20.3k | } |
87 | | |
88 | 10.4k | if (num_not_graph * 100 > |
89 | 10.4k | bytes_to_print * FLAGS_non_graph_characters_percentage_to_use_hexadecimal_rendering) { |
90 | 4.70k | return ToDebugHexString(); |
91 | 4.70k | } |
92 | 5.70k | size_t size = bytes_to_print + 3 * num_not_graph + (abbreviated ? 202 : 05.70k ); |
93 | | |
94 | 5.70k | std::string ret; |
95 | 5.70k | ret.reserve(size); |
96 | 16.6k | for (size_t i = 0; i < bytes_to_print; i++10.9k ) { |
97 | 10.9k | auto ch = begin_[i]; |
98 | 10.9k | if (!isgraph(ch)) { |
99 | 0 | if (ch == '\r') { |
100 | 0 | ret += "\\r"; |
101 | 0 | } else if (ch == '\n') { |
102 | 0 | ret += "\\n"; |
103 | 0 | } else if (ch == ' ') { |
104 | 0 | ret += ' '; |
105 | 0 | } else { |
106 | 0 | StringAppendF(&ret, "\\x%02x", ch & 0xff); |
107 | 0 | } |
108 | 10.9k | } else { |
109 | 10.9k | ret.push_back(ch); |
110 | 10.9k | } |
111 | 10.9k | } |
112 | 5.70k | if (abbreviated) { |
113 | 2 | StringAppendF(&ret, "...<%zd bytes total>", this->size()); |
114 | 2 | } |
115 | 5.70k | return ret; |
116 | 10.4k | } |
117 | | |
118 | 1.24k | Slice::Slice(const SliceParts& parts, std::string* buf) { |
119 | 1.24k | size_t length = 0; |
120 | 2.49k | for (int i = 0; i < parts.num_parts; ++i1.25k ) { |
121 | 1.25k | length += parts.parts[i].size(); |
122 | 1.25k | } |
123 | 1.24k | buf->reserve(length); |
124 | | |
125 | 2.49k | for (int i = 0; i < parts.num_parts; ++i1.25k ) { |
126 | 1.25k | buf->append(parts.parts[i].cdata(), parts.parts[i].size()); |
127 | 1.25k | } |
128 | 1.24k | *this = Slice(*buf); |
129 | 1.24k | } |
130 | | |
131 | 121M | Status Slice::consume_byte(char c) { |
132 | 121M | char consumed = consume_byte(); |
133 | 121M | if (consumed != c) { |
134 | 0 | return STATUS_FORMAT(Corruption, "Wrong first byte, expected $0 but found $1", |
135 | 0 | static_cast<int>(c), static_cast<int>(consumed)); |
136 | 0 | } |
137 | | |
138 | 121M | return Status::OK(); |
139 | 121M | } |
140 | | |
141 | 4.29G | uint8_t Slice::operator[](size_t n) const { |
142 | 4.29G | DCHECK_LT(n, size()); |
143 | 4.29G | return begin_[n]; |
144 | 4.29G | } |
145 | | |
146 | 12.6G | void Slice::remove_prefix(size_t n) { |
147 | 12.6G | DCHECK_LE(n, size()); |
148 | 12.6G | begin_ += n; |
149 | 12.6G | } |
150 | | |
151 | 270M | Slice Slice::Prefix(size_t n) const { |
152 | 270M | DCHECK_LE(n, size()); |
153 | 270M | return Slice(begin_, n); |
154 | 270M | } |
155 | | |
156 | 22.2M | Slice Slice::WithoutPrefix(size_t n) const { |
157 | 22.2M | DCHECK_LE(n, size()); |
158 | 22.2M | return Slice(begin_ + n, end_); |
159 | 22.2M | } |
160 | | |
161 | 2.15G | void Slice::remove_suffix(size_t n) { |
162 | 2.15G | DCHECK_LE(n, size()); |
163 | 2.15G | end_ -= n; |
164 | 2.15G | } |
165 | | |
166 | 0 | Slice Slice::Suffix(size_t n) const { |
167 | 0 | DCHECK_LE(n, size()); |
168 | 0 | return Slice(end_ - n, end_); |
169 | 0 | } |
170 | | |
171 | 0 | Slice Slice::WithoutSuffix(size_t n) const { |
172 | 0 | DCHECK_LE(n, size()); |
173 | 0 | return Slice(begin_, end_ - n); |
174 | 0 | } |
175 | | |
176 | 0 | void Slice::truncate(size_t n) { |
177 | 0 | DCHECK_LE(n, size()); |
178 | 0 | end_ = begin_ + n; |
179 | 0 | } |
180 | | |
181 | 5.07G | char Slice::consume_byte() { |
182 | 5.07G | DCHECK_GT(end_, begin_); |
183 | 5.07G | return *begin_++; |
184 | 5.07G | } |
185 | | |
186 | 0 | std::string SliceParts::ToDebugHexString() const { |
187 | 0 | std::string result; |
188 | 0 | for (int i = 0; i != num_parts; ++i) { |
189 | 0 | result += parts[i].ToDebugHexString(); |
190 | 0 | } |
191 | 0 | return result; |
192 | 0 | } |
193 | | |
194 | 798M | size_t SliceParts::SumSizes() const { |
195 | 798M | size_t result = 0; |
196 | 2.74G | for (int i = 0; i != num_parts; ++i1.94G ) { |
197 | 1.94G | result += parts[i].size(); |
198 | 1.94G | } |
199 | 798M | return result; |
200 | 798M | } |
201 | | |
202 | 798M | char* SliceParts::CopyAllTo(char* out) const { |
203 | 2.74G | for (int i = 0; i != num_parts; ++i1.94G ) { |
204 | 1.94G | if (!parts[i].size()) { |
205 | 131M | continue; |
206 | 131M | } |
207 | 1.81G | memcpy(out, parts[i].data(), parts[i].size()); |
208 | 1.81G | out += parts[i].size(); |
209 | 1.81G | } |
210 | 798M | return out; |
211 | 798M | } |
212 | | |
213 | 758k | Slice SliceParts::TheOnlyPart() const { |
214 | 758k | CHECK_EQ(num_parts, 1); |
215 | 758k | return parts[0]; |
216 | 758k | } |
217 | | |
218 | | } // namespace yb |