15namespace hi::inline v1 {
18template<
typename CharT>
21template<
typename CharT>
22constexpr void append_code_point(
std::basic_string<CharT> &r, uint32_t code_point)
noexcept requires(
sizeof(CharT) == 1)
24 if (code_point < 0x80) {
25 r +=
static_cast<CharT
>(code_point);
26 }
else if (code_point < 0x800) {
27 r +=
static_cast<CharT
>((code_point >> 6) | 0xc0);
28 r +=
static_cast<CharT
>((code_point & 0x3f) | 0x80);
29 }
else if (code_point < 0xd800) {
30 r +=
static_cast<CharT
>((code_point >> 12) | 0xe0);
31 r +=
static_cast<CharT
>(((code_point >> 6) & 0x3f) | 0x80);
32 r +=
static_cast<CharT
>((code_point & 0x3f) | 0x80);
33 }
else if (code_point < 0xe000) {
34 append_code_point(r, 0xfffd);
35 }
else if (code_point < 0x1'0000) {
36 r +=
static_cast<CharT
>((code_point >> 12) | 0xe0);
37 r +=
static_cast<CharT
>(((code_point >> 6) & 0x3f) | 0x80);
38 r +=
static_cast<CharT
>((code_point & 0x3f) | 0x80);
39 }
else if (code_point < 0x11'0000) {
40 r +=
static_cast<CharT
>((code_point >> 18) | 0xf0);
41 r +=
static_cast<CharT
>(((code_point >> 12) & 0x3f) | 0x80);
42 r +=
static_cast<CharT
>(((code_point >> 6) & 0x3f) | 0x80);
43 r +=
static_cast<CharT
>((code_point & 0x3f) | 0x80);
45 append_code_point(r, 0xfffd);
49template<
typename CharT>
50constexpr void append_code_point(
std::basic_string<CharT> &r, uint32_t code_point)
noexcept requires(
sizeof(CharT) == 2)
52 if (code_point < 0xd800) {
53 r +=
static_cast<CharT
>(code_point);
54 }
else if (code_point < 0xe000) {
55 append_code_point(r, 0xfffd);
56 }
else if (code_point < 0x1'0000) {
57 r +=
static_cast<CharT
>(code_point);
58 }
else if (code_point < 0x11'0000) {
59 code_point -= 0x1'0000;
60 r +=
static_cast<CharT
>(0xd800 + (code_point >> 10));
61 r +=
static_cast<CharT
>(0xdc00 + (code_point & 0x3ff));
63 append_code_point(r, 0xfffd);
67template<
typename CharT>
68constexpr void append_code_point(
std::basic_string<CharT> &r, uint32_t code_point)
noexcept requires(
sizeof(CharT) == 4)
70 if (code_point < 0xd800) {
71 r +=
static_cast<char32_t>(code_point);
72 }
else if (code_point < 0xe000) {
73 append_code_point(r, 0xfffd);
74 }
else if (code_point < 0x11'0000) {
75 r +=
static_cast<char32_t>(code_point);
77 append_code_point(r, 0xfffd);
81template<
typename ToCharT,
typename FromCharT>
82[[nodiscard]]
constexpr std::size_t guess_num_code_units(std::basic_string_view<FromCharT>
const &rhs)
noexcept
87template<
typename ToCharT,
typename FromCharT>
88[[nodiscard]]
constexpr std::size_t guess_num_code_units(std::basic_string_view<FromCharT>
const &rhs)
noexcept
89 requires(
sizeof(FromCharT) == 1 and
sizeof(ToCharT) > 1)
93 if ((c & 0xc0) != 0x80) {
100template<
typename ToCharT,
typename FromCharT>
101[[nodiscard]]
constexpr std::size_t guess_num_code_units(std::basic_string_view<FromCharT>
const &rhs)
noexcept
102 requires(
sizeof(FromCharT) > 1 and
sizeof(ToCharT) == 1)
105 for (
hilet c : rhs) {
108 }
else if (c < 0x800) {
110 }
else if (c < 0x1'0000) {
119template<
typename ToCharT,
typename FromCharT>
122 static_assert(
sizeof(FromCharT) == 1,
"Expect UTF-8 string to be in a 8-bit char type.");
125 r.reserve(guess_num_code_units<ToCharT>(rhs));
127 auto it =
begin(rhs);
130 uint32_t code_point = 0;
134 hilet c =
static_cast<uint8_t
>(*it);
143 }
else if (c < 0xc0) {
149 }
else if (c < 0xe0) {
151 code_point = c & 0x1f;
153 }
else if (c < 0xf0) {
155 code_point = c & 0x0f;
157 }
else if (c < 0xf8) {
159 code_point = c & 0x07;
168 }
else if ((c & 0xc0) == 0x80) {
171 code_point |= c & 0x3f;
181 if ((num == 2 and code_point < 0x80) or (num == 3 and code_point < 0x800) or (num == 4 and code_point < 0x1'0000)) {
186 append_code_point(r, code_point);
191 append_code_point(r, 0xfffd);
197template<
typename ToCharT,
typename FromCharT>
200 static_assert(
sizeof(FromCharT) == 2,
"Expect UTF-16 string to be in a 16-bit char type.");
203 r.reserve(guess_num_code_units<ToCharT>(rhs));
205 auto it =
begin(rhs);
208 uint32_t code_point = 0;
219 }
else if (c < 0xdc00) {
221 code_point = (c - 0xd800) << 10;
223 }
else if (c < 0xe000) {
233 }
else if (c >= 0xdc00 and c < 0xe000) {
235 code_point |= (c - 0xdc00);
236 code_point += 0x1'0000;
245 append_code_point(r, code_point);
250 append_code_point(r, 0xfffd);
256template<
typename ToCharT,
typename FromCharT>
259 static_assert(
sizeof(FromCharT) == 4,
"Expect UTF-32 string to be in a 32-bit char type.");
262 r.reserve(guess_num_code_units<ToCharT>(rhs));
264 for (
hilet c : rhs) {
265 append_code_point(r,
static_cast<uint32_t
>(c));
281[[nodiscard]]
constexpr std::endian guess_utf16_endianess(It first, It last, std::endian default_guess)
283 static_assert(
sizeof(*first) == 1,
"Expecting an array of 8-bit characters");
287 return default_guess;
292 hilet c0 =
static_cast<uint8_t
>(*first);
293 hilet c1 =
static_cast<uint8_t
>(*(first + 1));
294 if (c0 == 0xfe && c1 == 0xff) {
295 return std::endian::big;
296 }
else if (c1 == 0xfe and c0 == 0xff) {
297 return std::endian::little;
305 for (
auto i = 0; i != num_words; ++i) {
306 hilet c0 =
static_cast<uint8_t
>(*(it++));
307 hilet c1 =
static_cast<uint8_t
>(*(it++));
309 if (c0 == 0 and c0 != c1) {
311 }
else if (c1 == 0 and c0 != c1) {
317 if (count0 == count1) {
318 return default_guess;
319 }
else if (count0 > count1 and count0 > (num_words / 8)) {
320 return std::endian::little;
321 }
else if (count1 > count0 and count1 > (num_words / 8)) {
322 return std::endian::big;
324 return default_guess;
328template<
typename FromChar>
331 hi_static_not_implemented();
334template<std::same_as<
char16_t> FromChar>
337 return detail::from_utf16<char>(rhs);
340template<std::same_as<
char32_t> FromChar>
343 return detail::from_utf32<char>(rhs);
346template<std::same_as<
wchar_t> FromChar>
348 requires(
sizeof(FromChar) ==
sizeof(
char16_t))
350 return detail::from_utf16<char>(rhs);
353template<std::same_as<
wchar_t> FromChar>
355 requires(
sizeof(FromChar) ==
sizeof(
char32_t))
357 return detail::from_utf32<char>(rhs);
360template<
typename FromChar>
361[[nodiscard]]
constexpr std::u16string to_u16string(std::basic_string_view<FromChar> rhs)
noexcept
363 hi_static_not_implemented();
366template<std::same_as<
char> FromChar>
367[[nodiscard]]
constexpr std::u16string to_u16string(std::basic_string_view<FromChar> rhs)
noexcept
369 return detail::from_utf8<char16_t>(rhs);
372template<std::same_as<
char32_t> FromChar>
373[[nodiscard]]
constexpr std::u16string to_u16string(std::basic_string_view<FromChar> rhs)
noexcept
375 return detail::from_utf32<char16_t>(rhs);
378template<std::same_as<
wchar_t> FromChar>
379[[nodiscard]]
constexpr std::u16string to_u16string(std::basic_string_view<FromChar> rhs)
noexcept
380 requires(
sizeof(FromChar) ==
sizeof(
char16_t))
382 return detail::from_utf16<char16_t>(rhs);
385template<std::same_as<
wchar_t> FromChar>
386[[nodiscard]]
constexpr std::u16string to_u16string(std::basic_string_view<FromChar> rhs)
noexcept
387 requires(
sizeof(FromChar) ==
sizeof(
char32_t))
389 return detail::from_utf32<char16_t>(rhs);
392template<
typename FromChar>
393[[nodiscard]]
constexpr std::u32string to_u32string(std::basic_string_view<FromChar> rhs)
noexcept
395 hi_static_not_implemented();
398template<std::same_as<
char> FromChar>
399[[nodiscard]]
constexpr std::u32string to_u32string(std::basic_string_view<FromChar> rhs)
noexcept
401 return detail::from_utf8<char32_t>(rhs);
404template<std::same_as<
char16_t> FromChar>
405[[nodiscard]]
constexpr std::u32string to_u32string(std::basic_string_view<FromChar> rhs)
noexcept
407 return detail::from_utf16<char32_t>(rhs);
410template<std::same_as<
wchar_t> FromChar>
411[[nodiscard]]
constexpr std::u32string to_u32string(std::basic_string_view<FromChar> rhs)
noexcept
412 requires(
sizeof(FromChar) ==
sizeof(
char16_t))
414 return detail::from_utf16<char32_t>(rhs);
417template<std::same_as<
wchar_t> FromChar>
418[[nodiscard]]
constexpr std::u32string to_u32string(std::basic_string_view<FromChar> rhs)
noexcept
419 requires(
sizeof(FromChar) ==
sizeof(
char32_t))
421 return detail::from_utf32<char32_t>(rhs);
424template<
typename FromChar>
427 hi_static_not_implemented();
430template<std::same_as<
char> FromChar>
433 return detail::from_utf8<wchar_t>(rhs);
436template<std::same_as<
char16_t> FromChar>
439 return detail::from_utf16<wchar_t>(rhs);
442template<std::same_as<
char32_t> FromChar>
445 return detail::from_utf32<wchar_t>(rhs);
448template<
typename FromChar>
451 return to_string(std::basic_string_view<FromChar>(rhs));
454template<
typename FromChar>
457 return to_u16string(std::basic_string_view<FromChar>(rhs));
460template<
typename FromChar>
463 return to_u32string(std::basic_string_view<FromChar>(rhs));
466template<
typename FromChar>
469 return to_wstring(std::basic_string_view<FromChar>(rhs));
472template<
typename FromChar>
475 return to_string(std::basic_string_view<FromChar>(rhs));
478template<
typename FromChar>
479[[nodiscard]]
std::u16string to_u16string(FromChar
const *rhs)
noexcept
481 return to_u16string(std::basic_string_view<FromChar>(rhs));
484template<
typename FromChar>
485[[nodiscard]]
std::u32string to_u32string(FromChar
const *rhs)
noexcept
487 return to_u32string(std::basic_string_view<FromChar>(rhs));
490template<
typename FromChar>
493 return to_wstring(std::basic_string_view<FromChar>(rhs));
This file includes required definitions.
#define hilet
Invariant should be the default for variables.
Definition required.hpp:23