| 1 |
// { dg-do run { target c++20 } } |
| 2 |
|
| 3 |
#include <format> |
| 4 |
#include <locale> |
| 5 |
#include <vector> |
| 6 |
#include <cstring> |
| 7 |
#include <testsuite_hooks.h> |
| 8 |
|
| 9 |
struct punct : std::numpunct<char> |
| 10 |
{ |
| 11 |
std::string do_grouping() const override { return "\2"; } |
| 12 |
}; |
| 13 |
|
| 14 |
void |
| 15 |
test() |
| 16 |
{ |
| 17 |
char buf[32] = { }; |
| 18 |
auto out = std::format_to(buf, "test"); |
| 19 |
VERIFY( out == buf+4 ); |
| 20 |
|
| 21 |
std::locale loc({}, new punct); |
| 22 |
auto out2 = std::format_to(buf, loc, "{:Ld}", 12345); |
| 23 |
VERIFY( out2 == buf+7 ); |
| 24 |
VERIFY( std::string_view(buf, out2 - buf) == "1,23,45" ); |
| 25 |
} |
| 26 |
|
| 27 |
struct wpunct : std::numpunct<wchar_t> |
| 28 |
{ |
| 29 |
std::string do_grouping() const override { return "\2"; } |
| 30 |
}; |
| 31 |
|
| 32 |
void |
| 33 |
test_wchar() |
| 34 |
{ |
| 35 |
wchar_t buf[32] = { }; |
| 36 |
auto out = std::format_to(buf, L"123 + 456 = {}", 579); |
| 37 |
VERIFY( out == buf+15 ); |
| 38 |
|
| 39 |
std::locale loc({}, new wpunct); |
| 40 |
auto out2 = std::format_to(buf, loc, L"{:Ld}", 12345); |
| 41 |
VERIFY( out2 == buf+7 ); |
| 42 |
VERIFY( std::wstring_view(buf, out2 - buf) == L"1,23,45" ); |
| 43 |
} |
| 44 |
|
| 45 |
template<typename I> |
| 46 |
struct move_only_iterator |
| 47 |
{ |
| 48 |
using iterator = I; |
| 49 |
using value_type = iterator::value_type; |
| 50 |
using difference_type = iterator::difference_type; |
| 51 |
using iterator_category = std::output_iterator_tag; |
| 52 |
|
| 53 |
move_only_iterator(iterator b) : base_(b) { } |
| 54 |
move_only_iterator(move_only_iterator&&) = default; |
| 55 |
move_only_iterator& operator=(move_only_iterator&&) = default; |
| 56 |
|
| 57 |
move_only_iterator& operator++() { ++base_; return *this; } |
| 58 |
move_only_iterator operator++(int) { auto tmp = *this; ++base_; return tmp; } |
| 59 |
|
| 60 |
decltype(auto) operator*() { return *base_; } |
| 61 |
|
| 62 |
private: |
| 63 |
iterator base_; |
| 64 |
}; |
| 65 |
|
| 66 |
void |
| 67 |
test_move_only() |
| 68 |
{ |
| 69 |
std::string str; |
| 70 |
move_only_iterator mo(std::back_inserter(str)); |
| 71 |
[[maybe_unused]] auto res |
| 72 |
= std::format_to(std::move(mo), "for{:.3} that{:c}", "matte", (int)'!'); |
| 73 |
VERIFY( str == "format that!" ); |
| 74 |
|
| 75 |
std::vector<wchar_t> vec; |
| 76 |
move_only_iterator wmo(std::back_inserter(vec)); |
| 77 |
[[maybe_unused]] auto wres |
| 78 |
= std::format_to(std::move(wmo), L"for{:.3} hat{:c}", L"matte", (long)L'!'); |
| 79 |
VERIFY( std::wstring_view(vec.data(), vec.size()) == L"format hat!" ); |
| 80 |
} |
| 81 |
|
| 82 |
void |
| 83 |
test_pr110917() |
| 84 |
{ |
| 85 |
// PR libstdc++/110917 |
| 86 |
// std::format_to(int*, ...) fails to compile because of _S_make_span |
| 87 |
unsigned char buf[7]; |
| 88 |
auto res = std::format_to(buf, "{} {}", "abc", 123); |
| 89 |
VERIFY( res == buf + 7 ); |
| 90 |
VERIFY( ! std::memcmp(buf, "abc 123", 7) ); |
| 91 |
} |
| 92 |
|
| 93 |
int main() |
| 94 |
{ |
| 95 |
test(); |
| 96 |
test_wchar(); |
| 97 |
test_move_only(); |
| 98 |
test_pr110917(); |
| 99 |
} |