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

Function decode_utf8

common/grammar-parser.cpp:12–25  ·  view source on GitHub ↗

NOTE: assumes valid utf8 (but checks for overrun) copied from llama.cpp

Source from the content-addressed store, hash-verified

10 // NOTE: assumes valid utf8 (but checks for overrun)
11 // copied from llama.cpp
12 static std::pair<uint32_t, const char *> decode_utf8(const char * src) {
13 static const int lookup[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 4 };
14 uint8_t first_byte = static_cast<uint8_t>(*src);
15 uint8_t highbits = first_byte >> 4;
16 int len = lookup[highbits];
17 uint8_t mask = (1 << (8 - len)) - 1;
18 uint32_t value = first_byte & mask;
19 const char * end = src + len; // may overrun!
20 const char * pos = src + 1;
21 for ( ; pos < end && *pos; pos++) {
22 value = (value << 6) + (static_cast<uint8_t>(*pos) & 0x3F);
23 }
24 return std::make_pair(value, pos);
25 }
26
27 static uint32_t get_symbol_id(parse_state & state, const char * src, size_t len) {
28 uint32_t next_id = static_cast<uint32_t>(state.symbol_ids.size());

Callers 1

parse_charFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected