replace hardcoded values with a surrogate handling methods

Change-Id: Ib41e08d835f2e8ca2e32b4025c6f5a99840f2e27
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@nokia.com>
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
This commit is contained in:
Konstantin Ritt 2012-04-12 18:21:39 +03:00 committed by Qt by Nokia
parent b317fe2a60
commit 9fd2edb6da
8 changed files with 29 additions and 27 deletions

View File

@ -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<quint32>(tuple) : qFromLittleEndian<quint32>(tuple);
if (code >= 0x10000) {
if (QChar::requiresSurrogates(code)) {
*qch++ = QChar::highSurrogate(code);
*qch++ = QChar::lowSurrogate(code);
} else {

View File

@ -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<const QChar *>(&entry->mapping[0]), l);
else
str->replace(i-1, 2, reinterpret_cast<const QChar *>(&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);

View File

@ -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);

View File

@ -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 {

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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;