OSDN Git Service

optimal buffer usage
authorIvailo Monev <xakepa10@gmail.com>
Thu, 25 Mar 2021 16:54:56 +0000 (18:54 +0200)
committerIvailo Monev <xakepa10@gmail.com>
Thu, 25 Mar 2021 17:38:03 +0000 (19:38 +0200)
Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
src/core/codecs/qicucodec.cpp
src/core/io/qurl.cpp
src/core/tools/qchar.cpp
src/core/tools/qlocale.cpp
src/core/tools/qstring.cpp
src/network/kernel/qcryptographichash.cpp

index ba62360..239b97e 100644 (file)
@@ -952,20 +952,18 @@ QString QIcuCodec::convertToUnicode(const char *src, int length,
     UConverter *conv = getConverter(state);
     Q_ASSERT(conv);
 
-    QString result(QMAXUSTRLEN(length), Qt::Uninitialized);
+    const int maxchars = QMAXUSTRLEN(length);
+    UChar result[maxchars];
+    int resultoffset = 0;
     UErrorCode error = U_ZERO_ERROR;
-    const int convresult = ucnv_toUChars(conv, reinterpret_cast<UChar *>(result.data()),
-        result.length(), src, length, &error);
-    if (Q_LIKELY(U_SUCCESS(error))) {
-        result.resize(convresult);
-        if (state && state->flags & QTextCodec::IgnoreHeader) {
-            // ICU always generates BOMs when converter is UTF-32/UTF-16 so no check is done to
-            // verify that, unless someone makes it an option
-            if (ucnv_compareNames(m_name.constData(), "UTF-32")) {
-                result.remove(0, 4);
-            } else if (ucnv_compareNames(m_name.constData(), "UTF-16")) {
-                result.remove(0, 2);
-            }
+    const int convresult = ucnv_toUChars(conv, result, maxchars, src, length, &error);
+    if (state && state->flags & QTextCodec::IgnoreHeader) {
+        // ICU always generates BOMs when converter is UTF-32/UTF-16 so no check is done to
+        // verify that, unless someone makes it an option
+        if (ucnv_compareNames(m_name.constData(), "UTF-32")) {
+            resultoffset = 4;
+        } else if (ucnv_compareNames(m_name.constData(), "UTF-16")) {
+            resultoffset = 2;
         }
     }
 
@@ -982,10 +980,15 @@ QString QIcuCodec::convertToUnicode(const char *src, int length,
     }
 #endif
 
-    if (!state)
+    if (!state) {
         ucnv_close(conv);
+    }
+
+    if (Q_LIKELY(U_SUCCESS(error))) {
+        return QString(reinterpret_cast<QChar*>(result) + resultoffset, convresult);
+    }
 
-    return result;
+    return QString();
 }
 
 QByteArray QIcuCodec::convertFromUnicode(const QChar *unicode, int length,
@@ -995,13 +998,10 @@ QByteArray QIcuCodec::convertFromUnicode(const QChar *unicode, int length,
     Q_ASSERT(conv);
 
     const int maxbytes = UCNV_GET_MAX_BYTES_FOR_STRING(length, ucnv_getMaxCharSize(conv));
-    QByteArray result(maxbytes, Qt::Uninitialized);
+    char result[maxbytes];
     UErrorCode error = U_ZERO_ERROR;
-    const int convresult = ucnv_fromUChars(conv, result.data(), result.length(),
+    const int convresult = ucnv_fromUChars(conv, result, maxbytes,
         reinterpret_cast<const UChar *>(unicode), length, &error);
-    if (Q_LIKELY(U_SUCCESS(error))) {
-        result.resize(convresult);
-    }
 
 #if 0
     if (state) {
@@ -1013,10 +1013,15 @@ QByteArray QIcuCodec::convertFromUnicode(const QChar *unicode, int length,
     }
 #endif
 
-    if (!state)
+    if (!state) {
         ucnv_close(conv);
+    }
+
+    if (Q_LIKELY(U_SUCCESS(error))) {
+        return QByteArray(result, convresult);
+    }
 
-    return result;
+    return QByteArray();
 }
 
 #ifndef QT_NO_TEXTCODEC
index 62a68ff..75b5d8f 100644 (file)
@@ -3485,19 +3485,18 @@ QString QUrl::fromAce(const QByteArray &domain)
     const QString utf8 = QString::fromUtf8(domain);
     UErrorCode error = U_ZERO_ERROR;
     UIDNAInfo info = UIDNA_INFO_INITIALIZER;
-    QString result(utf8.size() * 4, Qt::Uninitialized);
+    const int maxlength = utf8.size() * 4;
+    UChar result[maxlength];
     const int idnaresult = uidna_nameToUnicode(globalidna,
         reinterpret_cast<const UChar*>(utf8.unicode()), utf8.size(),
-        reinterpret_cast<UChar*>(result.data()), result.size(),
-        &info, &error);
+        result, maxlength, &info, &error);
 
     if (Q_UNLIKELY(U_FAILURE(error) && info.errors != 0)) {
         qWarning("QUrl::fromAce: failed %s", u_errorName(error));
         return utf8;
     }
 
-    result.resize(idnaresult);
-    return result;
+    return QString(reinterpret_cast<QChar*>(result), idnaresult);
 }
 
 /*!
@@ -3530,19 +3529,18 @@ QByteArray QUrl::toAce(const QString &domain)
 
     UErrorCode error = U_ZERO_ERROR;
     UIDNAInfo info = UIDNA_INFO_INITIALIZER;
-    QString result(domain.size() * 4, Qt::Uninitialized);
+    const int maxlength = domain.size() * 4;
+    UChar result[maxlength];
     const int idnaresult = uidna_nameToASCII(globalidna,
         reinterpret_cast<const UChar*>(domain.unicode()), domain.size(),
-        reinterpret_cast<UChar*>(result.data()), result.size(),
-        &info, &error);
+        result, maxlength, &info, &error);
 
     if (Q_UNLIKELY(U_FAILURE(error) && info.errors != 0)) {
         qWarning("QUrl::toAce: failed %s", u_errorName(error));
         return QByteArray();
     }
 
-    result.resize(idnaresult);
-    return result.toLatin1();
+    return QString(reinterpret_cast<QChar*>(result), idnaresult).toLatin1();
 }
 
 /*!
index aec1750..d6b3cd2 100644 (file)
@@ -1148,9 +1148,9 @@ QString QChar::decomposition(const uint ucs4)
     }
 
     errorcode = U_ZERO_ERROR;
-    QString result(4, Qt::Uninitialized);
-    const int decresult = unorm2_getDecomposition(normalizer, ucs4,
-        reinterpret_cast<UChar*>(result.data()), result.size(), &errorcode);
+    static const int maxlength = 4;
+    UChar result[maxlength];
+    const int decresult = unorm2_getDecomposition(normalizer, ucs4, result, maxlength, &errorcode);
     if (Q_UNLIKELY(U_FAILURE(errorcode))) {
         qWarning("QChar::decomposition: unorm2_getDecomposition() failed %s", u_errorName(errorcode));
         return QString();
@@ -1159,8 +1159,7 @@ QString QChar::decomposition(const uint ucs4)
         return QString();
     }
 
-    result.resize(decresult);
-    return result;
+    return QString(reinterpret_cast<QChar*>(result), decresult);
 }
 
 /*!
index edc6855..a2d2298 100644 (file)
@@ -146,8 +146,7 @@ QString QLocalePrivate::bcp47Name() const
     const int countrylen = qstrlen(country);
     const int totallen = langlen + scriptlen + countrylen + (script ? 1 : 0) + (country ? 1 : 0);
 
-    QString name(totallen, Qt::Uninitialized);
-    QChar *uc = name.data();
+    QChar uc[totallen];
     int datapos = 0;
     for (int i = 0; i < langlen; i++) {
         uc[datapos] = ushort(lang[i]);
@@ -168,7 +167,7 @@ QString QLocalePrivate::bcp47Name() const
         }
     }
 
-    return name;
+    return QString(uc, totallen);
 }
 
 static const QLocalePrivate *systemPrivate()
index 104c2b2..361cc80 100644 (file)
@@ -3281,16 +3281,12 @@ bool QString::endsWith(const QChar &c, Qt::CaseSensitivity cs) const
 
 static QByteArray toLatin1_helper(const QChar *data, int length)
 {
-    QByteArray ba(length, Qt::Uninitialized);
-    if (length) {
-        const ushort *src = reinterpret_cast<const ushort *>(data);
-        uchar *dst = (uchar*) ba.data();
-        while (length--) {
-            *dst++ = (*src>0xff) ? '?' : (uchar) *src;
-            ++src;
-        }
+    char result[length];
+    for (int i = 0; i < length; i++) {
+        const ushort ucs = data[i].unicode();
+        result[i] = (ucs > 0xff) ? '?' : char(ucs);
     }
-    return ba;
+    return QByteArray(result, length);
 }
 
 /*!
@@ -5673,17 +5669,17 @@ QString QString::normalized(QString::NormalizationForm mode) const
     }
 
     error = U_ZERO_ERROR;
-    QString result(QMAXUSTRLEN(size()), Qt::Uninitialized);
+    const int maxlength = QMAXUSTRLEN(size());
+    UChar result[maxlength];
     const int decresult = unorm2_normalize(normalizer,
         reinterpret_cast<const UChar*>(unicode()), size(),
-        reinterpret_cast<UChar*>(result.data()), result.size(), &error);
+        result, maxlength, &error);
     if (Q_UNLIKELY(U_FAILURE(error))) {
         qWarning("QString::normalized: unorm2_normalize() failed %s", u_errorName(error));
         return QString();
     }
 
-    result.resize(decresult);
-    return result;
+    return QString(reinterpret_cast<QChar*>(result), decresult);
 }
 
 /*!
index 0bb052a..e7f1053 100644 (file)
@@ -253,60 +253,60 @@ QByteArray QCryptographicHash::hash(const QByteArray &data, QCryptographicHash::
 {
     switch (method) {
         case QCryptographicHash::Md4: {
-            QByteArray result(MD4_DIGEST_LENGTH, Qt::Uninitialized);
+            unsigned char result[MD4_DIGEST_LENGTH];
             MD4_CTX md4Context;
             MD4_Init(&md4Context);
             MD4_Update(&md4Context, data.constData(), data.length());
-            MD4_Final(reinterpret_cast<unsigned char *>(result.data()), &md4Context);
-            return result;
+            MD4_Final(result, &md4Context);
+            return QByteArray(reinterpret_cast<char *>(result), MD4_DIGEST_LENGTH);
         }
         case QCryptographicHash::Md5: {
-            QByteArray result(MD5_DIGEST_LENGTH, Qt::Uninitialized);
+            unsigned char result[MD5_DIGEST_LENGTH];
             MD5_CTX md5Context;
             MD5_Init(&md5Context);
             MD5_Update(&md5Context, data.constData(), data.length());
-            MD5_Final(reinterpret_cast<unsigned char *>(result.data()), &md5Context);
-            return result;
+            MD5_Final(result, &md5Context);
+            return QByteArray(reinterpret_cast<char *>(result), MD5_DIGEST_LENGTH);
         }
         case QCryptographicHash::Sha1: {
-            QByteArray result(SHA_DIGEST_LENGTH, Qt::Uninitialized);
+            unsigned char result[SHA_DIGEST_LENGTH];
             SHA_CTX sha1Context;
             SHA1_Init(&sha1Context);
             SHA1_Update(&sha1Context, data.constData(), data.length());
-            SHA1_Final(reinterpret_cast<unsigned char *>(result.data()), &sha1Context);
-            return result;
+            SHA1_Final(result, &sha1Context);
+            return QByteArray(reinterpret_cast<char *>(result), SHA_DIGEST_LENGTH);
         }
         case QCryptographicHash::Sha224: {
-            QByteArray result(SHA224_DIGEST_LENGTH, Qt::Uninitialized);
+            unsigned char result[SHA224_DIGEST_LENGTH];
             SHA256_CTX sha224Context;
             SHA224_Init(&sha224Context);
             SHA224_Update(&sha224Context, data.constData(), data.length());
-            SHA224_Final(reinterpret_cast<unsigned char *>(result.data()), &sha224Context);
-            return result;
+            SHA224_Final(result, &sha224Context);
+            return QByteArray(reinterpret_cast<char *>(result), SHA224_DIGEST_LENGTH);
         }
         case QCryptographicHash::Sha256: {
-            QByteArray result(SHA256_DIGEST_LENGTH, Qt::Uninitialized);
+            unsigned char result[SHA256_DIGEST_LENGTH];
             SHA256_CTX sha256Context;
             SHA256_Init(&sha256Context);
             SHA256_Update(&sha256Context, data.constData(), data.length());
-            SHA256_Final(reinterpret_cast<unsigned char *>(result.data()), &sha256Context);
-            return result;
+            SHA256_Final(result, &sha256Context);
+            return QByteArray(reinterpret_cast<char *>(result), SHA256_DIGEST_LENGTH);
         }
         case QCryptographicHash::Sha384: {
-            QByteArray result(SHA384_DIGEST_LENGTH, Qt::Uninitialized);
+            unsigned char result[SHA384_DIGEST_LENGTH];
             SHA512_CTX sha384Context;
             SHA384_Init(&sha384Context);
             SHA384_Update(&sha384Context, data.constData(), data.length());
-            SHA384_Final(reinterpret_cast<unsigned char *>(result.data()), &sha384Context);
-            return result;
+            SHA384_Final(result, &sha384Context);
+            return QByteArray(reinterpret_cast<char *>(result), SHA384_DIGEST_LENGTH);
         }
         case QCryptographicHash::Sha512: {
-            QByteArray result(SHA512_DIGEST_LENGTH, Qt::Uninitialized);
+            unsigned char result[SHA512_DIGEST_LENGTH];
             SHA512_CTX sha512Context;
             SHA512_Init(&sha512Context);
             SHA512_Update(&sha512Context, data.constData(), data.length());
-            SHA512_Final(reinterpret_cast<unsigned char *>(result.data()), &sha512Context);
-            return result;
+            SHA512_Final(result, &sha512Context);
+            return QByteArray(reinterpret_cast<char *>(result), SHA512_DIGEST_LENGTH);
         }
     }