@brief Converts a UTF-32 encoded character to UTF-8 encoded bytes. @param[in] utf32 A UTF-32 encoded character. @param[out] utf8 UTF-8 encoded bytes. @param[in] encoded_size The size of UTF-encoded bytes.
| 2046 | /// @param[out] utf8 UTF-8 encoded bytes. |
| 2047 | /// @param[in] encoded_size The size of UTF-encoded bytes. |
| 2048 | inline void from_utf32(const char32_t utf32, std::array<uint8_t, 4>& utf8, uint32_t& encoded_size) { |
| 2049 | if (utf32 < 0x80u) { |
| 2050 | utf8[0] = static_cast<uint8_t>(utf32 & 0x007F); |
| 2051 | encoded_size = 1; |
| 2052 | } |
| 2053 | else if (utf32 <= 0x7FFu) { |
| 2054 | const auto utf8_chunk = static_cast<uint16_t>(0xC080u | ((utf32 & 0x07C0u) << 2) | (utf32 & 0x3Fu)); |
| 2055 | utf8[0] = static_cast<uint8_t>(utf8_chunk >> 8); |
| 2056 | utf8[1] = static_cast<uint8_t>(utf8_chunk); |
| 2057 | encoded_size = 2; |
| 2058 | } |
| 2059 | else if (utf32 <= 0xFFFFu) { |
| 2060 | const auto utf8_chunk = |
| 2061 | static_cast<uint32_t>(0xE08080u | ((utf32 & 0xF000u) << 4) | ((utf32 & 0x0FC0u) << 2) | (utf32 & 0x3F)); |
| 2062 | utf8[0] = static_cast<uint8_t>(utf8_chunk >> 16); |
| 2063 | utf8[1] = static_cast<uint8_t>(utf8_chunk >> 8); |
| 2064 | utf8[2] = static_cast<uint8_t>(utf8_chunk); |
| 2065 | encoded_size = 3; |
| 2066 | } |
| 2067 | else if (utf32 <= 0x10FFFFu) { |
| 2068 | const auto utf8_chunk = static_cast<uint32_t>( |
| 2069 | 0xF0808080u | ((utf32 & 0x1C0000u) << 6) | ((utf32 & 0x03F000u) << 4) | ((utf32 & 0x0FC0u) << 2) | |
| 2070 | (utf32 & 0x3Fu)); |
| 2071 | utf8[0] = static_cast<uint8_t>(utf8_chunk >> 24); |
| 2072 | utf8[1] = static_cast<uint8_t>(utf8_chunk >> 16); |
| 2073 | utf8[2] = static_cast<uint8_t>(utf8_chunk >> 8); |
| 2074 | utf8[3] = static_cast<uint8_t>(utf8_chunk); |
| 2075 | encoded_size = 4; |
| 2076 | } |
| 2077 | else { |
| 2078 | throw invalid_encoding("Invalid UTF-32 encoding detected.", utf32); |
| 2079 | } |
| 2080 | } |
| 2081 | |
| 2082 | } // namespace utf8 |
| 2083 |
no test coverage detected