utf-42
Loading...
Searching...
No Matches
/home/runner/work/utf-42/utf-42/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>
172 constexpr bool is_character_v = is_character<T>::value;
173#endif
174
175#if __cplusplus >= 201703L
176
177 template<typename char_t>
178 using basic_string_view = std::basic_string_view<char_t>;
179
180#else
181
197 template<typename char_t>
199 public:
200 using pointer = const char_t *;
201 using reference = const char_t &;
203 using size_type = std::size_t;
204
205 // Check char type
206 static_assert(utf42::is_character<char_t>::value, "type_t must be a character.");
207
209 constexpr basic_string_view() : m_pData(""), m_nSize(0) {
210 }
211
213 constexpr basic_string_view(nullptr_t) = delete;
214
223 template<std::size_t N>
224 constexpr basic_string_view(const char_t (&pStr)[N])
225 : m_pData(pStr), m_nSize(N - 1) {
226 static_assert(N > 0, "basic_string_view: invalid length");
227 }
228
234 constexpr size_type length() const noexcept { return m_nSize; }
235
241 constexpr pointer data() const noexcept { return m_pData; }
242
248 std::basic_string<char_t> str() const {
249 return std::basic_string<char_t>(m_pData, m_nSize);
250 }
251
252 private:
253 pointer m_pData;
254 size_type m_nSize;
255 };
256
257#endif
258
259#if __cplusplus >= 202002L
260
267 template<typename T>
268 concept CharacterType = is_character_v<T>;
269
276 template<typename T>
277 concept IntegralType = std::is_integral_v<T>;
278
285 template<typename T>
286 concept FloatingPointType = std::is_floating_point_v<T>;
287#endif
288
298 struct poly_enc {
299 // String literal fields
302#if __cplusplus >= 202002L
304#endif
307
334
347#if __cplusplus >= 202002L
348 template<CharacterType char_t>
351#else
352 template<typename char_t>
355#endif
356 };
357
358 // Primary template for unsupported types
359#if __cplusplus >= 202002L
360 template<CharacterType char_t>
361 constexpr basic_string_view<char_t> poly_enc::visit() const noexcept {
362 return {};
363 }
364#else
365 template<typename char_t>
367 static_assert(false, "Unsupported character type");
368 return {};
369 }
370#endif
371
372 // Specialization for char
373 template<>
374 constexpr basic_string_view<char> poly_enc::visit<char>() const noexcept {
375 return TXT_CHAR;
376 }
377
378 // Specialization for wchar_t
379 template<>
380 constexpr basic_string_view<wchar_t> poly_enc::visit<wchar_t>() const noexcept {
381 return TXT_CHAR_W;
382 }
383
384#if __cplusplus >= 202002L
385 // Specialization for char8_t
386 template<>
387 constexpr basic_string_view<char8_t> poly_enc::visit<char8_t>() const noexcept {
388 return TXT_CHAR_8;
389 }
390#endif
391
392 // Specialization for char16_t
393 template<>
394 constexpr basic_string_view<char16_t> poly_enc::visit<char16_t>() const noexcept {
395 return TXT_CHAR_16;
396 }
397
398 // Specialization for char32_t
399 template<>
400 constexpr basic_string_view<char32_t> poly_enc::visit<char32_t>() const noexcept {
401 return TXT_CHAR_32;
402 }
403
416#if __cplusplus >= 202002L
417 template<CharacterType char_t>
418 consteval std::basic_string_view<char_t>
419 visit_poly_enc(const poly_enc &oPolyEnv) {
420 return oPolyEnv.visit<char_t>();
421 }
422#else
423 template<typename char_t>
424 constexpr basic_string_view<char_t>
426 return oPolyEnv.visit<char_t>();
427 }
428#endif
429} // namespace utf42
430
431// Clean up helper macro
432#undef LIB_UTF_42_CHAR_TYPE
433
434#endif //LIB_UTF_42
A class that provides a lightweight, non-owning view of a string.
Definition utf42.h:198
constexpr pointer data() const noexcept
Get a pointer to the underlying character data.
Definition utf42.h:241
constexpr basic_string_view()
Default constructor initializes to an empty string view.
Definition utf42.h:209
constexpr basic_string_view(nullptr_t)=delete
Deleted constructor from nullptr to avoid unintended usage.
constexpr basic_string_view(const char_t(&pStr)[N])
Constructor from a C-style string.
Definition utf42.h:224
const char_t * pointer
Pointer type to characters.
Definition utf42.h:200
constexpr size_type length() const noexcept
Get the length of the string.
Definition utf42.h:234
std::size_t size_type
Type for sizes of the string.
Definition utf42.h:203
std::basic_string< char_t > str() const
Convert to std::basic_string for further manipulation.
Definition utf42.h:248
Main namespace of the library.
constexpr 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:425
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:298
constexpr basic_string_view< char_t > visit() const noexcept
Selects the appropriate encoded string view for a given character type.
Definition utf42.h:366
basic_string_view< char16_t > TXT_CHAR_16
UTF-16 character literal.
Definition utf42.h:305
constexpr poly_enc(const basic_string_view< char > txt_char, const basic_string_view< wchar_t > txt_char_w, 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:317
basic_string_view< char > TXT_CHAR
Narrow character literal.
Definition utf42.h:300
basic_string_view< char32_t > TXT_CHAR_32
UTF-32 character literal.
Definition utf42.h:306
basic_string_view< wchar_t > TXT_CHAR_W
Wide character literal.
Definition utf42.h:301