Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specialize formatter for all std::basic_string types #3943

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -4017,11 +4017,14 @@ FMT_FORMAT_AS(unsigned short, unsigned);
FMT_FORMAT_AS(long, detail::long_type);
FMT_FORMAT_AS(unsigned long, detail::ulong_type);
FMT_FORMAT_AS(Char*, const Char*);
FMT_FORMAT_AS(std::basic_string<Char>, basic_string_view<Char>);
FMT_FORMAT_AS(std::nullptr_t, const void*);
FMT_FORMAT_AS(detail::std_string_view<Char>, basic_string_view<Char>);
FMT_FORMAT_AS(void*, const void*);

template <typename Char, typename Traits, typename Allocator>
class formatter<std::basic_string<Char, Traits, Allocator>, Char>
dieram3 marked this conversation as resolved.
Show resolved Hide resolved
: public formatter<basic_string_view<Char>, Char> {};

template <typename Char, size_t N>
struct formatter<Char[N], Char> : formatter<basic_string_view<Char>, Char> {};

Expand Down
18 changes: 12 additions & 6 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <iterator> // std::back_inserter
#include <list> // std::list
#include <mutex> // std::mutex
#include <string> // std::string
#include <thread> // std::thread
#include <type_traits> // std::is_default_constructible

Expand Down Expand Up @@ -2222,16 +2223,21 @@ template <typename Char, typename... T> void check_enabled_formatters() {
}

TEST(format_test, test_formatters_enabled) {
using custom_string =
std::basic_string<char, std::char_traits<char>, mock_allocator<char>>;
using custom_wstring = std::basic_string<wchar_t, std::char_traits<wchar_t>,
mock_allocator<wchar_t>>;

check_enabled_formatters<char, bool, char, signed char, unsigned char, short,
unsigned short, int, unsigned, long, unsigned long,
long long, unsigned long long, float, double,
long double, void*, const void*, char*, const char*,
std::string, std::nullptr_t>();
check_enabled_formatters<wchar_t, bool, wchar_t, signed char, unsigned char,
short, unsigned short, int, unsigned, long,
unsigned long, long long, unsigned long long, float,
double, long double, void*, const void*, wchar_t*,
const wchar_t*, std::wstring, std::nullptr_t>();
std::string, custom_string, std::nullptr_t>();
dieram3 marked this conversation as resolved.
Show resolved Hide resolved
check_enabled_formatters<
wchar_t, bool, wchar_t, signed char, unsigned char, short, unsigned short,
int, unsigned, long, unsigned long, long long, unsigned long long, float,
double, long double, void*, const void*, wchar_t*, const wchar_t*,
std::wstring, custom_wstring, std::nullptr_t>();
}

TEST(format_int_test, data) {
Expand Down
10 changes: 10 additions & 0 deletions test/mock-allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ template <typename T> class mock_allocator {
using value_type = T;
using size_type = size_t;

using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using difference_type = ptrdiff_t;

template <typename U> struct rebind {
using other = mock_allocator<U>;
};

mock_allocator() {}
mock_allocator(const mock_allocator&) {}

Expand Down
Loading