Skip to content

Commit

Permalink
Add support for std::string_view in StrCat even when
Browse files Browse the repository at this point in the history
absl::string_view != std::string_view.

PiperOrigin-RevId: 704802270
Change-Id: I8293f755a688707db575f0df22440f24ffad430e
  • Loading branch information
Abseil Team authored and copybara-github committed Dec 10, 2024
1 parent 4528747 commit 28528f5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions absl/strings/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,7 @@ cc_test(
deps = [
":str_format",
":strings",
"//absl/base:config",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
Expand Down
1 change: 1 addition & 0 deletions absl/strings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ absl_cc_test(
DEPS
absl::strings
absl::str_format
absl::config
absl::core_headers
GTest::gmock_main
)
Expand Down
11 changes: 11 additions & 0 deletions absl/strings/str_cat.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions absl/strings/str_cat_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 28528f5

Please sign in to comment.