/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 | | #include <string_view> |
21 | | |
22 | | #include "absl/base/config.h" |
23 | | #include "absl/strings/internal/utf8.h" |
24 | | #include "absl/strings/string_view.h" |
25 | | #include "absl/types/span.h" |
26 | | |
27 | | namespace absl { |
28 | | ABSL_NAMESPACE_BEGIN |
29 | | namespace log_internal { |
30 | | // Copies into `dst` as many bytes of `src` as will fit, then truncates the |
31 | | // copied bytes from the front of `dst` and returns the number of bytes written. |
32 | 0 | inline size_t AppendTruncated(absl::string_view src, absl::Span<char> &dst) { |
33 | 0 | if (src.size() > dst.size()) src = src.substr(0, dst.size()); |
34 | 0 | memcpy(dst.data(), src.data(), src.size()); |
35 | 0 | dst.remove_prefix(src.size()); |
36 | 0 | return src.size(); |
37 | 0 | } |
38 | | // Likewise, but it also takes a wide character string and transforms it into a |
39 | | // UTF-8 encoded byte string regardless of the current locale. |
40 | | // - On platforms where `wchar_t` is 2 bytes (e.g., Windows), the input is |
41 | | // treated as UTF-16. |
42 | | // - On platforms where `wchar_t` is 4 bytes (e.g., Linux, macOS), the input |
43 | | // is treated as UTF-32. |
44 | 0 | inline size_t AppendTruncated(std::wstring_view src, absl::Span<char> &dst) { |
45 | 0 | absl::strings_internal::ShiftState state; |
46 | 0 | size_t total_bytes_written = 0; |
47 | 0 | for (const wchar_t wc : src) { |
48 | | // If the destination buffer might not be large enough to write the next |
49 | | // character, stop. |
50 | 0 | if (dst.size() < absl::strings_internal::kMaxEncodedUTF8Size) break; |
51 | 0 | size_t bytes_written = |
52 | 0 | absl::strings_internal::WideToUtf8(wc, dst.data(), state); |
53 | 0 | if (bytes_written == static_cast<size_t>(-1)) { |
54 | | // Invalid character. Encode REPLACEMENT CHARACTER (U+FFFD) instead. |
55 | 0 | constexpr wchar_t kReplacementCharacter = L'\uFFFD'; |
56 | 0 | bytes_written = absl::strings_internal::WideToUtf8(kReplacementCharacter, |
57 | 0 | dst.data(), state); |
58 | 0 | } |
59 | 0 | dst.remove_prefix(bytes_written); |
60 | 0 | total_bytes_written += bytes_written; |
61 | 0 | } |
62 | 0 | return total_bytes_written; |
63 | 0 | } |
64 | | // Likewise, but `n` copies of `c`. |
65 | 0 | inline size_t AppendTruncated(char c, size_t n, absl::Span<char> &dst) { |
66 | 0 | if (n > dst.size()) n = dst.size(); |
67 | 0 | memset(dst.data(), c, n); |
68 | 0 | dst.remove_prefix(n); |
69 | 0 | return n; |
70 | 0 | } |
71 | | } // namespace log_internal |
72 | | ABSL_NAMESPACE_END |
73 | | } // namespace absl |
74 | | |
75 | | #endif // ABSL_LOG_INTERNAL_APPEND_TRUNCATED_H_ |