MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / decode_utf8

Function decode_utf8

smallthinker/src/llama-grammar.cpp:16–29  ·  view source on GitHub ↗

NOTE: assumes valid utf8 (but checks for overrun)

Source from the content-addressed store, hash-verified

14
15// NOTE: assumes valid utf8 (but checks for overrun)
16static std::pair<uint32_t, const char *> decode_utf8(const char * src) {
17 static const int lookup[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 4 };
18 uint8_t first_byte = static_cast<uint8_t>(*src);
19 uint8_t highbits = first_byte >> 4;
20 int len = lookup[highbits];
21 uint8_t mask = (1 << (8 - len)) - 1;
22 uint32_t value = first_byte & mask;
23 const char * end = src + len; // may overrun!
24 const char * pos = src + 1;
25 for ( ; pos < end && *pos; pos++) {
26 value = (value << 6) + (static_cast<uint8_t>(*pos) & 0x3F);
27 }
28 return std::make_pair(value, pos);
29}
30
31static std::pair<std::vector<uint32_t>, llama_partial_utf8> decode_utf8(
32 const std::string & src,

Callers 3

parse_charFunction · 0.70
llama_grammar_apply_implFunction · 0.70
llama_grammar_accept_strFunction · 0.70

Calls 5

reserveMethod · 0.80
c_strMethod · 0.45
sizeMethod · 0.45
push_backMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected