Commit 6d95d30f authored by Victor Zverovich's avatar Victor Zverovich Committed by Facebook GitHub Bot

Add a StringPiece formatter

Summary: Add an {fmt} formatter for `StringPiece`. This is much faster than going through ostream `operator<<` which we fallback to otherwise.

Reviewed By: yfeldblum

Differential Revision: D24337424

fbshipit-source-id: c6f1775cf83cfa6bcaa25cb4f6b665a76672e7c3
parent 35b4d759
...@@ -41,6 +41,8 @@ ...@@ -41,6 +41,8 @@
#include <string_view> // @manual #include <string_view> // @manual
#endif #endif
#include <fmt/format.h>
#include <folly/CpuId.h> #include <folly/CpuId.h>
#include <folly/Likely.h> #include <folly/Likely.h>
#include <folly/Traits.h> #include <folly/Traits.h>
...@@ -1524,6 +1526,19 @@ constexpr Range<wchar_t const*> operator"" _sp( ...@@ -1524,6 +1526,19 @@ constexpr Range<wchar_t const*> operator"" _sp(
} // namespace folly } // namespace folly
// Avoid ambiguity in older fmt versions due to StringPiece's conversions.
#if FMT_VERSION >= 70000
template <>
struct fmt::formatter<folly::StringPiece> : private formatter<string_view> {
using formatter<string_view>::parse;
template <typename Context>
auto format(folly::StringPiece s, Context& ctx) {
return formatter<string_view>::format({s.data(), s.size()}, ctx);
}
};
#endif
FOLLY_POP_WARNING FOLLY_POP_WARNING
FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(folly::Range) FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(folly::Range)
......
...@@ -1638,6 +1638,10 @@ TEST(StringPiece, StringViewConversion) { ...@@ -1638,6 +1638,10 @@ TEST(StringPiece, StringViewConversion) {
EXPECT_EQ(tt3.which, 1); EXPECT_EQ(tt3.which, 1);
} }
TEST(StringPiece, Format) {
EXPECT_EQ(" foo", fmt::format("{:>5}", folly::StringPiece("foo")));
}
namespace { namespace {
// Range with non-pod value type should not cause compile errors. // Range with non-pod value type should not cause compile errors.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment