diff options
Diffstat (limited to 'src/corelib/tools/qstring.cpp')
-rw-r--r-- | src/corelib/tools/qstring.cpp | 106 |
1 files changed, 78 insertions, 28 deletions
diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index 3976f2cb6f..18c96c0b38 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -3158,6 +3158,15 @@ int QString::count(const QStringRef &str, Qt::CaseSensitivity cs) const \sa indexOf(), count() */ +/*! \fn bool QString::contains(QLatin1String str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const + \since 5.3 + + \overload contains() + + Returns \c true if this string contains an occurrence of the latin-1 string + \a str; otherwise returns \c false. +*/ + /*! \fn bool QString::contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const \overload contains() @@ -3937,13 +3946,9 @@ static inline __m128i mergeQuestionMarks(__m128i chunk) } #endif -static QByteArray toLatin1_helper(const QChar *data, int length) +static void toLatin1_helper(uchar *dst, const ushort *src, int length) { - QByteArray ba; if (length) { - ba.resize(length); - const ushort *src = reinterpret_cast<const ushort *>(data); - uchar *dst = (uchar*) ba.data(); #if defined(__SSE2__) if (length >= 16) { const int chunkCount = length >> 4; // divided by 16 @@ -3994,10 +3999,60 @@ static QByteArray toLatin1_helper(const QChar *data, int length) ++src; } } +} + +QByteArray QString::toLatin1_helper(const QString &string) +{ + if (Q_UNLIKELY(string.isNull())) + return QByteArray(); + + return toLatin1_helper(string.constData(), string.length()); +} + +QByteArray QString::toLatin1_helper(const QChar *data, int length) +{ + QByteArray ba(length, Qt::Uninitialized); + + // since we own the only copy, we're going to const_cast the constData; + // that avoids an unnecessary call to detach() and expansion code that will never get used + QT_PREPEND_NAMESPACE(toLatin1_helper)(reinterpret_cast<uchar *>(const_cast<char *>(ba.constData())), + reinterpret_cast<const ushort *>(data), length); return ba; } +QByteArray QString::toLatin1_helper_inplace(QString &s) +{ + if (!s.isDetached()) + return s.toLatin1(); + + // We can return our own buffer to the caller. + // Conversion to Latin-1 always shrinks the buffer by half. + const ushort *data = reinterpret_cast<const ushort *>(s.constData()); + uint length = s.size(); + + // Swap the d pointers. + // Kids, avert your eyes. Don't try this at home. + QArrayData *ba_d = s.d; + + // multiply the allocated capacity by sizeof(ushort) + ba_d->alloc *= sizeof(ushort); + + // reset ourselves to QString() + s.d = QString().d; + + // do the in-place conversion + uchar *dst = reinterpret_cast<uchar *>(ba_d->data()); + QT_PREPEND_NAMESPACE(toLatin1_helper)(dst, data, length); + dst[length] = '\0'; + + QByteArrayDataPtr badptr = { ba_d }; + return QByteArray(badptr); +} + + /*! + \fn QByteArray QString::toLatin1() const + Returns a Latin-1 representation of the string as a QByteArray. The returned byte array is undefined if the string contains non-Latin1 @@ -4006,10 +4061,6 @@ static QByteArray toLatin1_helper(const QChar *data, int length) \sa fromLatin1(), toUtf8(), toLocal8Bit(), QTextCodec */ -QByteArray QString::toLatin1() const -{ - return toLatin1_helper(unicode(), length()); -} /*! \fn QByteArray QString::toAscii() const @@ -4024,19 +4075,9 @@ QByteArray QString::toLatin1() const \sa fromAscii(), toLatin1(), toUtf8(), toLocal8Bit(), QTextCodec */ -#if !defined(Q_OS_MAC) && defined(Q_OS_UNIX) && !defined(QT_USE_ICU) -static QByteArray toLocal8Bit_helper(const QChar *data, int length) -{ -#ifndef QT_NO_TEXTCODEC - QTextCodec *localeCodec = QTextCodec::codecForLocale(); - if (localeCodec) - return localeCodec->fromUnicode(data, length); -#endif // QT_NO_TEXTCODEC - return toLatin1_helper(data, length); -} -#endif - /*! + \fn QByteArray QString::toLocal8Bit() const + Returns the local 8-bit representation of the string as a QByteArray. The returned byte array is undefined if the string contains characters not supported by the local 8-bit encoding. @@ -4051,17 +4092,21 @@ static QByteArray toLocal8Bit_helper(const QChar *data, int length) \sa fromLocal8Bit(), toLatin1(), toUtf8(), QTextCodec */ -QByteArray QString::toLocal8Bit() const + +QByteArray QString::toLocal8Bit_helper(const QChar *data, int size) { #ifndef QT_NO_TEXTCODEC QTextCodec *localeCodec = QTextCodec::codecForLocale(); if (localeCodec) - return localeCodec->fromUnicode(*this); + return localeCodec->fromUnicode(data, size); #endif // QT_NO_TEXTCODEC - return toLatin1(); + return toLatin1_helper(data, size); } + /*! + \fn QByteArray QString::toUtf8() const + Returns a UTF-8 representation of the string as a QByteArray. UTF-8 is a Unicode codec and can represent all characters in a Unicode @@ -4077,12 +4122,13 @@ QByteArray QString::toLocal8Bit() const \sa fromUtf8(), toLatin1(), toLocal8Bit(), QTextCodec */ -QByteArray QString::toUtf8() const + +QByteArray QString::toUtf8_helper(const QString &str) { - if (isNull()) + if (str.isNull()) return QByteArray(); - return QUtf8::convertFromUnicode(constData(), length(), 0); + return QUtf8::convertFromUnicode(str.constData(), str.length(), 0); } /*! @@ -5109,7 +5155,11 @@ int QString::localeAwareCompare_helper(const QChar *data1, int length1, return ucstrcmp(data1, length1, data2, length2); #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE) +#ifndef Q_OS_WINRT int res = CompareString(GetUserDefaultLCID(), 0, (wchar_t*)data1, length1, (wchar_t*)data2, length2); +#else + int res = CompareStringEx(LOCALE_NAME_USER_DEFAULT, 0, (LPCWSTR)data1, length1, (LPCWSTR)data2, length2, NULL, NULL, 0); +#endif switch (res) { case CSTR_LESS_THAN: @@ -9279,7 +9329,7 @@ static inline bool qt_ends_with(const QChar *haystack, int haystackLen, */ QByteArray QStringRef::toLatin1() const { - return toLatin1_helper(unicode(), length()); + return QString::toLatin1_helper(unicode(), length()); } /*! |