MCPcopy Create free account
hub / github.com/0xShug0/audio.cpp / unicode_cpt_from_utf8

Function unicode_cpt_from_utf8

external/llama_tokenizer/unicode.cpp:30–65  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28}
29
30uint32_t unicode_cpt_from_utf8(const std::string & utf8, size_t & offset) {
31 assert(offset < utf8.size());
32 if (!(utf8[offset + 0] & 0x80)) {
33 auto result = utf8[offset + 0];
34 offset += 1;
35 return result;
36 }
37 if (!(utf8[offset + 0] & 0x40)) {
38 throw std::invalid_argument("invalid character");
39 }
40 if (!(utf8[offset + 0] & 0x20)) {
41 if (offset + 1 >= utf8.size() || ! ((utf8[offset + 1] & 0xc0) == 0x80)) {
42 throw std::invalid_argument("invalid character");
43 }
44 auto result = ((utf8[offset + 0] & 0x1f) << 6) | (utf8[offset + 1] & 0x3f);
45 offset += 2;
46 return result;
47 }
48 if (!(utf8[offset + 0] & 0x10)) {
49 if (offset + 2 >= utf8.size() || ! ((utf8[offset + 1] & 0xc0) == 0x80) || ! ((utf8[offset + 2] & 0xc0) == 0x80)) {
50 throw std::invalid_argument("invalid character");
51 }
52 auto result = ((utf8[offset + 0] & 0x0f) << 12) | ((utf8[offset + 1] & 0x3f) << 6) | (utf8[offset + 2] & 0x3f);
53 offset += 3;
54 return result;
55 }
56 if (!(utf8[offset + 0] & 0x08)) {
57 if (offset + 3 >= utf8.size() || ! ((utf8[offset + 1] & 0xc0) == 0x80) || ! ((utf8[offset + 2] & 0xc0) == 0x80) || !((utf8[offset + 3] & 0xc0) == 0x80)) {
58 throw std::invalid_argument("invalid character");
59 }
60 auto result = ((utf8[offset + 0] & 0x07) << 18) | ((utf8[offset + 1] & 0x3f) << 12) | ((utf8[offset + 2] & 0x3f) << 6) | (utf8[offset + 3] & 0x3f);
61 offset += 4;
62 return result;
63 }
64 throw std::invalid_argument("failed to convert utf8 to codepoint");
65}
66
67//static std::vector<uint16_t> unicode_cpt_to_utf16(uint32_t cpt) {
68// std::vector<uint16_t> result;

Callers 2

unicode_cpts_from_utf8Function · 0.85

Calls 1

sizeMethod · 0.45

Tested by

no test coverage detected