MCPcopy Create free account
hub / github.com/bab2min/tomotopy / char2Byte

Function char2Byte

src/python/handler/py_main.cpp:19–54  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17#endif
18
19void char2Byte(const char* strBegin, const char* strEnd, vector<uint32_t>& startPos, vector<uint16_t>& length)
20{
21 if (strBegin == strEnd) return;
22 vector<size_t> charPos;
23 auto it = strBegin;
24 for (; it != strEnd; )
25 {
26 charPos.emplace_back(it - strBegin);
27 uint8_t c = *it;
28 if ((c & 0xF8) == 0xF0)
29 {
30 it += 4;
31 }
32 else if ((c & 0xF0) == 0xE0)
33 {
34 it += 3;
35 }
36 else if ((c & 0xE0) == 0xC0)
37 {
38 it += 2;
39 }
40 else if ((c & 0x80))
41 {
42 throw std::runtime_error{ "utf-8 decoding error" };
43 }
44 else it += 1;
45 }
46 charPos.emplace_back(strEnd - strBegin);
47
48 for (size_t i = 0; i < startPos.size(); ++i)
49 {
50 size_t s = startPos[i], e = (size_t)startPos[i] + length[i];
51 startPos[i] = charPos[s];
52 length[i] = charPos[e] - charPos[s];
53 }
54}
55
56void char2Byte(const string& str, vector<uint32_t>& startPos, vector<uint16_t>& length)
57{

Callers 2

insertCorpusMethod · 0.85
makeCorpusMethod · 0.85

Calls 4

emplace_backMethod · 0.80
sizeMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected