Character length Utf-32 in Qt

I am using Qt5. I have a QString containing one U character "\ x1D4CC" (𝓌) that is longer than 16 bits. Although this is only one character, Qt returns the size of this string 2. Is there a way to show how many real characters QString makes an assumption that there can be 32 characters?

+5
source share
1 answer

Unicode characters with code values ​​above 65535 are stored using surrogate pairs, i.e. two consecutive QChars . QString::length returns the number of QChar in this string, which may differ from the number of graphemes (real characters).

To calculate the number of graphemes, you can use the QTextBoundaryFinder class.

 QString str = "𝓌"; QTextBoundaryFinder finder(QTextBoundaryFinder::Grapheme, str); int count = 0; while (finder.toNextBoundary() != -1) ++count; qDebug() << count; 

Or you can convert your string to a UCS-4 / UTF-32 representation and calculate the number of 32-bit characters.

 QVector<uint> ucs4 = str.toUcs4(); qDebug() << ucs4.size(); 
+3
source

Source: https://habr.com/ru/post/1247124/


All Articles