* Encode a character to UTF-8. * @param c Character * @return Binary data and length. Length is zero, if "c" is out of range. */
| 19 | * @return Binary data and length. Length is zero, if "c" is out of range. |
| 20 | */ |
| 21 | [[nodiscard]] std::pair<char[4], size_t> EncodeUtf8(char32_t c) |
| 22 | { |
| 23 | std::pair<char[4], size_t> result{}; |
| 24 | auto &[buf, len] = result; |
| 25 | if (c < 0x80) { |
| 26 | buf[len++] = c; |
| 27 | } else if (c < 0x800) { |
| 28 | buf[len++] = 0xC0 + GB(c, 6, 5); |
| 29 | buf[len++] = 0x80 + GB(c, 0, 6); |
| 30 | } else if (c < 0x10000) { |
| 31 | buf[len++] = 0xE0 + GB(c, 12, 4); |
| 32 | buf[len++] = 0x80 + GB(c, 6, 6); |
| 33 | buf[len++] = 0x80 + GB(c, 0, 6); |
| 34 | } else if (c < 0x110000) { |
| 35 | buf[len++] = 0xF0 + GB(c, 18, 3); |
| 36 | buf[len++] = 0x80 + GB(c, 12, 6); |
| 37 | buf[len++] = 0x80 + GB(c, 6, 6); |
| 38 | buf[len++] = 0x80 + GB(c, 0, 6); |
| 39 | } |
| 40 | return result; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Decode a character from UTF-8. |
no test coverage detected