Factor out the internal helper AppendTruncated, which is used and redefined in a couple places, plus several more that have yet to be released.

PiperOrigin-RevId: 486759835
Change-Id: Ib1b24f287f856ca38b691fbce7e747f0f5a34626
diff --git a/absl/log/BUILD.bazel b/absl/log/BUILD.bazel
index 16788ae..261a338 100644
--- a/absl/log/BUILD.bazel
+++ b/absl/log/BUILD.bazel
@@ -303,6 +303,7 @@
         "//absl/base:config",
         "//absl/base:core_headers",
         "//absl/base:log_severity",
+        "//absl/log/internal:append_truncated",
         "//absl/log/internal:format",
         "//absl/log/internal:test_helpers",
         "//absl/strings",
diff --git a/absl/log/CMakeLists.txt b/absl/log/CMakeLists.txt
index 28d4b51..f5b608c 100644
--- a/absl/log/CMakeLists.txt
+++ b/absl/log/CMakeLists.txt
@@ -96,6 +96,7 @@
   DEPS
     absl::config
     absl::core_headers
+    absl::log_internal_append_truncated
     absl::log_internal_config
     absl::log_internal_globals
     absl::log_severity
@@ -143,6 +144,7 @@
     absl::errno_saver
     absl::inlined_vector
     absl::examine_stack
+    absl::log_internal_append_truncated
     absl::log_internal_config
     absl::log_internal_format
     absl::log_internal_globals
@@ -318,6 +320,22 @@
     absl::config
 )
 
