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

Function split_utf8_spans

src/framework/text/chunking.cpp:65–94  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

63 token == u8"," || token == u8"、" || token == u8";" || token == u8":";
64}
65
66bool is_tag_open(std::string_view token) {
67 return token == "[" || token == "<";
68}
69
70bool is_tag_close(std::string_view token, std::string_view open) {
71 return (open == "[" && token == "]") || (open == "<" && token == ">");
72}
73
74std::vector<Utf8Span> split_utf8_spans(std::string_view text, std::string_view label) {
75 std::vector<Utf8Span> spans;
76 spans.reserve(utf8_codepoint_count(text, label));
77 for (size_t pos = 0; pos < text.size();) {
78 const auto ch = static_cast<unsigned char>(text[pos]);
79 size_t width = 0;
80 if (ch <= 0x7FU) {
81 width = 1;
82 } else if ((ch & 0xE0U) == 0xC0U) {
83 width = 2;
84 } else if ((ch & 0xF0U) == 0xE0U) {
85 width = 3;
86 } else if ((ch & 0xF8U) == 0xF0U) {
87 width = 4;
88 } else {
89 throw std::runtime_error(std::string(label) + " contains invalid UTF-8");
90 }
91 if (pos + width > text.size()) {
92 throw std::runtime_error(std::string(label) + " contains truncated UTF-8");
93 }
94 for (size_t i = 1; i < width; ++i) {
95 if (!is_utf8_continuation(static_cast<unsigned char>(text[pos + i]))) {
96 throw std::runtime_error(std::string(label) + " contains invalid UTF-8 continuation byte");
97 }

Callers 2

split_tag_aware_bodyFunction · 0.85

Calls 4

utf8_codepoint_countFunction · 0.85
is_utf8_continuationFunction · 0.85
stringFunction · 0.50
sizeMethod · 0.45

Tested by

no test coverage detected