From 3a3e2193d7f8879e2d68c311c3149e9f6b779d5a Mon Sep 17 00:00:00 2001 From: Konstantin Ritt Date: Sat, 7 Nov 2015 00:31:43 +0400 Subject: QUnicodeTables generator: Minor clean-up & stricter checks Use static_cast instead of C-style cast; ushort -> unsigned short; Avoid a lvalue in qGetProp(); Minor styling fixes. Change-Id: Ic6412e28ac9316a587d32d0e674d21f672967fd6 Reviewed-by: Lars Knoll --- util/unicode/main.cpp | 105 ++++++++++++++++++++++++-------------------------- 1 file changed, 51 insertions(+), 54 deletions(-) (limited to 'util') diff --git a/util/unicode/main.cpp b/util/unicode/main.cpp index ab37bffe87..35f5aa9d24 100644 --- a/util/unicode/main.cpp +++ b/util/unicode/main.cpp @@ -848,12 +848,14 @@ static int appendToSpecialCaseMap(const QList &map) { QList utf16map; for (int i = 0; i < map.size(); ++i) { - int val = map.at(i); - if (QChar::requiresSurrogates(val)) { - utf16map << QChar::highSurrogate(val); - utf16map << QChar::lowSurrogate(val); + uint codepoint = map.at(i); + // if the condition below doesn't hold anymore we need to modify our special case mapping code + Q_ASSERT(!QChar::requiresSurrogates(codepoint)); + if (QChar::requiresSurrogates(codepoint)) { + utf16map << QChar::highSurrogate(codepoint); + utf16map << QChar::lowSurrogate(codepoint); } else { - utf16map << val; + utf16map << codepoint; } } int length = utf16map.size(); @@ -1133,41 +1135,39 @@ static void readUnicodeData() int upperCase = properties[UD_UpperCase].toInt(&ok, 16); Q_ASSERT(ok); int diff = upperCase - codepoint; + // if the conditions below doesn't hold anymore we need to modify our upper casing code + Q_ASSERT(QChar::requiresSurrogates(codepoint) == QChar::requiresSurrogates(upperCase)); + if (QChar::requiresSurrogates(codepoint)) { + Q_ASSERT(QChar::highSurrogate(codepoint) == QChar::highSurrogate(upperCase)); + Q_ASSERT(QChar::lowSurrogate(codepoint) + diff == QChar::lowSurrogate(upperCase)); + } if (qAbs(diff) >= (1<<13)) { qWarning() << "upperCaseDiff exceeded (" << hex << codepoint << "->" << upperCase << "); map it for special case"; - // if the condition below doesn't hold anymore we need to modify our special upper casing code in qchar.cpp - Q_ASSERT(!QChar::requiresSurrogates(codepoint) && !QChar::requiresSurrogates(upperCase)); data.p.upperCaseSpecial = true; data.p.upperCaseDiff = appendToSpecialCaseMap(QList() << upperCase); } else { data.p.upperCaseDiff = diff; maxUpperCaseDiff = qMax(maxUpperCaseDiff, qAbs(diff)); } - if (QChar::requiresSurrogates(codepoint) || QChar::requiresSurrogates(upperCase)) { - // if the conditions below doesn't hold anymore we need to modify our upper casing code - Q_ASSERT(QChar::highSurrogate(codepoint) == QChar::highSurrogate(upperCase)); - Q_ASSERT(QChar::lowSurrogate(codepoint) + diff == QChar::lowSurrogate(upperCase)); - } } if (!properties[UD_LowerCase].isEmpty()) { int lowerCase = properties[UD_LowerCase].toInt(&ok, 16); Q_ASSERT(ok); int diff = lowerCase - codepoint; + // if the conditions below doesn't hold anymore we need to modify our lower casing code + Q_ASSERT(QChar::requiresSurrogates(codepoint) == QChar::requiresSurrogates(lowerCase)); + if (QChar::requiresSurrogates(codepoint)) { + Q_ASSERT(QChar::highSurrogate(codepoint) == QChar::highSurrogate(lowerCase)); + Q_ASSERT(QChar::lowSurrogate(codepoint) + diff == QChar::lowSurrogate(lowerCase)); + } if (qAbs(diff) >= (1<<13)) { qWarning() << "lowerCaseDiff exceeded (" << hex << codepoint << "->" << lowerCase << "); map it for special case"; - // if the condition below doesn't hold anymore we need to modify our special lower casing code in qchar.cpp - Q_ASSERT(!QChar::requiresSurrogates(codepoint) && !QChar::requiresSurrogates(lowerCase)); data.p.lowerCaseSpecial = true; data.p.lowerCaseDiff = appendToSpecialCaseMap(QList() << lowerCase); } else { data.p.lowerCaseDiff = diff; maxLowerCaseDiff = qMax(maxLowerCaseDiff, qAbs(diff)); } - if (QChar::requiresSurrogates(codepoint) || QChar::requiresSurrogates(lowerCase)) { - // if the conditions below doesn't hold anymore we need to modify our lower casing code - Q_ASSERT(QChar::highSurrogate(codepoint) == QChar::highSurrogate(lowerCase)); - Q_ASSERT(QChar::lowSurrogate(codepoint) + diff == QChar::lowSurrogate(lowerCase)); - } } // we want toTitleCase to map to ToUpper in case we don't have any titlecase. if (properties[UD_TitleCase].isEmpty()) @@ -1176,21 +1176,20 @@ static void readUnicodeData() int titleCase = properties[UD_TitleCase].toInt(&ok, 16); Q_ASSERT(ok); int diff = titleCase - codepoint; + // if the conditions below doesn't hold anymore we need to modify our title casing code + Q_ASSERT(QChar::requiresSurrogates(codepoint) == QChar::requiresSurrogates(titleCase)); + if (QChar::requiresSurrogates(codepoint)) { + Q_ASSERT(QChar::highSurrogate(codepoint) == QChar::highSurrogate(titleCase)); + Q_ASSERT(QChar::lowSurrogate(codepoint) + diff == QChar::lowSurrogate(titleCase)); + } if (qAbs(diff) >= (1<<13)) { qWarning() << "titleCaseDiff exceeded (" << hex << codepoint << "->" << titleCase << "); map it for special case"; - // if the condition below doesn't hold anymore we need to modify our special title casing code in qchar.cpp - Q_ASSERT(!QChar::requiresSurrogates(codepoint) && !QChar::requiresSurrogates(titleCase)); data.p.titleCaseSpecial = true; data.p.titleCaseDiff = appendToSpecialCaseMap(QList() << titleCase); } else { data.p.titleCaseDiff = diff; maxTitleCaseDiff = qMax(maxTitleCaseDiff, qAbs(diff)); } - if (QChar::requiresSurrogates(codepoint) || QChar::requiresSurrogates(titleCase)) { - // if the conditions below doesn't hold anymore we need to modify our title casing code - Q_ASSERT(QChar::highSurrogate(codepoint) == QChar::highSurrogate(titleCase)); - Q_ASSERT(QChar::lowSurrogate(codepoint) + diff == QChar::lowSurrogate(titleCase)); - } } if (!properties[UD_DigitValue].isEmpty()) @@ -1546,8 +1545,10 @@ static QByteArray createNormalizationCorrections() ++numCorrections; maxVersion = qMax(c.version, maxVersion); } + if (out.endsWith(",\n")) + out.chop(2); - out += "};\n\n" + out += "\n};\n\n" "enum { NumNormalizationCorrections = " + QByteArray::number(numCorrections) + " };\n" "enum { NormalizationCorrectionsVersionMax = " + QByteArray::number(maxVersion) + " };\n\n"; @@ -1742,23 +1743,20 @@ static void readCaseFolding() if (foldMap.size() == 1) { int caseFolded = foldMap.at(0); int diff = caseFolded - codepoint; + // if the conditions below doesn't hold anymore we need to modify our case folding code + Q_ASSERT(QChar::requiresSurrogates(codepoint) == QChar::requiresSurrogates(caseFolded)); + if (QChar::requiresSurrogates(codepoint)) { + Q_ASSERT(QChar::highSurrogate(codepoint) == QChar::highSurrogate(caseFolded)); + Q_ASSERT(QChar::lowSurrogate(codepoint) + diff == QChar::lowSurrogate(caseFolded)); + } if (qAbs(diff) >= (1<<13)) { qWarning() << "caseFoldDiff exceeded (" << hex << codepoint << "->" << caseFolded << "); map it for special case"; - // if the condition below doesn't hold anymore we need to modify our special case folding code in qchar.cpp - Q_ASSERT(!QChar::requiresSurrogates(codepoint) && !QChar::requiresSurrogates(caseFolded)); ud.p.caseFoldSpecial = true; ud.p.caseFoldDiff = appendToSpecialCaseMap(foldMap); } else { ud.p.caseFoldDiff = diff; maxCaseFoldDiff = qMax(maxCaseFoldDiff, qAbs(diff)); } - if (QChar::requiresSurrogates(codepoint) || QChar::requiresSurrogates(caseFolded)) { - // if the conditions below doesn't hold anymore we need to modify our case folding code - Q_ASSERT(QChar::highSurrogate(codepoint) == QChar::highSurrogate(caseFolded)); - Q_ASSERT(QChar::lowSurrogate(codepoint) + diff == QChar::lowSurrogate(caseFolded)); - } -// if (caseFolded != codepoint + ud.p.lowerCaseDiff) -// qDebug() << hex << codepoint; } else { qFatal("we currently don't support full case foldings"); // qDebug() << "special" << hex << foldMap; @@ -2301,7 +2299,7 @@ static QByteArray createPropertyInfo() out += "static const unsigned short uc_property_trie[] = {\n"; // first write the map - out += " // 0 - 0x" + QByteArray::number(BMP_END, 16); + out += " // [0x0..0x" + QByteArray::number(BMP_END, 16) + ")"; for (int i = 0; i < BMP_END/BMP_BLOCKSIZE; ++i) { if (!(i % 8)) { if (out.endsWith(' ')) @@ -2315,7 +2313,7 @@ static QByteArray createPropertyInfo() } if (out.endsWith(' ')) out.chop(1); - out += "\n\n // 0x" + QByteArray::number(BMP_END, 16) + " - 0x" + QByteArray::number(SMP_END, 16) + "\n"; + out += "\n\n // [0x" + QByteArray::number(BMP_END, 16) + "..0x" + QByteArray::number(SMP_END, 16) + ")\n"; for (int i = BMP_END/BMP_BLOCKSIZE; i < blockMap.size(); ++i) { if (!(i % 8)) { if (out.endsWith(' ')) @@ -2346,8 +2344,8 @@ static QByteArray createPropertyInfo() out += ", "; } } - if (out.endsWith(' ')) - out.chop(1); + if (out.endsWith(", ")) + out.chop(2); out += "\n};\n\n"; out += "#define GET_PROP_INDEX(ucs4) \\\n" @@ -2430,20 +2428,19 @@ static QByteArray createPropertyInfo() out += QByteArray::number( p.script ); out += " },"; } - out.chop(1); + if (out.endsWith(',')) + out.chop(1); out += "\n};\n\n"; out += "Q_DECL_CONST_FUNCTION static inline const Properties *qGetProp(uint ucs4) Q_DECL_NOTHROW\n" "{\n" - " const int index = GET_PROP_INDEX(ucs4);\n" - " return uc_properties + index;\n" + " return uc_properties + GET_PROP_INDEX(ucs4);\n" "}\n" "\n" "Q_DECL_CONST_FUNCTION static inline const Properties *qGetProp(ushort ucs2) Q_DECL_NOTHROW\n" "{\n" - " const int index = GET_PROP_INDEX_UCS2(ucs2);\n" - " return uc_properties + index;\n" + " return uc_properties + GET_PROP_INDEX_UCS2(ucs2);\n" "}\n" "\n" "Q_DECL_CONST_FUNCTION Q_CORE_EXPORT const Properties * QT_FASTCALL properties(uint ucs4) Q_DECL_NOTHROW\n" @@ -2458,22 +2455,22 @@ static QByteArray createPropertyInfo() out += "Q_CORE_EXPORT GraphemeBreakClass QT_FASTCALL graphemeBreakClass(uint ucs4) Q_DECL_NOTHROW\n" "{\n" - " return (GraphemeBreakClass)qGetProp(ucs4)->graphemeBreakClass;\n" + " return static_cast(qGetProp(ucs4)->graphemeBreakClass);\n" "}\n" "\n" "Q_CORE_EXPORT WordBreakClass QT_FASTCALL wordBreakClass(uint ucs4) Q_DECL_NOTHROW\n" "{\n" - " return (WordBreakClass)qGetProp(ucs4)->wordBreakClass;\n" + " return static_cast(qGetProp(ucs4)->wordBreakClass);\n" "}\n" "\n" "Q_CORE_EXPORT SentenceBreakClass QT_FASTCALL sentenceBreakClass(uint ucs4) Q_DECL_NOTHROW\n" "{\n" - " return (SentenceBreakClass)qGetProp(ucs4)->sentenceBreakClass;\n" + " return static_cast(qGetProp(ucs4)->sentenceBreakClass);\n" "}\n" "\n" "Q_CORE_EXPORT LineBreakClass QT_FASTCALL lineBreakClass(uint ucs4) Q_DECL_NOTHROW\n" "{\n" - " return (LineBreakClass)qGetProp(ucs4)->lineBreakClass;\n" + " return static_cast(qGetProp(ucs4)->lineBreakClass);\n" "}\n" "\n"; @@ -2486,7 +2483,7 @@ static QByteArray createSpecialCaseMap() QByteArray out; - out += "static const ushort specialCaseMap[] = {\n" + out += "static const unsigned short specialCaseMap[] = {\n" " 0x0, // placeholder"; int i = 1; while (i < specialCaseMap.size()) { @@ -2686,10 +2683,10 @@ static QByteArray createCompositionInfo() " (ucs4 < 0x" + QByteArray::number(BMP_END, 16) + " \\\n" " ? (uc_decomposition_trie[uc_decomposition_trie[ucs4>>" + QByteArray::number(BMP_SHIFT) + "] + (ucs4 & 0x" + QByteArray::number(BMP_BLOCKSIZE-1, 16)+ ")]) \\\n" - " : (ucs4 < 0x" + QByteArray::number(SMP_END, 16) + "\\\n" + " : (ucs4 < 0x" + QByteArray::number(SMP_END, 16) + " \\\n" " ? uc_decomposition_trie[uc_decomposition_trie[((ucs4 - 0x" + QByteArray::number(BMP_END, 16) + ")>>" + QByteArray::number(SMP_SHIFT) + ") + 0x" + QByteArray::number(BMP_END/BMP_BLOCKSIZE, 16) + "]" - " + (ucs4 & 0x" + QByteArray::number(SMP_BLOCKSIZE-1, 16) + ")]\\\n" + " + (ucs4 & 0x" + QByteArray::number(SMP_BLOCKSIZE-1, 16) + ")] \\\n" " : 0xffff))\n\n"; out += "static const unsigned short uc_decomposition_map[] = {"; @@ -2885,10 +2882,10 @@ static QByteArray createLigatureInfo() " (ucs4 < 0x" + QByteArray::number(BMP_END, 16) + " \\\n" " ? (uc_ligature_trie[uc_ligature_trie[ucs4>>" + QByteArray::number(BMP_SHIFT) + "] + (ucs4 & 0x" + QByteArray::number(BMP_BLOCKSIZE-1, 16)+ ")]) \\\n" - " : (ucs4 < 0x" + QByteArray::number(SMP_END, 16) + "\\\n" + " : (ucs4 < 0x" + QByteArray::number(SMP_END, 16) + " \\\n" " ? uc_ligature_trie[uc_ligature_trie[((ucs4 - 0x" + QByteArray::number(BMP_END, 16) + ")>>" + QByteArray::number(SMP_SHIFT) + ") + 0x" + QByteArray::number(BMP_END/BMP_BLOCKSIZE, 16) + "]" - " + (ucs4 & 0x" + QByteArray::number(SMP_BLOCKSIZE-1, 16) + ")]\\\n" + " + (ucs4 & 0x" + QByteArray::number(SMP_BLOCKSIZE-1, 16) + ")] \\\n" " : 0xffff))\n\n"; out += "static const unsigned short uc_ligature_map[] = {"; -- cgit v1.2.3