+absl_cc_library(
+  NAME
+    log_internal_append_truncated
+  SRCS
+  HDRS
+    "internal/append_truncated.h"
+  COPTS
+    ${ABSL_DEFAULT_COPTS}
+  LINKOPTS
+    ${ABSL_DEFAULT_LINKOPTS}
+  DEPS
+    absl::config
+    absl::strings
+    absl::span
+)
+
 # Public targets
 absl_cc_library(
   NAME
@@ -636,6 +654,7 @@
     absl::config
     absl::core_headers
     absl::log_entry
+    absl::log_internal_append_truncated
     absl::log_internal_format
     absl::log_internal_globals
     absl::log_internal_test_helpers
diff --git a/absl/log/internal/BUILD.bazel b/absl/log/internal/BUILD.bazel
index 3ddae93..af0a4b8 100644
--- a/absl/log/internal/BUILD.bazel
+++ b/absl/log/internal/BUILD.bazel
@@ -91,6 +91,7 @@
     copts = ABSL_DEFAULT_COPTS,
     linkopts = ABSL_DEFAULT_LINKOPTS,
     deps = [
+        ":append_truncated",
         ":config",
         ":globals",
         "//absl/base:config",
@@ -132,6 +133,7 @@
         "//absl/log:__pkg__",
     ],
     deps = [
+        ":append_truncated",
         ":config",
         ":format",
         ":globals",
@@ -159,6 +161,18 @@
 )
 
 cc_library(
+    name = "append_truncated",
+    hdrs = ["append_truncated.h"],
+    copts = ABSL_DEFAULT_COPTS,
+    linkopts = ABSL_DEFAULT_LINKOPTS,
+    deps = [
+        "//absl/base:config",
+        "//absl/strings",
+        "//absl/types:span",
+    ],
+)
+
+cc_library(
     name = "log_sink_set",
     srcs = ["log_sink_set.cc"],
     hdrs = ["log_sink_set.h"],
diff --git a/absl/log/internal/append_truncated.h b/absl/log/internal/append_truncated.h
new file mode 100644
index 0000000..096b751
--- /dev/null
+++ b/absl/log/internal/append_truncated.h
@@ -0,0 +1,40 @@
+// Copyright 2022 The Abseil Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef ABSL_LOG_INTERNAL_APPEND_TRUNCATED_H_
+#define ABSL_LOG_INTERNAL_APPEND_TRUNCATED_H_
+
+#include <cstddef>
+#include <cstring>
+
+#include "absl/base/config.h"
+#include "absl/strings/string_view.h"
+#include "absl/types/span.h"
+
+namespace absl {
+ABSL_NAMESPACE_BEGIN
+namespace log_internal {
+// Copies into `dst` as many bytes of `src` as will fit, then truncates the
+// copied bytes from the front of `dst` and returns the number of bytes written.
+inline size_t AppendTruncated(absl::string_view src, absl::Span<char> &dst) {
+  if (src.size() > dst.size()) src = src.substr(0, dst.size());
+  memcpy(dst.data(), src.data(), src.size());
+  dst.remove_prefix(src.size());
+  return src.size();
+}
+}  // namespace log_internal
+ABSL_NAMESPACE_END
+}  // namespace absl
+
+#endif  // ABSL_LOG_INTERNAL_APPEND_TRUNCATED_H_
diff --git a/absl/log/internal/log_format.cc b/absl/log/internal/log_format.cc
index 5b280a2..cf8cdfd 100644
--- a/absl/log/internal/log_format.cc
+++ b/absl/log/internal/log_format.cc
@@ -32,6 +32,7 @@
 #include "absl/base/config.h"
 #include "absl/base/log_severity.h"
 #include "absl/base/optimization.h"
+#include "absl/log/internal/append_truncated.h"
 #include "absl/log/internal/config.h"
 #include "absl/log/internal/globals.h"
 #include "absl/strings/numbers.h"
@@ -143,15 +144,6 @@
   return bytes_formatted;
 }
 
-// Copies into `dst` as many bytes of `src` as will fit, then advances `dst`
-// past the copied bytes and returns the number of bytes written.
-size_t AppendTruncated(absl::string_view src, absl::Span<char>& dst) {
-  if (src.size() > dst.size()) src = src.substr(0, dst.size());
-  memcpy(dst.data(), src.data(), src.size());
-  dst.remove_prefix(src.size());
-  return src.size();
-}
-
 size_t FormatLineNumber(int line, absl::Span<char>& buf) {
   constexpr size_t kLineFieldMaxLen =
       sizeof(":] ") + (1 + std::numeric_limits<int>::digits10 + 1) - sizeof("");
@@ -199,7 +191,7 @@
                        log_internal::Tid tid, absl::string_view basename,
                        int line, absl::Span<char>& buf) {
   auto prefix_size = FormatBoundedFields(severity, timestamp, tid, buf);
-  prefix_size += AppendTruncated(basename, buf);
+  prefix_size += log_internal::AppendTruncated(basename, buf);
   prefix_size += FormatLineNumber(line, buf);
   return prefix_size;
 }
diff --git a/absl/log/internal/log_message.cc b/absl/log/internal/log_message.cc
index f32c7e6..98e45f8 100644
--- a/absl/log/internal/log_message.cc
+++ b/absl/log/internal/log_message.cc
@@ -41,6 +41,7 @@
 #include "absl/container/inlined_vector.h"
 #include "absl/debugging/internal/examine_stack.h"
 #include "absl/log/globals.h"
+#include "absl/log/internal/append_truncated.h"
 #include "absl/log/internal/config.h"
 #include "absl/log/internal/globals.h"
 #include "absl/log/internal/log_format.h"
@@ -65,14 +66,6 @@
 namespace log_internal {
 
 namespace {
-// Copies into `dst` as many bytes of `src` as will fit, then truncates the
-// copied bytes from the front of `dst` and returns the number of bytes written.
-size_t AppendTruncated(absl::string_view src, absl::Span<char>* dst) {
-  if (src.size() > dst->size()) src = src.substr(0, dst->size());
-  memcpy(dst->data(), src.data(), src.size());
-  dst->remove_prefix(src.size());
-  return src.size();
-}
 
 absl::string_view Basename(absl::string_view filepath) {
 #ifdef _WIN32
@@ -163,7 +156,7 @@
 
   size_t Append(absl::string_view data) {
     absl::Span<char> remaining(pptr(), static_cast<size_t>(epptr() - pptr()));
-    const size_t written = AppendTruncated(data, &remaining);
+    const size_t written = log_internal::AppendTruncated(data, remaining);
     pbump(static_cast<int>(written));
     return written;
   }
diff --git a/absl/log/log_entry_test.cc b/absl/log/log_entry_test.cc
index 7238356..b1b21c0 100644
--- a/absl/log/log_entry_test.cc
+++ b/absl/log/log_entry_test.cc
@@ -30,6 +30,7 @@
 #include "absl/base/attributes.h"
 #include "absl/base/config.h"
 #include "absl/base/log_severity.h"
+#include "absl/log/internal/append_truncated.h"
 #include "absl/log/internal/log_format.h"
 #include "absl/log/internal/test_helpers.h"
 #include "absl/strings/numbers.h"
@@ -40,7 +41,6 @@
 #include "absl/types/span.h"
 
 namespace {
-
 using ::absl::log_internal::LogEntryTestPeer;
 using ::testing::Eq;
 using ::testing::IsTrue;
@@ -49,16 +49,6 @@
 
 auto* test_env ABSL_ATTRIBUTE_UNUSED = ::testing::AddGlobalTestEnvironment(
     new absl::log_internal::LogTestEnvironment);
-
-// Copies into `dst` as many bytes of `src` as will fit, then truncates the
-// copied bytes from the front of `dst` and returns the number of bytes written.
-size_t AppendTruncated(absl::string_view src, absl::Span<char>& dst) {
-  if (src.size() > dst.size()) src = src.substr(0, dst.size());
-  memcpy(dst.data(), src.data(), src.size());
-  dst.remove_prefix(src.size());
-  return src.size();
-}
-
 }  // namespace
 
 namespace absl {
@@ -103,7 +93,7 @@
 
     EXPECT_THAT(entry_.prefix_len_,
                 Eq(static_cast<size_t>(view.data() - buf_.data())));
-    AppendTruncated(text_message, view);
+    log_internal::AppendTruncated(text_message, view);
     view = absl::Span<char>(view.data(), view.size() + 2);
     view[0] = '\n';
     view[1] = '\0';