Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. QStringList splitUnicodeString(const QString &str, quint32 limit)
  2. {
  3.     if (limit < 4) {
  4.         qDebug() << "splitUnicodeString error: limit < 4";
  5.         return QStringList();
  6.     }
  7.     QStringList result;
  8.     std::u32string u32str = str.toStdU32String();
  9.  
  10.     quint32 len = 0;
  11.     QString tmp;
  12.     for (quint32 ch : u32str) {
  13.         if (ch <= UCHAR_MAX) {
  14.             if (len + 1 > limit) {
  15.                 result.push_back(tmp);
  16.                 tmp.clear();
  17.                 len = 1;
  18.             } else {
  19.                 len += 1;
  20.             }
  21.         } else if (ch <= USHRT_MAX) {
  22.             if (len + 2 > limit) {
  23.                 result.push_back(tmp);
  24.                 tmp.clear();
  25.                 len = 2;
  26.             } else {
  27.                 len += 2;
  28.             }
  29.         } else if (ch <= UINT_MAX) {
  30.             if (len + 4 > limit) {
  31.                 result.push_back(tmp);
  32.                 tmp.clear();
  33.                 len = 4;
  34.             } else {
  35.                 len += 4;
  36.             }
  37.         }
  38.         tmp.push_back(ch);
  39.     }
  40.     return result;
  41. }
  42.  
  43. int main(int argc, char *argv[])
  44. {
  45.     qDebug() << splitUnicodeString("lol😀lolазазазаз", 7);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement