MCPcopy Create free account
hub / github.com/appdevforall/CodeOnTheGo / decode_utf8

Function decode_utf8

subprojects/llama.cpp/src/llama-grammar.cpp:18–31  ·  view source on GitHub ↗

NOTE: assumes valid utf8 (but checks for overrun)

Source from the content-addressed store, hash-verified

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

Callers 4

parse_charFunction · 0.70
llama_grammar_apply_implFunction · 0.70
llama_grammar_accept_strFunction · 0.70

Calls 3

sizeMethod · 0.65
clearMethod · 0.65
push_backMethod · 0.45

Tested by

no test coverage detected