From 59fb33e0952f6d11993191b6f9ad612971447615 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Thu, 8 Dec 2022 18:58:55 +0200 Subject: [PATCH] QString: add convenience method converting latin-1 into a QVLA Less code duplication, as "convert Latin-1 data to Utf16 and store in a QVLA" is used in various places. Change-Id: I861f5b8f988411d400db0440b0f5a56c5365e990 Reviewed-by: Thiago Macieira --- src/corelib/text/qstring.cpp | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 030e583ddc..078ada91c1 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -996,6 +996,14 @@ Q_CORE_EXPORT void qt_from_latin1(char16_t *dst, const char *str, size_t size) n #endif } +static QVarLengthArray qt_from_latin1_to_qvla(QLatin1StringView str) noexcept +{ + const qsizetype len = str.size(); + QVarLengthArray arr(len); + qt_from_latin1(arr.data(), str.data(), len); + return arr; +} + template static void qt_to_latin1_internal(uchar *dst, const char16_t *src, qsizetype length) { @@ -3840,10 +3848,8 @@ QString &QString::replace(QLatin1StringView before, QLatin1StringView after, Qt: if (blen == 1 && alen == 1) return replace(before.front(), after.front(), cs); - QVarLengthArray a(alen); - QVarLengthArray b(blen); - qt_from_latin1(a.data(), after.latin1(), alen); - qt_from_latin1(b.data(), before.latin1(), blen); + QVarLengthArray a = qt_from_latin1_to_qvla(after); + QVarLengthArray b = qt_from_latin1_to_qvla(before); return replace((const QChar *)b.data(), blen, (const QChar *)a.data(), alen, cs); } @@ -3865,8 +3871,7 @@ QString &QString::replace(QLatin1StringView before, const QString &after, Qt::Ca if (blen == 1 && after.size() == 1) return replace(before.front(), after.front(), cs); - QVarLengthArray b(blen); - qt_from_latin1(b.data(), before.latin1(), blen); + QVarLengthArray b = qt_from_latin1_to_qvla(before); return replace((const QChar *)b.data(), blen, after.constData(), after.d.size, cs); } @@ -3888,8 +3893,7 @@ QString &QString::replace(const QString &before, QLatin1StringView after, Qt::Ca if (before.size() == 1 && alen == 1) return replace(before.front(), after.front(), cs); - QVarLengthArray a(alen); - qt_from_latin1(a.data(), after.latin1(), alen); + QVarLengthArray a = qt_from_latin1_to_qvla(after); return replace(before.constData(), before.d.size, (const QChar *)a.data(), alen, cs); } @@ -3911,12 +3915,10 @@ QString &QString::replace(QChar c, QLatin1StringView after, Qt::CaseSensitivity if (alen == 1) return replace(c, after.front(), cs); - QVarLengthArray a(alen); - qt_from_latin1(a.data(), after.latin1(), alen); + QVarLengthArray a = qt_from_latin1_to_qvla(after); return replace(&c, 1, (const QChar *)a.data(), alen, cs); } - /*! \fn bool QString::operator==(const QString &s1, const QString &s2) \overload operator==() @@ -8383,8 +8385,7 @@ QString QString::arg(QStringView a, int fieldWidth, QChar fillChar) const */ QString QString::arg(QLatin1StringView a, int fieldWidth, QChar fillChar) const { - QVarLengthArray utf16(a.size()); - qt_from_latin1(utf16.data(), a.data(), a.size()); + QVarLengthArray utf16 = qt_from_latin1_to_qvla(a); return arg(QStringView(utf16.data(), utf16.size()), fieldWidth, fillChar); } @@ -10655,9 +10656,7 @@ qsizetype QtPrivate::count(QStringView haystack, QLatin1StringView needle, Qt::C if (haystack.size() < needle.size()) return -1; - QVarLengthArray s(needle.size()); - qt_from_latin1(s.data(), needle.latin1(), size_t(needle.size())); - + QVarLengthArray s = qt_from_latin1_to_qvla(needle); return QtPrivate::count(haystack, QStringView(s.data(), s.size()), cs); } @@ -10840,8 +10839,7 @@ qsizetype QtPrivate::findString(QStringView haystack, qsizetype from, QLatin1Str if (haystack.size() < needle.size()) return -1; - QVarLengthArray s(needle.size()); - qt_from_latin1(s.data(), needle.latin1(), needle.size()); + QVarLengthArray s = qt_from_latin1_to_qvla(needle); return QtPrivate::findString(haystack, from, QStringView(reinterpret_cast(s.constData()), s.size()), cs); }