Commit 92bff437 authored by Victor Zverovich's avatar Victor Zverovich Committed by Facebook GitHub Bot

Add a formatter for fbstring

Summary: {fmt} is moving away from relying on implicit conversions because they may introduce ODR violations and not supported consistently. For example, `fbstring` can be formatted via an implicit conversion or via `operator<<`. To make sure this doesn't happen and make the code compatible with future versions of {fmt} add a `formatter` specialization for `fbstring`. This is consistent with `folly::StringPiece` which already has such specialization.

Reviewed By: ot

Differential Revision: D33994222

fbshipit-source-id: 8150415ef45bd97b24aa12dd209b56c977c148c6
parent 6199764f
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
#include <fmt/format.h>
#include <folly/CPortability.h> #include <folly/CPortability.h>
#include <folly/CppAttributes.h> #include <folly/CppAttributes.h>
#include <folly/Likely.h> #include <folly/Likely.h>
...@@ -2730,3 +2731,12 @@ struct IsSomeString; ...@@ -2730,3 +2731,12 @@ struct IsSomeString;
template <> template <>
struct IsSomeString<fbstring> : std::true_type {}; struct IsSomeString<fbstring> : std::true_type {};
} // namespace folly } // namespace folly
template <>
struct fmt::formatter<folly::fbstring> : formatter<fmt::string_view> {
template <typename Context>
typename Context::iterator format(
const folly::fbstring& s, Context& ctx) const {
return formatter<fmt::string_view>::format({s.data(), s.size()}, ctx);
}
};
...@@ -1738,3 +1738,7 @@ TEST(FBString, convertToStringView) { ...@@ -1738,3 +1738,7 @@ TEST(FBString, convertToStringView) {
EXPECT_EQ(sv2, "bar"); EXPECT_EQ(sv2, "bar");
} }
#endif #endif
TEST(FBString, Format) {
EXPECT_EQ(" foo", fmt::format("{:>5}", folly::fbstring("foo")));
}
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