mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-26 14:12:16 +03:00
Добавляем получение символа getUtf8CharByIndex
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -234,3 +234,28 @@ bool strInVector(const String& str, const std::vector<String>& vec) {
|
||||
}
|
||||
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 "";
|
||||
}
|
||||
Reference in New Issue
Block a user