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;
}
}
}
#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,
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) {
}
#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
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);
}
/*!
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();
}
/*!
}
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();
return QString();
}
- result.resize(decresult);
- return result;
+ return QString(reinterpret_cast<QChar*>(result), decresult);
}
/*!
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]);
}
}
- return name;
+ return QString(uc, totallen);
}
static const QLocalePrivate *systemPrivate()
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);
}
/*!
}
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);
}
/*!
{
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);
}
}