add util::length_utf8

This commit is contained in:
MihailRis
2024-10-26 10:31:45 +03:00
parent 52ed1abf1c
commit f3181dee88
2 changed files with 16 additions and 1 deletions
+11 -1
View File
@@ -128,7 +128,7 @@ inline uint utf8_len(ubyte cp) {
if ((cp & 0xF8) == 0xF0) {
return 4;
}
return 0;
throw std::runtime_error("utf8 decode error");
}
uint32_t util::decode_utf8(uint& size, const char* chr) {
@@ -156,6 +156,16 @@ size_t util::crop_utf8(std::string_view s, size_t maxSize) {
return pos;
}
size_t util::length_utf8(std::string_view s) {
size_t length = 0;
size_t pos = 0;
while (pos < s.length()) {
pos += utf8_len(s[pos]);
length++;
}
return length;
}
template<class C>
std::string xstr2str_utf8(const std::basic_string<C>& xs) {
std::vector<char> chars;