Add support for std::string_view in StrCat even when

absl::string_view != std::string_view.

PiperOrigin-RevId: 704802270
Change-Id: I8293f755a688707db575f0df22440f24ffad430e
This commit is contained in:
Abseil Team
2024-12-10 12:14:58 -08:00
committed by Copybara-Service
parent 45287476e8
commit 28528f595e
4 changed files with 26 additions and 0 deletions

View File

@@ -1118,6 +1118,7 @@ cc_test(
deps = [
":str_format",
":strings",
"//absl/base:config",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],

View File

@@ -371,6 +371,7 @@ absl_cc_test(
DEPS
absl::strings
absl::str_format
absl::config
absl::core_headers
GTest::gmock_main
)

View File

@@ -101,6 +101,7 @@
#include <vector>
#include "absl/base/attributes.h"
#include "absl/base/config.h"
#include "absl/base/nullability.h"
#include "absl/base/port.h"
#include "absl/meta/type_traits.h"
@@ -110,6 +111,10 @@
#include "absl/strings/numbers.h"
#include "absl/strings/string_view.h"
#if defined(ABSL_HAVE_STD_STRING_VIEW) && !defined(ABSL_USES_STD_STRING_VIEW)
#include <string_view>
#endif
namespace absl {
ABSL_NAMESPACE_BEGIN
@@ -361,6 +366,12 @@ class AlphaNum {
ABSL_ATTRIBUTE_LIFETIME_BOUND)
: piece_(pc) {}
#if defined(ABSL_HAVE_STD_STRING_VIEW) && !defined(ABSL_USES_STD_STRING_VIEW)
AlphaNum(std::string_view pc // NOLINT(runtime/explicit)
ABSL_ATTRIBUTE_LIFETIME_BOUND)
: piece_(pc.data(), pc.size()) {}
#endif // !ABSL_USES_STD_STRING_VIEW
template <typename T, typename = typename std::enable_if<
HasAbslStringify<T>::value>::type>
AlphaNum( // NOLINT(runtime/explicit)

View File

@@ -24,9 +24,14 @@
#include <vector>
#include "gtest/gtest.h"
#include "absl/base/config.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#if defined(ABSL_HAVE_STD_STRING_VIEW)
#include <string_view>
#endif
#ifdef __ANDROID__
// Android assert messages only go to system log, so death tests cannot inspect
// the message for matching.
@@ -214,6 +219,14 @@ TEST(StrCat, CornerCases) {
EXPECT_EQ(result, "");
}
#if defined(ABSL_HAVE_STD_STRING_VIEW)
TEST(StrCat, StdStringView) {
std::string_view pieces[] = {"Hello", ", ", "World", "!"};
EXPECT_EQ(absl::StrCat(pieces[0], pieces[1], pieces[2], pieces[3]),
"Hello, World!");
}
#endif // ABSL_HAVE_STD_STRING_VIEW
TEST(StrCat, NullConstCharPtr) {
const char* null = nullptr;
EXPECT_EQ(absl::StrCat("mon", null, "key"), "monkey");