Добавляем получение символа getUtf8CharByIndex

This commit is contained in:
2025-05-29 18:52:23 +03:00
parent d11f69b08d
commit 33d449d892
2 changed files with 27 additions and 0 deletions

View File

@@ -51,3 +51,5 @@ unsigned char ChartoHex(char ch);
std::vector<String> splitStr(const String& str, const String& delimiter);
bool strInVector(const String& str, const std::vector<String>& vec);
String getUtf8CharByIndex(const String& utf8str, int index);

View File

@@ -233,4 +233,29 @@ bool strInVector(const String& str, const std::vector<String>& vec) {
if (vec[i] == str) return true;
}
return false;
}
String getUtf8CharByIndex(const String& utf8str, int index) {
if (index < 0) index = 0;
int len = utf8str.length();
int charCount = 0;
int i = 0;
while (i < len) {
int charLen = 1;
unsigned char c = utf8str[i];
if ((c & 0x80) == 0x00) charLen = 1; // 0xxxxxxx
else if ((c & 0xE0) == 0xC0) charLen = 2; // 110xxxxx
else if ((c & 0xF0) == 0xE0) charLen = 3; // 1110xxxx
else if ((c & 0xF8) == 0xF0) charLen = 4; // 11110xxx
if (charCount == index) {
return utf8str.substring(i, i + charLen);
}
if (i + charLen >= len) return utf8str.substring(i, i + charLen);
i += charLen;
charCount++;
}
return "";
}