summaryrefslogtreecommitdiffstats
path: root/qmake
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2020-09-09 12:58:25 +0200
committerLars Knoll <lars.knoll@qt.io>2020-09-10 17:36:10 +0200
commitc4dbd1cddca21b5fec8b8ffdc971b895b156f33f (patch)
tree39b196a344361af87db8b75cf0370cbf60b009b8 /qmake
parentac310d5b23b56a217c408f8ee549c36f73c7a437 (diff)
Cleanup ProString::append/prepend
Don't use evil hacks that make assumptions about QString internals. Change-Id: I663602d197f0fcf62886dbfb9a87547097cdab04 Reviewed-by: Andrei Golubev <andrei.golubev@qt.io> Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io> Reviewed-by: Kai Koehne <kai.koehne@qt.io>
Diffstat (limited to 'qmake')
-rw-r--r--qmake/library/proitems.cpp92
-rw-r--r--qmake/library/proitems.h1
2 files changed, 38 insertions, 55 deletions
diff --git a/qmake/library/proitems.cpp b/qmake/library/proitems.cpp
index db19794bec..c31b3e764f 100644
--- a/qmake/library/proitems.cpp
+++ b/qmake/library/proitems.cpp
@@ -160,51 +160,18 @@ QString &ProString::toQString(QString &tmp) const
return tmp;
}
-/*
- * \brief ProString::prepareExtend
- * \param extraLen number of new characters to be added
- * \param thisTarget offset to which current contents should be moved
- * \param extraTarget offset at which new characters will be added
- * \return pointer to storage location for new characters
- *
- * Prepares the string for adding new characters.
- * If the string is detached and has enough space, it will be changed in place.
- * Otherwise, it will be replaced with a new string object, thus detaching.
- * In either case, the hash will be reset.
- */
-QChar *ProString::prepareExtend(int extraLen, int thisTarget, int extraTarget)
-{
- if (m_string.isDetached() && m_length + extraLen <= m_string.capacity()) {
- m_string.reserve(0); // Prevent the resize() below from reallocating
- QChar *ptr = (QChar *)m_string.constData();
- if (m_offset != thisTarget)
- memmove(ptr + thisTarget, ptr + m_offset, m_length * 2);
- ptr += extraTarget;
- m_offset = 0;
- m_length += extraLen;
- m_string.resize(m_length);
- m_hash = 0x80000000;
- return ptr;
- } else {
- QString neu(m_length + extraLen, Qt::Uninitialized);
- QChar *ptr = (QChar *)neu.constData();
- memcpy(ptr + thisTarget, m_string.constData() + m_offset, m_length * 2);
- ptr += extraTarget;
- *this = ProString(neu);
- return ptr;
- }
-}
-
ProString &ProString::prepend(const ProString &other)
{
if (other.m_length) {
if (!m_length) {
*this = other;
} else {
- QChar *ptr = prepareExtend(other.m_length, other.m_length, 0);
- memcpy(ptr, other.constData(), other.m_length * 2);
+ m_string = other.toQStringView() + toQStringView();
+ m_offset = 0;
+ m_length = m_string.length();
if (!m_file)
m_file = other.m_file;
+ m_hash = 0x80000000;
}
}
return *this;
@@ -212,20 +179,33 @@ ProString &ProString::prepend(const ProString &other)
ProString &ProString::append(const QLatin1String other)
{
- const char *latin1 = other.latin1();
- int size = other.size();
- if (size) {
- QChar *ptr = prepareExtend(size, 0, m_length);
- for (int i = 0; i < size; i++)
- *ptr++ = QLatin1Char(latin1[i]);
+ if (other.size()) {
+ if (m_length != m_string.length()) {
+ m_string = toQStringView() + other;
+ m_offset = 0;
+ m_length = m_string.length();
+ } else {
+ Q_ASSERT(m_offset == 0);
+ m_string.append(other);
+ m_length += other.size();
+ }
+ m_hash = 0x80000000;
}
return *this;
}
ProString &ProString::append(QChar other)
{
- QChar *ptr = prepareExtend(1, 0, m_length);
- *ptr = other;
+ if (m_length != m_string.length()) {
+ m_string = toQStringView() + other;
+ m_offset = 0;
+ m_length = m_string.length();
+ } else {
+ Q_ASSERT(m_offset == 0);
+ m_string.append(other);
+ ++m_length;
+ }
+ m_hash = 0x80000000;
return *this;
}
@@ -236,16 +216,18 @@ ProString &ProString::append(const ProString &other, bool *pending)
if (!m_length) {
*this = other;
} else {
- QChar *ptr;
+ if (m_length != m_string.length())
+ m_string = toQString();
if (pending && !*pending) {
- ptr = prepareExtend(1 + other.m_length, 0, m_length);
- *ptr++ = QLatin1Char(' ');
+ m_string += QLatin1Char(' ') + other.toQStringView();
} else {
- ptr = prepareExtend(other.m_length, 0, m_length);
+ m_string += other.toQStringView();
}
- memcpy(ptr, other.m_string.constData() + other.m_offset, other.m_length * 2);
+ m_length = m_string.length();
+ m_offset = 0;
if (other.m_file)
m_file = other.m_file;
+ m_hash = 0x80000000;
}
if (pending)
*pending = true;
@@ -274,18 +256,20 @@ ProString &ProString::append(const ProStringList &other, bool *pending, bool ski
else
totalLength--;
- QChar *ptr = prepareExtend(totalLength, 0, m_length);
+ m_string = toQString();
+ m_offset = 0;
for (int i = startIdx; i < sz; ++i) {
if (putSpace)
- *ptr++ = QLatin1Char(' ');
+ m_string += QLatin1Char(' ');
else
putSpace = true;
const ProString &str = other.at(i);
- memcpy(ptr, str.m_string.constData() + str.m_offset, str.m_length * 2);
- ptr += str.m_length;
+ m_string += str.toQStringView();
}
+ m_length = m_string.length();
if (other.last().m_file)
m_file = other.last().m_file;
+ m_hash = 0x80000000;
}
if (pending)
*pending = true;
diff --git a/qmake/library/proitems.h b/qmake/library/proitems.h
index 24412098d6..e875e839ff 100644
--- a/qmake/library/proitems.h
+++ b/qmake/library/proitems.h
@@ -185,7 +185,6 @@ private:
int m_offset, m_length;
int m_file;
mutable uint m_hash;
- QChar *prepareExtend(int extraLen, int thisTarget, int extraTarget);
uint updatedHash() const;
friend size_t qHash(const ProString &str);
friend QString operator+(const ProString &one, const ProString &two);