zstring: Support std::format
Change-Id: I8ea0a298e438fddb61573eb8e2349ecd125e60f1
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/include/stdplus/zstring.hpp b/include/stdplus/zstring.hpp
index 29288ae..d36b4dc 100644
--- a/include/stdplus/zstring.hpp
+++ b/include/stdplus/zstring.hpp
@@ -249,6 +249,25 @@
namespace fmt
{
+template <typename CharT, typename Traits>
+struct formatter<stdplus::basic_zstring<CharT, Traits>,
+ std::remove_const_t<CharT>> :
+ formatter<const CharT*, std::remove_const_t<CharT>>
+{
+ template <typename FormatContext>
+ inline auto format(stdplus::basic_zstring<CharT, Traits> str,
+ FormatContext& ctx) const
+ {
+ return formatter<const CharT*, std::remove_const_t<CharT>>::format(
+ str.c_str(), ctx);
+ }
+};
+} // namespace fmt
+
+namespace std
+{
+template <typename T, typename CharT>
+struct formatter;
template <typename CharT, typename Traits>
struct formatter<stdplus::basic_zstring<CharT, Traits>,
@@ -263,5 +282,4 @@
str.c_str(), ctx);
}
};
-
-} // namespace fmt
+} // namespace std
diff --git a/include/stdplus/zstring_view.hpp b/include/stdplus/zstring_view.hpp
index 19697f7..e1d180f 100644
--- a/include/stdplus/zstring_view.hpp
+++ b/include/stdplus/zstring_view.hpp
@@ -355,6 +355,24 @@
namespace fmt
{
+template <typename CharT, typename Traits>
+struct formatter<stdplus::basic_zstring_view<CharT, Traits>, CharT> :
+ formatter<basic_string_view<CharT>, CharT>
+{
+ template <typename FormatContext>
+ inline auto format(stdplus::basic_zstring_view<CharT, Traits> str,
+ FormatContext& ctx) const
+ {
+ return formatter<basic_string_view<CharT>, CharT>::format(
+ static_cast<std::basic_string_view<CharT, Traits>>(str), ctx);
+ }
+};
+} // namespace fmt
+
+namespace std
+{
+template <typename T, typename CharT>
+struct formatter;
template <typename CharT, typename Traits>
struct formatter<stdplus::basic_zstring_view<CharT, Traits>, CharT> :
@@ -368,5 +386,4 @@
static_cast<std::basic_string_view<CharT, Traits>>(str), ctx);
}
};
-
-} // namespace fmt
+} // namespace std
diff --git a/test/zstring.cpp b/test/zstring.cpp
index 53c3a14..25ce5b0 100644
--- a/test/zstring.cpp
+++ b/test/zstring.cpp
@@ -2,6 +2,7 @@
#include <stdplus/zstring.hpp>
+#include <format>
#include <iostream>
#include <string>
#include <string_view>
@@ -39,6 +40,8 @@
std::cerr << const_zstring(as);
EXPECT_EQ("dd", fmt::format("d{}", zs));
EXPECT_EQ("dc", fmt::format("d{}", const_zstring(as)));
+ EXPECT_EQ("dd", std::format("d{}", zs));
+ EXPECT_EQ("dc", std::format("d{}", const_zstring(as)));
auto from_str = [&](const_zstring cs) { EXPECT_EQ(cs, "ac"); };
from_str("ac"s);
diff --git a/test/zstring_view.cpp b/test/zstring_view.cpp
index 0ef7fb7..f856757 100644
--- a/test/zstring_view.cpp
+++ b/test/zstring_view.cpp
@@ -2,6 +2,7 @@
#include <stdplus/zstring_view.hpp>
+#include <format>
#include <iostream>
#include <set>
#include <string>
@@ -57,6 +58,7 @@
std::cerr << s1;
EXPECT_EQ("bac", fmt::format("b{}", s1));
+ EXPECT_EQ("bac", std::format("b{}", s1));
std::unordered_set<zstring_view> uset{s1, s2};
EXPECT_EQ(1, uset.count("ac"));