MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / EncodeUtf8

Function EncodeUtf8

src/core/utf8.cpp:21–41  ·  view source on GitHub ↗

* Encode a character to UTF-8. * @param c Character * @return Binary data and length. Length is zero, if "c" is out of range. */

Source from the content-addressed store, hash-verified

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.

Callers 7

InsertCharMethod · 0.85
APPEND_CHARMethod · 0.85
PutUtf8Method · 0.85
FindUtf8Method · 0.85
PeekUntilUtf8Method · 0.85
ReadUntilUtf8Method · 0.85
SkipUntilUtf8Method · 0.85

Calls 1

GBFunction · 0.85

Tested by

no test coverage detected