/src/abseil-cpp/absl/log/internal/append_truncated.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2022 The Abseil Authors |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #ifndef ABSL_LOG_INTERNAL_APPEND_TRUNCATED_H_ |
16 | | #define ABSL_LOG_INTERNAL_APPEND_TRUNCATED_H_ |
17 | | |
18 | | #include <cstddef> |
19 | | #include <cstring> |
20 | | |
21 | | #include "absl/base/config.h" |
22 | | #include "absl/strings/string_view.h" |
23 | | #include "absl/types/span.h" |
24 | | |
25 | | namespace absl { |
26 | | ABSL_NAMESPACE_BEGIN |
27 | | namespace log_internal { |
28 | | // Copies into `dst` as many bytes of `src` as will fit, then truncates the |
29 | | // copied bytes from the front of `dst` and returns the number of bytes written. |
30 | 10.1M | inline size_t AppendTruncated(absl::string_view src, absl::Span<char> &dst) { |
31 | 10.1M | if (src.size() > dst.size()) src = src.substr(0, dst.size()); |
32 | 10.1M | memcpy(dst.data(), src.data(), src.size()); |
33 | 10.1M | dst.remove_prefix(src.size()); |
34 | 10.1M | return src.size(); |
35 | 10.1M | } |
36 | | // Likewise, but `n` copies of `c`. |
37 | 0 | inline size_t AppendTruncated(char c, size_t n, absl::Span<char> &dst) { |
38 | 0 | if (n > dst.size()) n = dst.size(); |
39 | 0 | memset(dst.data(), c, n); |
40 | 0 | dst.remove_prefix(n); |
41 | 0 | return n; |
42 | 0 | } |
43 | | } // namespace log_internal |
44 | | ABSL_NAMESPACE_END |
45 | | } // namespace absl |
46 | | |
47 | | #endif // ABSL_LOG_INTERNAL_APPEND_TRUNCATED_H_ |