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

Function unicode_cpt_from_utf8

smallthinker/src/unicode.cpp:36–71  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 3

normalize_prefixMethod · 0.85
unicode_cpts_from_utf8Function · 0.85

Calls 1

sizeMethod · 0.45

Tested by

no test coverage detected