Merge pull request #1 from viniciuslrangel/str-fmt

custom u8string formatter for fmt library
This commit is contained in:
Paris Oplopoios 2024-09-25 15:21:16 +03:00 committed by GitHub
commit 481c3d0640
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -19,3 +19,27 @@ struct fmt::formatter<T, std::enable_if_t<std::is_enum_v<T>, char>>
}
};
#endif
namespace fmt {
template <typename T = std::string_view>
struct UTF {
T data;
explicit UTF(const std::u8string_view view) {
data = T{(const char*)&view.front(), (const char*)&view.back()};
}
explicit UTF(const std::u8string& str)
: UTF(std::u8string_view{str}) {
}
};
}
template <>
struct fmt::formatter<fmt::UTF<std::string_view>, char>
: formatter<std::string_view> {
template <typename FormatContext>
auto format(const UTF<std::string_view>& wrapper, FormatContext& ctx) const {
return formatter<std::string_view>::format(wrapper.data, ctx);
}
};