HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
UTF.hpp
1// Copyright Take Vos 2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "../required.hpp"
8#include "../cast.hpp"
9#include <string>
10#include <string_view>
11
12namespace tt {
13
21template<typename It>
22[[nodiscard]] constexpr std::endian guess_utf16_endianess(It first, It last, std::endian default_guess)
23{
24 static_assert(sizeof(*first) == 1, "Expecting an array of 8-bit characters");
25 ttlet num_words = narrow_cast<size_t>(std::distance(first, last) / 2);
26
27 if (not num_words) {
28 return default_guess;
29 }
30
31 // Check for BOM.
32 {
33 ttlet c0 = static_cast<uint8_t>(*first);
34 ttlet c1 = static_cast<uint8_t>(*(first + 1));
35 if (c0 == 0xfe && c1 == 0xff) {
36 return std::endian::big;
37 } else if (c1 == 0xfe and c0 == 0xff) {
38 return std::endian::little;
39 }
40 }
41
42 // Count the nul bytes in high or low byte of the UTF16 string.
43 size_t count0 = 0;
44 size_t count1 = 0;
45 auto it = first;
46 for (auto i = 0; i != num_words; ++i) {
47 ttlet c0 = static_cast<uint8_t>(*(it++));
48 ttlet c1 = static_cast<uint8_t>(*(it++));
49
50 if (c0 == 0 and c0 != c1) {
51 ++count0;
52 } else if (c1 == 0 and c0 != c1) {
53 ++count1;
54 }
55 }
56
57 // Check for at least 1/8 ASCII characters.
58 if (count0 == count1) {
59 return default_guess;
60 } else if (count0 > count1 and count0 > (num_words / 8)) {
61 return std::endian::little;
62 } else if (count1 > count0 and count1 > (num_words / 8)) {
63 return std::endian::big;
64 } else {
65 return default_guess;
66 }
67}
68
82[[nodiscard]] std::u32string utf8_to_utf32(std::string_view rhs) noexcept;
83
97[[nodiscard]] std::u16string utf8_to_utf16(std::string_view rhs) noexcept;
98
112[[nodiscard]] std::string utf8_to_utf8(std::string_view rhs) noexcept;
113
127[[nodiscard]] std::wstring utf8_to_wide(std::string_view rhs) noexcept;
128
139[[nodiscard]] std::u32string utf16_to_utf32(std::u16string_view rhs) noexcept;
140
151[[nodiscard]] std::u16string utf16_to_utf16(std::u16string_view rhs) noexcept;
152
163[[nodiscard]] std::string utf16_to_utf8(std::u16string_view rhs) noexcept;
164
175[[nodiscard]] std::wstring utf16_to_wide(std::u16string_view rhs) noexcept;
176
187[[nodiscard]] std::u32string utf32_to_utf32(std::u32string_view rhs) noexcept;
188
199[[nodiscard]] std::u16string utf32_to_utf16(std::u32string_view rhs) noexcept;
200
211[[nodiscard]] std::string utf32_to_utf8(std::u32string_view rhs) noexcept;
212
223[[nodiscard]] std::wstring utf32_to_wide(std::u32string_view rhs) noexcept;
224
233[[nodiscard]] std::u32string wide_to_utf32(std::wstring_view rhs) noexcept;
234
243[[nodiscard]] std::u16string wide_to_utf16(std::wstring_view rhs) noexcept;
244
253[[nodiscard]] std::string wide_to_utf8(std::wstring_view rhs) noexcept;
254
263[[nodiscard]] std::wstring wide_to_wide(std::wstring_view rhs) noexcept;
264
265[[nodiscard]] std::string to_string(std::u16string_view rhs) noexcept;
266
267[[nodiscard]] std::string to_string(std::u32string_view rhs) noexcept;
268
269[[nodiscard]] std::string to_string(std::wstring_view rhs) noexcept;
270
271[[nodiscard]] std::u16string to_u16string(std::string_view rhs) noexcept;
272
273[[nodiscard]] std::u16string to_u16string(std::wstring_view rhs) noexcept;
274
275[[nodiscard]] std::u16string to_u16string(std::u32string_view rhs) noexcept;
276
277[[nodiscard]] std::u32string to_u32string(std::string_view rhs) noexcept;
278
279[[nodiscard]] std::u32string to_u32string(std::u16string_view rhs) noexcept;
280
281[[nodiscard]] std::u32string to_u32string(std::wstring_view rhs) noexcept;
282
283[[nodiscard]] std::wstring to_wstring(std::string_view rhs) noexcept;
284
285[[nodiscard]] std::wstring to_wstring(std::u16string_view rhs) noexcept;
286
287[[nodiscard]] std::wstring to_wstring(std::u32string_view rhs) noexcept;
288
289} // namespace tt
T distance(T... args)
T to_string(T... args)
T to_wstring(T... args)