void QString::replace_helper(uint
*indices,
int nIndices,
int blen,
const QChar *after,
int alen
) {
// copy *after in case it lies inside our own d->data area
// (which we could possibly invalidate via a realloc or corrupt via memcpy operations.)
QChar *afterBuffer
= const_cast<QChar
*>
(after
);
if (after >= reinterpret_cast<QChar *>(d->data) && after < reinterpret_cast<QChar *>(d->data) + d->size) {
afterBuffer
= static_cast<QChar
*>
(qMalloc
(alen
*sizeof(QChar)));
Q_CHECK_PTR(afterBuffer);
::memcpy(afterBuffer, after, alen
*sizeof(QChar));
}
QT_TRY {
detach();
if (blen == alen) {
// replace in place
for (int i = 0; i < nIndices; ++i)
memcpy(d
->data
+ indices
[i
], afterBuffer, alen
* sizeof(QChar));
} else if (alen < blen) {
// replace from front
uint to = indices[0];
if (alen)
memcpy(d
->data
+to, after, alen
*sizeof(QChar));
to += alen;
uint movestart = indices[0] + blen;
for (int i = 1; i < nIndices; ++i) {
int msize = indices[i] - movestart;
if (msize > 0) {
memmove(d
->data
+ to, d
->data
+ movestart, msize
* sizeof(QChar));
to += msize;
}
if (alen) {
memcpy(d
->data
+ to, afterBuffer, alen
*sizeof(QChar));
to += alen;
}
movestart = indices[i] + blen;
}
int msize = d->size - movestart;
if (msize > 0)
memmove(d
->data
+ to, d
->data
+ movestart, msize
* sizeof(QChar));
resize(d->size - nIndices*(blen-alen));
} else {
// replace from back
int adjust = nIndices*(alen-blen);
int newLen = d->size + adjust;
int moveend = d->size;
resize(newLen);
while (nIndices) {
--nIndices;
int movestart = indices[nIndices] + blen;
int insertstart = indices[nIndices] + nIndices*(alen-blen);
int moveto = insertstart + alen;
memmove(d->data + moveto, d->data + movestart,
(moveend
- movestart
)*sizeof(QChar));
memcpy(d
->data
+ insertstart, afterBuffer, alen
*sizeof(QChar));
moveend = movestart-blen;
}
}
} QT_CATCH(const std::bad_alloc &) {
if (afterBuffer != after)
qFree(afterBuffer);
QT_RETHROW;
}
if (afterBuffer != after)
qFree(afterBuffer);
}
void QString::replace_helper(uint *indices, int nIndices, int blen, const QChar *after, int alen)
{
// copy *after in case it lies inside our own d->data area
// (which we could possibly invalidate via a realloc or corrupt via memcpy operations.)
QChar *afterBuffer = const_cast<QChar *>(after);
if (after >= reinterpret_cast<QChar *>(d->data) && after < reinterpret_cast<QChar *>(d->data) + d->size) {
afterBuffer = static_cast<QChar *>(qMalloc(alen*sizeof(QChar)));
Q_CHECK_PTR(afterBuffer);
::memcpy(afterBuffer, after, alen*sizeof(QChar));
}
QT_TRY {
detach();
if (blen == alen) {
// replace in place
for (int i = 0; i < nIndices; ++i)
memcpy(d->data + indices[i], afterBuffer, alen * sizeof(QChar));
} else if (alen < blen) {
// replace from front
uint to = indices[0];
if (alen)
memcpy(d->data+to, after, alen*sizeof(QChar));
to += alen;
uint movestart = indices[0] + blen;
for (int i = 1; i < nIndices; ++i) {
int msize = indices[i] - movestart;
if (msize > 0) {
memmove(d->data + to, d->data + movestart, msize * sizeof(QChar));
to += msize;
}
if (alen) {
memcpy(d->data + to, afterBuffer, alen*sizeof(QChar));
to += alen;
}
movestart = indices[i] + blen;
}
int msize = d->size - movestart;
if (msize > 0)
memmove(d->data + to, d->data + movestart, msize * sizeof(QChar));
resize(d->size - nIndices*(blen-alen));
} else {
// replace from back
int adjust = nIndices*(alen-blen);
int newLen = d->size + adjust;
int moveend = d->size;
resize(newLen);
while (nIndices) {
--nIndices;
int movestart = indices[nIndices] + blen;
int insertstart = indices[nIndices] + nIndices*(alen-blen);
int moveto = insertstart + alen;
memmove(d->data + moveto, d->data + movestart,
(moveend - movestart)*sizeof(QChar));
memcpy(d->data + insertstart, afterBuffer, alen*sizeof(QChar));
moveend = movestart-blen;
}
}
} QT_CATCH(const std::bad_alloc &) {
if (afterBuffer != after)
qFree(afterBuffer);
QT_RETHROW;
}
if (afterBuffer != after)
qFree(afterBuffer);
}
To copy to clipboard, switch view to plain text mode
Bookmarks