CppTrail
Loading...
Searching...
No Matches
utf42.h
Go to the documentation of this file.
1
48#ifndef LIB_UTF_42
49#define LIB_UTF_42
50
51#include <type_traits>
52#include <cstddef>
53
54// Use std::basic_string_view if C++17 or custom basic_string_view otherwise
55#if __cplusplus >= 201703L
56#include <string_view>
57#else
58#include <string>
59#endif
60
61// C++ version requirements
62#if __cplusplus < 201103L
63#pragma error "C++ minimum version required is 11"
64#endif
65
78#if __cplusplus >= 202002L
79
80#define make_poly_enc(char_t, lit) utf42::visit_poly_enc<char_t>( \
81 utf42::poly_enc{ \
82 lit, \
83 L##lit, \
84 u8##lit, \
85 u##lit, \
86 U##lit, \
87 } \
88)
89
90#else
91
92#define make_poly_enc(char_t, lit) utf42::visit_poly_enc<char_t>( \
93 utf42::poly_enc{ \
94 lit, \
95 L##lit, \
96 u##lit, \
97 U##lit, \
98 } \
99)
100
101#endif
102
113#if __cplusplus >= 202002L
114
115#define cons_poly_enc(lit) utf42::poly_enc{ \
116 lit, \
117 L##lit, \
118 u8##lit, \
119 u##lit, \
120 U##lit, \
121}
122
123#else
124
125#define cons_poly_enc(lit) utf42::poly_enc{ \
126 lit, \
127 L##lit, \
128 u##lit, \
129 U##lit, \
130}
131
132#endif
133
138namespace utf42 {
152 template<typename T>
153 struct is_character : std::integral_constant<bool,
154 std::is_same<T, char>::value ||
155 std::is_same<T, wchar_t>::value ||
156#if __cplusplus >= 202002L
157 std::is_same<T, char8_t>::value ||
158#endif
159 std::is_same<T, char16_t>::value ||
160 std::is_same<T, char32_t>::value
161 > {
162 };
163
164#if __cplusplus >= 201703L
171 template<typename T>
173#endif
174
175#if __cplusplus >= 201703L
176
178 template<typename char_t>
179 using basic_string_view = std::basic_string_view<char_t>;
180
181#else
182
198 template<typename char_t>
199 class basic_string_view {
200 public:
201 using pointer = const char_t *;
202 using reference = const char_t &;
203 using char_type = char_t;
204 using size_type = std::size_t;
205
206 // Check char type
207 static_assert(utf42::is_character<char_t>::value, "type_t must be a character.");
208
210 constexpr basic_string_view() : m_pData(""), m_nSize(0) {
211 }
212
214 constexpr basic_string_view(nullptr_t) = delete;
215
224 template<std::size_t N>
225 constexpr basic_string_view(const char_t (&pStr)[N])
226 : m_pData(pStr), m_nSize(N - 1) {
227 static_assert(N > 0, "basic_string_view: invalid length");
228 }
229
235 constexpr size_type length() const noexcept { return m_nSize; }
236
242 constexpr pointer data() const noexcept { return m_pData; }
243
249 std::basic_string<char_t> str() const {
250 return std::basic_string<char_t>(m_pData, m_nSize);
251 }
252
253 private:
256 };
257
258#endif
259
260#if __cplusplus >= 202002L
261
268 template<typename T>
270
277 template<typename T>
278 concept IntegralType = std::is_integral_v<T>;
279
286 template<typename T>
287 concept FloatingPointType = std::is_floating_point_v<T>;
288#endif
289
299 struct poly_enc {
300 // String literal fields
303#if __cplusplus >= 202002L
305#endif
308
335
348#if __cplusplus >= 202002L
349 template<CharacterType char_t>
352#else
353 template<typename char_t>
356#endif
357 };
358
359 // Primary template for unsupported types
360#if __cplusplus >= 202002L
361 template<CharacterType char_t>
363 return {};
364 }
365#else
366 template<typename char_t>
368 static_assert(false, "Unsupported character type");
369 return {};
370 }
371#endif
372
374 template<>
375 constexpr basic_string_view<char> poly_enc::visit<char>() const noexcept {
376 return TXT_CHAR;
377 }
378
380 template<>
381 constexpr basic_string_view<wchar_t> poly_enc::visit<wchar_t>() const noexcept {
382 return TXT_CHAR_W;
383 }
384
385#if __cplusplus >= 202002L
387 template<>
388 constexpr basic_string_view<char8_t> poly_enc::visit<char8_t>() const noexcept {
389 return TXT_CHAR_8;
390 }
391#endif
392
394 template<>
395 constexpr basic_string_view<char16_t> poly_enc::visit<char16_t>() const noexcept {
396 return TXT_CHAR_16;
397 }
398
400 template<>
401 constexpr basic_string_view<char32_t> poly_enc::visit<char32_t>() const noexcept {
402 return TXT_CHAR_32;
403 }
404
417#if __cplusplus >= 202002L
418 template<CharacterType char_t>
419 consteval std::basic_string_view<char_t>
421 return oPolyEnv.visit<char_t>();
422 }
423#else
424 template<typename char_t>
426 visit_poly_enc(const poly_enc &oPolyEnv) {
427 return oPolyEnv.visit<char_t>();
428 }
429#endif
430} // namespace utf42
431
432// Clean up helper macro
433#undef LIB_UTF_42_CHAR_TYPE
434
435#endif //LIB_UTF_42
Concept constraining a type to a supported character type.
Definition utf42.h:269
Concept constraining a type to an floating point type.
Definition utf42.h:287
Concept constraining a type to an integral type.
Definition utf42.h:278
Main namespace of the library.
std::basic_string_view< char_t > basic_string_view
String view type for poly_enc.
Definition utf42.h:179
consteval std::basic_string_view< char_t > visit_poly_enc(const poly_enc &oPolyEnv)
Selects the appropriate encoded string view for a given character type.
Definition utf42.h:420
constexpr bool is_character_v
Convenience variable template for is_char.
Definition utf42.h:172
Type trait that checks whether a type is a supported character type.
Definition utf42.h:161
Container holding all character-encoded views of a string literal.
Definition utf42.h:299
constexpr basic_string_view< char_t > visit() const noexcept
Selects the appropriate encoded string view for a given character type.
Definition utf42.h:362
constexpr poly_enc(const basic_string_view< char > txt_char, const basic_string_view< wchar_t > txt_char_w, const basic_string_view< char8_t > txt_char_8, const basic_string_view< char16_t > txt_char_16, const basic_string_view< char32_t > txt_char_32) noexcept
Constructs a polymorphic encoding container.
Definition utf42.h:318
basic_string_view< char16_t > TXT_CHAR_16
UTF-16 character literal.
Definition utf42.h:306
basic_string_view< char8_t > TXT_CHAR_8
UTF-8 character literal. Only defined if C++20 is available.
Definition utf42.h:304
basic_string_view< char > TXT_CHAR
Narrow character literal.
Definition utf42.h:301
basic_string_view< char32_t > TXT_CHAR_32
UTF-32 character literal.
Definition utf42.h:307
basic_string_view< wchar_t > TXT_CHAR_W
Wide character literal.
Definition utf42.h:302