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

Function DecodeUtf8

src/core/utf8.cpp:48–74  ·  view source on GitHub ↗

* Decode a character from UTF-8. * @param buf Binary data. * @return Length and character. Length is zero, if the input data is invalid. */

Source from the content-addressed store, hash-verified

46 * @return Length and character. Length is zero, if the input data is invalid.
47 */
48[[nodiscard]] std::pair<size_t, char32_t> DecodeUtf8(std::string_view buf)
49{
50 if (buf.size() >= 1 && !HasBit(buf[0], 7)) {
51 /* Single byte character: 0xxxxxxx */
52 char32_t c = buf[0];
53 return {1, c};
54 } else if (buf.size() >= 2 && GB(buf[0], 5, 3) == 6) {
55 if (IsUtf8Part(buf[1])) {
56 /* Double byte character: 110xxxxx 10xxxxxx */
57 char32_t c = GB(buf[0], 0, 5) << 6 | GB(buf[1], 0, 6);
58 if (c >= 0x80) return {2, c};
59 }
60 } else if (buf.size() >= 3 && GB(buf[0], 4, 4) == 14) {
61 if (IsUtf8Part(buf[1]) && IsUtf8Part(buf[2])) {
62 /* Triple byte character: 1110xxxx 10xxxxxx 10xxxxxx */
63 char32_t c = GB(buf[0], 0, 4) << 12 | GB(buf[1], 0, 6) << 6 | GB(buf[2], 0, 6);
64 if (c >= 0x800) return {3, c};
65 }
66 } else if (buf.size() >= 4 && GB(buf[0], 3, 5) == 30) {
67 if (IsUtf8Part(buf[1]) && IsUtf8Part(buf[2]) && IsUtf8Part(buf[3])) {
68 /* 4 byte character: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
69 char32_t c = GB(buf[0], 0, 3) << 18 | GB(buf[1], 0, 6) << 12 | GB(buf[2], 0, 6) << 6 | GB(buf[3], 0, 6);
70 if (c >= 0x10000 && c <= 0x10FFFF) return {4, c};
71 }
72 }
73 return {};
74}
75
76/**
77 * Create iterator pointing at codepoint, which occupies the byte position "offset".

Callers 5

operator()Method · 0.85
operator*Method · 0.85
PeekUtf8Method · 0.85
GlobalVarChangeInfoFunction · 0.85
PollEventMethod · 0.85

Calls 4

HasBitFunction · 0.85
GBFunction · 0.85
IsUtf8PartFunction · 0.85
sizeMethod · 0.45

Tested by

no test coverage detected