summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--absl/strings/BUILD.bazel3
-rw-r--r--absl/strings/CMakeLists.txt1
-rw-r--r--absl/strings/str_cat.h5
-rw-r--r--absl/strings/str_cat_test.cc18
-rw-r--r--absl/strings/str_format.h5
-rw-r--r--absl/strings/str_format_test.cc17
6 files changed, 47 insertions, 2 deletions
diff --git a/absl/strings/BUILD.bazel b/absl/strings/BUILD.bazel
index 5a40887e..5b12c010 100644
--- a/absl/strings/BUILD.bazel
+++ b/absl/strings/BUILD.bazel
@@ -963,8 +963,8 @@ cc_test(
copts = ABSL_TEST_COPTS,
visibility = ["//visibility:private"],
deps = [
+ ":str_format",
":strings",
- "//absl/base:core_headers",
"@com_google_googletest//:gtest_main",
],
)
@@ -1164,7 +1164,6 @@ cc_test(
":cord",
":str_format",
":strings",
- "//absl/base:core_headers",
"@com_google_googletest//:gtest_main",
],
)
diff --git a/absl/strings/CMakeLists.txt b/absl/strings/CMakeLists.txt
index e1c2093a..01f86184 100644
--- a/absl/strings/CMakeLists.txt
+++ b/absl/strings/CMakeLists.txt
@@ -281,6 +281,7 @@ absl_cc_test(
${ABSL_TEST_COPTS}
DEPS
absl::strings
+ absl::str_format
absl::core_headers
GTest::gmock_main
)
diff --git a/absl/strings/str_cat.h b/absl/strings/str_cat.h
index 05728ab5..6ee88f14 100644
--- a/absl/strings/str_cat.h
+++ b/absl/strings/str_cat.h
@@ -85,6 +85,11 @@ class StringifySink {
bool PutPaddedString(string_view v, int width, int precision, bool left);
+ // Support `absl::Format(&sink, format, args...)`.
+ friend void AbslFormatFlush(StringifySink* sink, absl::string_view v) {
+ sink->Append(v);
+ }
+
template <typename T>
friend string_view ExtractStringification(StringifySink& sink, const T& v);
diff --git a/absl/strings/str_cat_test.cc b/absl/strings/str_cat_test.cc
index 868b9bce..1b3b7ece 100644
--- a/absl/strings/str_cat_test.cc
+++ b/absl/strings/str_cat_test.cc
@@ -21,6 +21,7 @@
#include <vector>
#include "gtest/gtest.h"
+#include "absl/strings/str_format.h"
#include "absl/strings/substitute.h"
#ifdef __ANDROID__
@@ -632,4 +633,21 @@ TEST(StrCat, AbslStringifyExample) {
EXPECT_EQ(absl::StrCat("a ", p, " z"), "a (10, 20) z");
}
+struct PointStringifyUsingFormat {
+ template <typename FormatSink>
+ friend void AbslStringify(FormatSink& sink,
+ const PointStringifyUsingFormat& p) {
+ absl::Format(&sink, "(%g, %g)", p.x, p.y);
+ }
+
+ double x = 10.0;
+ double y = 20.0;
+};
+
+TEST(StrCat, AbslStringifyExampleUsingFormat) {
+ PointStringifyUsingFormat p;
+ EXPECT_EQ(absl::StrCat(p), "(10, 20)");
+ EXPECT_EQ(absl::StrCat("a ", p, " z"), "a (10, 20) z");
+}
+
} // namespace
diff --git a/absl/strings/str_format.h b/absl/strings/str_format.h
index e6537ea0..ffbcb9af 100644
--- a/absl/strings/str_format.h
+++ b/absl/strings/str_format.h
@@ -789,6 +789,11 @@ class FormatSink {
return sink_->PutPaddedString(v, width, precision, left);
}
+ // Support `absl::Format(&sink, format, args...)`.
+ friend void AbslFormatFlush(FormatSink* sink, absl::string_view v) {
+ sink->Append(v);
+ }
+
private:
friend str_format_internal::FormatSinkImpl;
explicit FormatSink(str_format_internal::FormatSinkImpl* s) : sink_(s) {}
diff --git a/absl/strings/str_format_test.cc b/absl/strings/str_format_test.cc
index 0c4f10c8..62ed262d 100644
--- a/absl/strings/str_format_test.cc
+++ b/absl/strings/str_format_test.cc
@@ -1118,6 +1118,23 @@ TEST_F(FormatExtensionTest, AbslStringifyExample) {
PointStringify p;
EXPECT_EQ(absl::StrFormat("a %v z", p), "a (10, 20) z");
}
+
+struct PointStringifyUsingFormat {
+ template <typename FormatSink>
+ friend void AbslStringify(FormatSink& sink,
+ const PointStringifyUsingFormat& p) {
+ absl::Format(&sink, "(%g, %g)", p.x, p.y);
+ }
+
+ double x = 10.0;
+ double y = 20.0;
+};
+
+TEST_F(FormatExtensionTest, AbslStringifyExampleUsingFormat) {
+ PointStringifyUsingFormat p;
+ EXPECT_EQ(absl::StrFormat("a %v z", p), "a (10, 20) z");
+}
+
} // namespace
// Some codegen thunks that we can use to easily dump the generated assembly for