summaryrefslogtreecommitdiffhomepage
path: root/src/test-helper.cpp
blob: d591916203d14660ceb8c3e36b8602f902660592 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "test-helper.h"

#include <array>
#include <chrono>
#include <codecvt>
#include <deque>
#include <exception>
#include <iostream>
#include <iomanip>
#include <limits>
#include <list>
#include <locale>
#include <ostream>
#include <random>
#include <string>
#include <tuple>
#include <type_traits>
#include <vector>

// output operators must be in same namespace as the type itself
namespace std {

#ifdef __cpp_char8_t
 std::ostream& operator<<(std::ostream& os, std::basic_string<utf8_t> const& s)
 {
  os << "[";
  for (auto& c: s)
   os << " " << std::to_string(static_cast<uint8_t>(c));
  os << "]";

  return os;
 }
#endif

 std::ostream& operator<<(std::ostream& os, std::basic_string<char16_t> const& s)
 {
  os << "[";
  for (auto& c: s)
   os << " " << std::to_string(static_cast<uint16_t>(c));
  os << "]";

  return os;
 }

 std::ostream& operator<<(std::ostream& os, std::basic_string<char32_t> const& s)
 {
  os << "[";
  for (auto& c: s)
   os << " " << std::to_string(static_cast<uint32_t>(c));
  os << "]";

  return os;
 }

#ifdef __cpp_char8_t
 std::ostream& operator<<(std::ostream& os, utf8_t const& c)
 {
  os << "\\x" << std::setfill('0') << std::setw(2) << std::right << std::hex << static_cast<uint8_t>(c);

  return os;
 }
#endif

 std::ostream& operator<<(std::ostream& os, char16_t const& c)
 {
  os << "\\u" << std::setfill('0') << std::setw(4) << std::right << std::hex << static_cast<uint16_t>(c);

  return os;
 }

 std::ostream& operator<<(std::ostream& os, char32_t const& c)
 {
  os << "\\U" << std::setfill('0') << std::setw(8) << std::right << std::hex << static_cast<uint32_t>(c);

  return os;
 }

} // namespace std

namespace {

 // utility wrapper to adapt locale-bound facets for wstring/wbuffer convert
 template<class Facet>
 struct deletable_facet : Facet
 {
  template<class ...Args>
  deletable_facet(Args&& ...args) : Facet(std::forward<Args>(args)...) {}
  ~deletable_facet() {}
 };

 // char8_t instead of char doesn't work w/ clang++-13 + C++20 (yet?)
 std::wstring_convert<deletable_facet<std::codecvt<char16_t, char, std::mbstate_t>>, char16_t> conv16;
 std::wstring_convert<deletable_facet<std::codecvt<char32_t, char, std::mbstate_t>>, char32_t> conv32;

} // namespace

template<>
std::basic_string<utf8_t> std_convert<utf8_t, utf8_t>(const std::basic_string<utf8_t>& s)
{
 std::string a{s.begin(), s.end()};
 a = conv32.to_bytes(conv32.from_bytes(a));
 return std::basic_string<utf8_t>{a.begin(), a.end()};
}

template<>
std::basic_string<char16_t> std_convert<utf8_t, char16_t>(const std::basic_string<utf8_t>& s)
{
 std::string a{s.begin(), s.end()};
 return conv16.from_bytes(a);
}

template<>
std::basic_string<char32_t> std_convert<utf8_t, char32_t>(const std::basic_string<utf8_t>& s)
{
 std::string a{s.begin(), s.end()};
 return conv32.from_bytes(a);
}

template<>
std::basic_string<utf8_t> std_convert<char16_t, utf8_t>(const std::basic_string<char16_t>& s)
{
 auto result{conv16.to_bytes(s)};
 return std::basic_string<utf8_t>(result.begin(), result.end());
}

template<>
std::basic_string<char16_t> std_convert<char16_t, char16_t>(const std::basic_string<char16_t>& s)
{
 return conv16.from_bytes(conv16.to_bytes(s));
}

template<>
std::basic_string<char32_t> std_convert<char16_t, char32_t>(const std::basic_string<char16_t>& s)
{
 return conv32.from_bytes(conv16.to_bytes(s));
}

template<>
std::basic_string<utf8_t> std_convert<char32_t, utf8_t>(const std::basic_string<char32_t>& s)
{
 auto result{conv32.to_bytes(s)};
 return std::basic_string<utf8_t>(result.begin(), result.end());
}

template<>
std::basic_string<char16_t> std_convert<char32_t, char16_t>(const std::basic_string<char32_t>& s)
{
 return conv16.from_bytes(conv32.to_bytes(s));
}

template<>
std::basic_string<char32_t> std_convert<char32_t, char32_t>(const std::basic_string<char32_t>& s)
{
 return conv32.from_bytes(conv32.to_bytes(s));
}