From 9fd2edb6da5f1f6d644cd7c3f35aebe3e88beee2 Mon Sep 17 00:00:00 2001 From: Konstantin Ritt Date: Thu, 12 Apr 2012 18:21:39 +0300 Subject: [PATCH] replace hardcoded values with a surrogate handling methods Change-Id: Ib41e08d835f2e8ca2e32b4025c6f5a99840f2e27 Reviewed-by: Oswald Buddenhagen Reviewed-by: Lars Knoll --- src/corelib/codecs/qutfcodec.cpp | 6 +++--- src/corelib/io/qurl.cpp | 6 +++--- src/corelib/json/qjsonparser.cpp | 7 ++++--- src/corelib/json/qjsonwriter.cpp | 2 +- src/plugins/platforms/windows/qwindowsfontengine.cpp | 12 ++++++------ .../platforms/windows/qwindowsfontenginedirectwrite.cpp | 9 +++++---- src/widgets/widgets/qwidgetlinecontrol.cpp | 8 ++++---- tests/benchmarks/corelib/tools/qstring/main.cpp | 6 +++--- 8 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/corelib/codecs/qutfcodec.cpp b/src/corelib/codecs/qutfcodec.cpp index 18d063a..9111ac6 100644 --- a/src/corelib/codecs/qutfcodec.cpp +++ b/src/corelib/codecs/qutfcodec.cpp @@ -127,7 +127,7 @@ QByteArray QUtf8::convertFromUnicode(const QChar *uc, int len, QTextCodec::Conve continue; } - if (u > 0xffff) { + if (QChar::requiresSurrogates(u)) { *cursor++ = 0xf0 | ((uchar) (u >> 18)); *cursor++ = 0x80 | (((uchar) (u >> 12)) & 0x3f); } else { @@ -196,7 +196,7 @@ QString QUtf8::convertToUnicode(const char *chars, int len, QTextCodec::Converte bool nonCharacter; if (!headerdone && uc == 0xfeff) { // don't do anything, just skip the BOM - } else if (!(nonCharacter = isUnicodeNonCharacter(uc)) && uc > 0xffff && uc < 0x110000) { + } else if (!(nonCharacter = isUnicodeNonCharacter(uc)) && QChar::requiresSurrogates(uc) && uc < 0x110000) { // surrogate pair Q_ASSERT((qch - (ushort*)result.unicode()) + 2 < result.length()); *qch++ = QChar::highSurrogate(uc); @@ -487,7 +487,7 @@ QString QUtf32::convertToUnicode(const char *chars, int len, QTextCodec::Convert } } uint code = (endian == BigEndianness) ? qFromBigEndian(tuple) : qFromLittleEndian(tuple); - if (code >= 0x10000) { + if (QChar::requiresSurrogates(code)) { *qch++ = QChar::highSurrogate(code); *qch++ = QChar::lowSurrogate(code); } else { diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp index 0659053..c6b5ea6 100644 --- a/src/corelib/io/qurl.cpp +++ b/src/corelib/io/qurl.cpp @@ -2388,7 +2388,7 @@ static void mapToLowerCase(QString *str, int from) while (l < 4 && entry->mapping[l]) ++l; if (l > 1) { - if (uc <= 0xffff) + if (!QChar::requiresSurrogates(uc)) str->replace(i, 1, reinterpret_cast(&entry->mapping[0]), l); else str->replace(i-1, 2, reinterpret_cast(&entry->mapping[0]), l); @@ -2434,7 +2434,7 @@ static void stripProhibitedOutput(QString *str, int from) uc = QChar::surrogateToUcs4(uc, low); } } - if (uc <= 0xFFFF) { + if (!QChar::requiresSurrogates(uc)) { if (uc < 0x80 || !(uc <= 0x009F || uc == 0x00A0 @@ -2991,7 +2991,7 @@ void qt_nameprep(QString *source, int from) } } if (!isMappedToNothing(uc)) { - if (uc <= 0xFFFF) { + if (!QChar::requiresSurrogates(uc)) { *out++ = *in; } else { *out++ = QChar::highSurrogate(uc); diff --git a/src/corelib/json/qjsonparser.cpp b/src/corelib/json/qjsonparser.cpp index 7d25c81..a174265 100644 --- a/src/corelib/json/qjsonparser.cpp +++ b/src/corelib/json/qjsonparser.cpp @@ -769,9 +769,10 @@ static inline bool scanUtf8Char(const char *&json, const char *end, uint *result uc = (uc << 6) | (ch & 0x3f); } - if (isUnicodeNonCharacter(uc) || uc >= 0x110000 || - (uc < min_uc) || (uc >= 0xd800 && uc <= 0xdfff)) + if (uc < min_uc || isUnicodeNonCharacter(uc) || + (uc >= 0xd800 && uc <= 0xdfff) || uc >= 0x110000) { return false; + } *result = uc; return true; @@ -850,7 +851,7 @@ bool Parser::parseString(bool *latin1) return false; } } - if (ch > 0xffff) { + if (QChar::requiresSurrogates(ch)) { int pos = reserveSpace(4); *(QJsonPrivate::qle_ushort *)(data + pos) = QChar::highSurrogate(ch); *(QJsonPrivate::qle_ushort *)(data + pos + 2) = QChar::lowSurrogate(ch); diff --git a/src/corelib/json/qjsonwriter.cpp b/src/corelib/json/qjsonwriter.cpp index d392bd7..7cdc3f0 100644 --- a/src/corelib/json/qjsonwriter.cpp +++ b/src/corelib/json/qjsonwriter.cpp @@ -160,7 +160,7 @@ static QByteArray escapedString(const QString &s) continue; } - if (u > 0xffff) { + if (QChar::requiresSurrogates(u)) { *cursor++ = 0xf0 | ((uchar) (u >> 18)); *cursor++ = 0x80 | (((uchar) (u >> 12)) & 0x3f); } else { diff --git a/src/plugins/platforms/windows/qwindowsfontengine.cpp b/src/plugins/platforms/windows/qwindowsfontengine.cpp index 007f6d5..9407262 100644 --- a/src/plugins/platforms/windows/qwindowsfontengine.cpp +++ b/src/plugins/platforms/windows/qwindowsfontengine.cpp @@ -179,14 +179,14 @@ void QWindowsFontEngine::getCMap() } } - +// ### Qt 5.1: replace with QStringIterator inline unsigned int getChar(const QChar *str, int &i, const int len) { - unsigned int uc = str[i].unicode(); - if (uc >= 0xd800 && uc < 0xdc00 && i < len-1) { + uint uc = str[i].unicode(); + if (QChar::isHighSurrogate(uc) && i < len-1) { uint low = str[i+1].unicode(); - if (low >= 0xdc00 && low < 0xe000) { - uc = (uc - 0xd800)*0x400 + (low - 0xdc00) + 0x10000; + if (QChar::isLowSurrogate(low)) { + uc = QChar::surrogateToUcs4(uc, low); ++i; } } @@ -402,7 +402,7 @@ void QWindowsFontEngine::recalcAdvances(QGlyphLayout *glyphs, QTextEngine::Shape if (!ttf) { QChar ch[2] = { ushort(glyph), 0 }; int chrLen = 1; - if (glyph > 0xffff) { + if (QChar::requiresSurrogates(glyph)) { ch[0] = QChar::highSurrogate(glyph); ch[1] = QChar::lowSurrogate(glyph); ++chrLen; diff --git a/src/plugins/platforms/windows/qwindowsfontenginedirectwrite.cpp b/src/plugins/platforms/windows/qwindowsfontenginedirectwrite.cpp index 82410bf..9c98144 100644 --- a/src/plugins/platforms/windows/qwindowsfontenginedirectwrite.cpp +++ b/src/plugins/platforms/windows/qwindowsfontenginedirectwrite.cpp @@ -295,13 +295,14 @@ QFixed QWindowsFontEngineDirectWrite::emSquareSize() const return QFontEngine::emSquareSize(); } +// ### Qt 5.1: replace with QStringIterator inline unsigned int getChar(const QChar *str, int &i, const int len) { - unsigned int uc = str[i].unicode(); - if (uc >= 0xd800 && uc < 0xdc00 && i < len-1) { + uint uc = str[i].unicode(); + if (QChar::isHighSurrogate(uc) && i < len-1) { uint low = str[i+1].unicode(); - if (low >= 0xdc00 && low < 0xe000) { - uc = (uc - 0xd800)*0x400 + (low - 0xdc00) + 0x10000; + if (QChar::isLowSurrogate(low)) { + uc = QChar::surrogateToUcs4(uc, low); ++i; } } diff --git a/src/widgets/widgets/qwidgetlinecontrol.cpp b/src/widgets/widgets/qwidgetlinecontrol.cpp index c9300d3..017cbee 100644 --- a/src/widgets/widgets/qwidgetlinecontrol.cpp +++ b/src/widgets/widgets/qwidgetlinecontrol.cpp @@ -103,11 +103,11 @@ void QWidgetLineControl::updateDisplayText(bool forceUpdate) int cursor = m_cursor - 1; QChar uc = m_text.at(cursor); str[cursor] = uc; - if (cursor > 0 && uc.unicode() >= 0xdc00 && uc.unicode() < 0xe000) { + if (cursor > 0 && uc.isLowSurrogate()) { // second half of a surrogate, check if we have the first half as well, // if yes restore both at once uc = m_text.at(cursor - 1); - if (uc.unicode() >= 0xd800 && uc.unicode() < 0xdc00) + if (uc.isHighSurrogate()) str[cursor - 1] = uc; } } @@ -220,11 +220,11 @@ void QWidgetLineControl::backspace() if (m_maskData) m_cursor = prevMaskBlank(m_cursor); QChar uc = m_text.at(m_cursor); - if (m_cursor > 0 && uc.unicode() >= 0xdc00 && uc.unicode() < 0xe000) { + if (m_cursor > 0 && uc.isLowSurrogate()) { // second half of a surrogate, check if we have the first half as well, // if yes delete both at once uc = m_text.at(m_cursor - 1); - if (uc.unicode() >= 0xd800 && uc.unicode() < 0xdc00) { + if (uc.isHighSurrogate()) { internalDelete(true); --m_cursor; } diff --git a/tests/benchmarks/corelib/tools/qstring/main.cpp b/tests/benchmarks/corelib/tools/qstring/main.cpp index 40300af..9f9e7e4 100644 --- a/tests/benchmarks/corelib/tools/qstring/main.cpp +++ b/tests/benchmarks/corelib/tools/qstring/main.cpp @@ -1996,7 +1996,7 @@ int fromUtf8_qt47(ushort *dst, const char *chars, int len) bool nonCharacter; if (!headerdone && uc == 0xfeff) { // don't do anything, just skip the BOM - } else if (!(nonCharacter = isUnicodeNonCharacter(uc)) && uc > 0xffff && uc < 0x110000) { + } else if (!(nonCharacter = isUnicodeNonCharacter(uc)) && QChar::requiresSurrogates(uc) && uc < 0x110000) { // surrogate pair //Q_ASSERT((qch - (ushort*)result.unicode()) + 2 < result.length()); *qch++ = QChar::highSurrogate(uc); @@ -2102,7 +2102,7 @@ int fromUtf8_qt47_stateless(ushort *dst, const char *chars, int len) bool nonCharacter; if (!headerdone && uc == 0xfeff) { // don't do anything, just skip the BOM - } else if (!(nonCharacter = isUnicodeNonCharacter(uc)) && uc > 0xffff && uc < 0x110000) { + } else if (!(nonCharacter = isUnicodeNonCharacter(uc)) && QChar::requiresSurrogates(uc) && uc < 0x110000) { // surrogate pair //Q_ASSERT((qch - (ushort*)result.unicode()) + 2 < result.length()); *qch++ = QChar::highSurrogate(uc); @@ -2258,7 +2258,7 @@ static inline void extract_utf8_multibyte(ushort *&dst, const char *&chars, qptr // dst[counter] will correspond to chars[counter..counter+2], so adjust chars += 3; len -= 3; - if (trusted || (ucs >= 0x10000 && ucs < 0x110000 && !isUnicodeNonCharacter(ucs))) { + if (trusted || (QChar::requiresSurrogates(ucs) && ucs < 0x110000 && !isUnicodeNonCharacter(ucs))) { dst[counter + 0] = QChar::highSurrogate(ucs); dst[counter + 1] = QChar::lowSurrogate(ucs); counter += 2; -- 2.7.4