Make sinks provided to `AbslStringify()` usable with `absl::Format()`.

Such sinks must define ADL-callable `AbslFormatFlush()`. It can just forward to
`Append()`.

PiperOrigin-RevId: 479043790
Change-Id: I5d7d80ca1e17adf03b77726df8a52e2b4e9196ce
pull/1285/head
Marcin Kowalczyk 2 years ago committed by Copybara-Service
parent d24156f63c
commit b3162b1da6
  1. 3
      absl/strings/BUILD.bazel
  2. 1
      absl/strings/CMakeLists.txt
  3. 5
      absl/strings/str_cat.h
  4. 18
      absl/strings/str_cat_test.cc
  5. 5
      absl/strings/str_format.h
  6. 17
      absl/strings/str_format_test.cc

@ -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",
],
)

@ -281,6 +281,7 @@ absl_cc_test(
${ABSL_TEST_COPTS}
DEPS
absl::strings
absl::str_format
absl::core_headers
GTest::gmock_main
)

@ -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);

@ -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

@ -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) {}

@ -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

Loading…
Cancel
Save