| 1 |
// { dg-do run { target c++23 } } |
| 2 |
|
| 3 |
#include <format> |
| 4 |
#include <span> |
| 5 |
#include <testsuite_hooks.h> |
| 6 |
#include <testsuite_iterators.h> |
| 7 |
#include <vector> |
| 8 |
|
| 9 |
template<typename... Args> |
| 10 |
bool |
| 11 |
is_format_string_for(const char* str, Args&&... args) |
| 12 |
{ |
| 13 |
try { |
| 14 |
(void) std::vformat(str, std::make_format_args(args...)); |
| 15 |
return true; |
| 16 |
} catch (const std::format_error&) { |
| 17 |
return false; |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
template<typename... Args> |
| 22 |
bool |
| 23 |
is_format_string_for(const wchar_t* str, Args&&... args) |
| 24 |
{ |
| 25 |
try { |
| 26 |
(void) std::vformat(str, std::make_wformat_args(args...)); |
| 27 |
return true; |
| 28 |
} catch (const std::format_error&) { |
| 29 |
return false; |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
template<typename Rg, typename CharT> |
| 34 |
bool is_range_formatter_spec_for(CharT const* spec, Rg&& rg) |
| 35 |
{ |
| 36 |
using V = std::remove_cvref_t<std::ranges::range_reference_t<Rg>>; |
| 37 |
std::range_formatter<V, CharT> fmt; |
| 38 |
std::basic_format_parse_context<CharT> pc(spec); |
| 39 |
try { |
| 40 |
(void)fmt.parse(pc); |
| 41 |
return true; |
| 42 |
} catch (const std::format_error&) { |
| 43 |
return false; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
#define WIDEN_(C, S) ::std::__format::_Widen<C>(S, L##S) |
| 48 |
#define WIDEN(S) WIDEN_(_CharT, S) |
| 49 |
|
| 50 |
void |
| 51 |
test_format_string() |
| 52 |
{ |
| 53 |
// only CharT value types are supported |
| 54 |
VERIFY( !is_range_formatter_spec_for(L"s", std::vector<char>()) ); |
| 55 |
VERIFY( !is_format_string_for(L"{:s}", std::vector<char>()) ); |
| 56 |
VERIFY( !is_range_formatter_spec_for(L"s", std::vector<char>()) ); |
| 57 |
VERIFY( !is_format_string_for(L"{:s}", std::vector<char>()) ); |
| 58 |
VERIFY( !is_range_formatter_spec_for("s", std::vector<int>()) ); |
| 59 |
VERIFY( !is_format_string_for("{:s}", std::vector<int>()) ); |
| 60 |
|
| 61 |
// invalid format stringss |
| 62 |
VERIFY( !is_range_formatter_spec_for("?", std::vector<char>()) ); |
| 63 |
VERIFY( !is_format_string_for("{:?}", std::vector<char>()) ); |
| 64 |
VERIFY( !is_range_formatter_spec_for("ns", std::vector<char>()) ); |
| 65 |
VERIFY( !is_format_string_for("{:ns}", std::vector<char>()) ); |
| 66 |
VERIFY( !is_range_formatter_spec_for("s:", std::vector<char>()) ); |
| 67 |
VERIFY( !is_format_string_for("{:s:}", std::vector<char>()) ); |
| 68 |
|
| 69 |
// precision is not supported, even for s |
| 70 |
VERIFY( !is_range_formatter_spec_for(".10s", std::vector<char>()) ); |
| 71 |
VERIFY( !is_format_string_for("{:.10s}", std::vector<char>()) ); |
| 72 |
VERIFY( !is_format_string_for("{:.{}s}", std::vector<char>(), 10) ); |
| 73 |
|
| 74 |
// width needs to be integer type |
| 75 |
VERIFY( !is_format_string_for("{:{}s}", std::vector<char>(), 1.0f) ); |
| 76 |
} |
| 77 |
|
| 78 |
template<typename Range> |
| 79 |
void test_output() |
| 80 |
{ |
| 81 |
using _CharT = std::ranges::range_value_t<Range>; |
| 82 |
auto makeRange = [](std::basic_string<_CharT>& s) { |
| 83 |
return Range(s.data(), s.data() + s.size()); |
| 84 |
}; |
| 85 |
std::basic_string<_CharT> res; |
| 86 |
size_t size = 0; |
| 87 |
|
| 88 |
std::basic_string<_CharT> s1 = WIDEN("abcd"); |
| 89 |
res = std::format(WIDEN("{}"), makeRange(s1)); |
| 90 |
VERIFY( res == WIDEN("['a', 'b', 'c', 'd']") ); |
| 91 |
|
| 92 |
res = std::format(WIDEN("{::}"), makeRange(s1)); |
| 93 |
VERIFY( res == WIDEN("[a, b, c, d]") ); |
| 94 |
|
| 95 |
res = std::format(WIDEN("{:s}"), makeRange(s1)); |
| 96 |
VERIFY( res == WIDEN("abcd") ); |
| 97 |
|
| 98 |
res = std::format(WIDEN("{:?s}"), makeRange(s1)); |
| 99 |
VERIFY( res == WIDEN(R"("abcd")") ); |
| 100 |
|
| 101 |
res = std::format(WIDEN("{:3s}"), makeRange(s1)); |
| 102 |
VERIFY( res == WIDEN("abcd") ); |
| 103 |
|
| 104 |
res = std::format(WIDEN("{:7s}"), makeRange(s1)); |
| 105 |
VERIFY( res == WIDEN("abcd ") ); |
| 106 |
|
| 107 |
res = std::format(WIDEN("{:{}s}"), makeRange(s1), 7); |
| 108 |
VERIFY( res == WIDEN("abcd ") ); |
| 109 |
|
| 110 |
res = std::format(WIDEN("{1:{0}s}"), 7, makeRange(s1)); |
| 111 |
VERIFY( res == WIDEN("abcd ") ); |
| 112 |
|
| 113 |
res = std::format(WIDEN("{:*>6s}"), makeRange(s1)); |
| 114 |
VERIFY( res == WIDEN("**abcd") ); |
| 115 |
|
| 116 |
res = std::format(WIDEN("{:-<5s}"), makeRange(s1)); |
| 117 |
VERIFY( res == WIDEN("abcd-") ); |
| 118 |
|
| 119 |
res = std::format(WIDEN("{:=^8s}"), makeRange(s1)); |
| 120 |
VERIFY( res == WIDEN("==abcd==") ); |
| 121 |
|
| 122 |
std::basic_string<_CharT> s2(512, static_cast<_CharT>('a')); |
| 123 |
res = std::format(WIDEN("{:=^8s}"), makeRange(s2)); |
| 124 |
VERIFY( res == s2 ); |
| 125 |
|
| 126 |
size = std::formatted_size(WIDEN("{:s}"), makeRange(s1)); |
| 127 |
VERIFY( size == 4 ); |
| 128 |
|
| 129 |
size = std::formatted_size(WIDEN("{:3s}"), makeRange(s1)); |
| 130 |
VERIFY( size == 4 ); |
| 131 |
|
| 132 |
size = std::formatted_size(WIDEN("{:7s}"), makeRange(s1)); |
| 133 |
VERIFY( size == 7 ); |
| 134 |
|
| 135 |
size = std::formatted_size(WIDEN("{:s}"), makeRange(s2)); |
| 136 |
VERIFY( size == 512 ); |
| 137 |
} |
| 138 |
|
| 139 |
template<typename CharT> |
| 140 |
struct cstr_view |
| 141 |
{ |
| 142 |
cstr_view() = default; |
| 143 |
explicit cstr_view(CharT* f, CharT* l) |
| 144 |
: ptr(f) |
| 145 |
{ VERIFY(!*l); } |
| 146 |
|
| 147 |
struct sentinel |
| 148 |
{ |
| 149 |
friend constexpr |
| 150 |
bool operator==(CharT const* ptr, sentinel) noexcept |
| 151 |
{ return !*ptr; } |
| 152 |
}; |
| 153 |
|
| 154 |
constexpr |
| 155 |
CharT* begin() const noexcept |
| 156 |
{ return ptr; }; |
| 157 |
static constexpr |
| 158 |
sentinel end() noexcept |
| 159 |
{ return {}; } |
| 160 |
|
| 161 |
private: |
| 162 |
CharT* ptr = ""; |
| 163 |
}; |
| 164 |
|
| 165 |
template<typename CharT> |
| 166 |
void |
| 167 |
test_outputs() |
| 168 |
{ |
| 169 |
using namespace __gnu_test; |
| 170 |
test_output<std::vector<CharT>>(); |
| 171 |
test_output<std::span<CharT>>(); |
| 172 |
test_output<cstr_view<CharT>>(); |
| 173 |
|
| 174 |
test_output<test_forward_range<CharT>>(); |
| 175 |
test_output<test_forward_sized_range<CharT>>(); |
| 176 |
|
| 177 |
test_output<test_input_range<CharT>>(); |
| 178 |
test_output<test_input_sized_range<CharT>>(); |
| 179 |
|
| 180 |
test_output<test_range_nocopy<CharT, input_iterator_wrapper_nocopy>>(); |
| 181 |
test_output<test_sized_range<CharT, input_iterator_wrapper_nocopy>>(); |
| 182 |
|
| 183 |
test_output<std::span<const CharT>>(); |
| 184 |
test_output<cstr_view<const CharT>>(); |
| 185 |
test_output<test_forward_range<const CharT>>(); |
| 186 |
|
| 187 |
static_assert(!std::formattable<std::span<volatile CharT>, CharT>); |
| 188 |
static_assert(!std::formattable<std::span<const volatile CharT>, CharT>); |
| 189 |
} |
| 190 |
|
| 191 |
void |
| 192 |
test_nested() |
| 193 |
{ |
| 194 |
std::string_view s1 = "str1"; |
| 195 |
std::string_view s2 = "str2"; |
| 196 |
|
| 197 |
std::vector<std::string> vs; |
| 198 |
vs.emplace_back(s1); |
| 199 |
vs.emplace_back(s2); |
| 200 |
|
| 201 |
VERIFY( std::format("{}", vs) == R"(["str1", "str2"])" ); |
| 202 |
VERIFY( std::format("{:}", vs) == R"(["str1", "str2"])" ); |
| 203 |
VERIFY( std::format("{::?}", vs) == R"(["str1", "str2"])" ); |
| 204 |
VERIFY( std::format("{::}", vs) == R"([str1, str2])" ); |
| 205 |
|
| 206 |
std::vector<std::vector<char>> vv; |
| 207 |
vv.emplace_back(s1.begin(), s1.end()); |
| 208 |
vv.emplace_back(s2.begin(), s2.end()); |
| 209 |
std::string_view escaped = R"([['s', 't', 'r', '1'], ['s', 't', 'r', '2']])"; |
| 210 |
|
| 211 |
VERIFY( std::format("{}", vv) == escaped ); |
| 212 |
VERIFY( std::format("{:}", vv) == escaped ); |
| 213 |
VERIFY( std::format("{::}", vv) == escaped ); |
| 214 |
VERIFY( std::format("{:::?}", vv) == escaped ); |
| 215 |
VERIFY( std::format("{:::}", vv) == R"([[s, t, r, 1], [s, t, r, 2]])" ); |
| 216 |
VERIFY( std::format("{::s}", vv) == R"([str1, str2])" ); |
| 217 |
VERIFY( std::format("{::?s}", vv) == R"(["str1", "str2"])" ); |
| 218 |
} |
| 219 |
|
| 220 |
int main() |
| 221 |
{ |
| 222 |
test_format_string(); |
| 223 |
test_outputs<char>(); |
| 224 |
test_outputs<wchar_t>(); |
| 225 |
test_nested(); |
| 226 |
} |