Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- QStringList splitUnicodeString(const QString &str, quint32 limit)
- {
- if (limit < 4) {
- qDebug() << "splitUnicodeString error: limit < 4";
- return QStringList();
- }
- QStringList result;
- std::u32string u32str = str.toStdU32String();
- quint32 len = 0;
- QString tmp;
- for (quint32 ch : u32str) {
- if (ch <= UCHAR_MAX) {
- if (len + 1 > limit) {
- result.push_back(tmp);
- tmp.clear();
- len = 1;
- } else {
- len += 1;
- }
- } else if (ch <= USHRT_MAX) {
- if (len + 2 > limit) {
- result.push_back(tmp);
- tmp.clear();
- len = 2;
- } else {
- len += 2;
- }
- } else if (ch <= UINT_MAX) {
- if (len + 4 > limit) {
- result.push_back(tmp);
- tmp.clear();
- len = 4;
- } else {
- len += 4;
- }
- }
- tmp.push_back(ch);
- }
- return result;
- }
- int main(int argc, char *argv[])
- {
- qDebug() << splitUnicodeString("lol😀lolазазазаз", 7);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement