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 @@
#include <type_traits>
#include <utility>
#include <fmt/format.h>
#include <folly/CPortability.h>
#include <folly/CppAttributes.h>
#include <folly/Likely.h>
......@@ -2730,3 +2731,12 @@ struct IsSomeString;
template <>
struct IsSomeString<fbstring> : std::true_type {};
} // 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) {
EXPECT_EQ(sv2, "bar");
}
#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