"Fossies" - the Fresh Open Source Software Archive

Member "abseil-cpp-20230802.1/absl/log/internal/append_truncated.h" (18 Sep 2023, 1623 Bytes) of package /linux/misc/abseil-cpp-20230802.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "append_truncated.h" see the Fossies "Dox" file reference documentation.

    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 inline size_t AppendTruncated(absl::string_view src, absl::Span<char> &dst) {
   31   if (src.size() > dst.size()) src = src.substr(0, dst.size());
   32   memcpy(dst.data(), src.data(), src.size());
   33   dst.remove_prefix(src.size());
   34   return src.size();
   35 }
   36 // Likewise, but `n` copies of `c`.
   37 inline size_t AppendTruncated(char c, size_t n, absl::Span<char> &dst) {
   38   if (n > dst.size()) n = dst.size();
   39   memset(dst.data(), c, n);
   40   dst.remove_prefix(n);
   41   return n;
   42 }
   43 }  // namespace log_internal
   44 ABSL_NAMESPACE_END
   45 }  // namespace absl
   46 
   47 #endif  // ABSL_LOG_INTERNAL_APPEND_TRUNCATED_H_