Skip to content

Commit 57d91d8

Browse files
committed
QByteArray: make replace() consistent with QString's
Assuming QString has seen a lot more effort put into this, simply copy the preamble of QString::replace(ptr, n, ptr, n, cs) over to the corresponding QByteArray::replace(view, view) function, so they behave the same. On reviewer request, change d.size == 0 to isEmpty(), in both functions' preambles. [ChangeLog][QtCore][Important Behavior Changes][QByteArray] replace() is now consistent with QString::replace() in its treatment of empty haystacks and needles. Fixes: QTBUG-134079 Change-Id: I2366febee7de4646d6b33c39d2338760b58f2ec4 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
1 parent 61c2d3f commit 57d91d8

File tree

4 files changed

+11
-20
lines changed

4 files changed

+11
-20
lines changed

src/corelib/text/qbytearray.cpp

+10-3
Original file line numberDiff line numberDiff line change
@@ -2532,12 +2532,19 @@ QByteArray &QByteArray::replace(QByteArrayView before, QByteArrayView after)
25322532
const char *a = after.data();
25332533
qsizetype asize = after.size();
25342534

2535+
if (isEmpty()) {
2536+
if (bsize)
2537+
return *this;
2538+
} else {
2539+
if (b == a && bsize == asize)
2540+
return *this;
2541+
}
2542+
if (asize == 0 && bsize == 0)
2543+
return *this;
2544+
25352545
if (bsize == 1 && asize == 1)
25362546
return replace(*b, *a); // use the fast char-char algorithm
25372547

2538-
if (isNull() || (b == a && bsize == asize))
2539-
return *this;
2540-
25412548
// protect against before or after being part of this
25422549
std::string pinnedNeedle, pinnedReplacement;
25432550
if (QtPrivate::q_points_into_range(a, d)) {

src/corelib/text/qstring.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3864,7 +3864,7 @@ QString &QString::replace(const QChar *before, qsizetype blen,
38643864
const QChar *after, qsizetype alen,
38653865
Qt::CaseSensitivity cs)
38663866
{
3867-
if (d.size == 0) {
3867+
if (isEmpty()) {
38683868
if (blen)
38693869
return *this;
38703870
} else {

tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp

-8
Original file line numberDiff line numberDiff line change
@@ -1590,15 +1590,7 @@ void tst_QByteArray::replaceWithEmptyNeedleInsertsBeforeEachChar()
15901590
// shared
15911591
auto copy = haystack;
15921592
copy.replace(needle, replacement);
1593-
if (isByteArray) {
1594-
QEXPECT_FAIL("/<null>/<null>/a/", "QTBUG-134079", Continue);
1595-
QEXPECT_FAIL("/<null>//a/", "QTBUG-134079", Continue);
1596-
}
15971593
QCOMPARE(copy.isNull(), result.isNull());
1598-
if (isByteArray) {
1599-
QEXPECT_FAIL("/<null>/<null>/a/", "QTBUG-134079", Continue);
1600-
QEXPECT_FAIL("/<null>//a/", "QTBUG-134079", Continue);
1601-
}
16021594
QCOMPARE(copy, result);
16031595
}
16041596
{

tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp

-8
Original file line numberDiff line numberDiff line change
@@ -2543,19 +2543,11 @@ void tst_QStringApiSymmetry::replace_split_impl() const
25432543
{
25442544
auto copy = haystack;
25452545
copy.replace(needle, replacement);
2546-
if constexpr (std::is_same_v<Haystack, QByteArray>) {
2547-
QEXPECT_FAIL("null ~= null$", "QTBUG-134079", Continue);
2548-
QEXPECT_FAIL("null ~= empty$", "QTBUG-134079", Continue);
2549-
}
25502546
QCOMPARE(copy, result);
25512547
}
25522548
{
25532549
auto copy = detached(haystack);
25542550
copy.replace(needle, replacement);
2555-
if constexpr (std::is_same_v<Haystack, QByteArray>) {
2556-
QEXPECT_FAIL("null ~= null$", "QTBUG-134079", Continue);
2557-
QEXPECT_FAIL("null ~= empty$", "QTBUG-134079", Continue);
2558-
}
25592551
QCOMPARE(copy, result);
25602552
}
25612553
}

0 commit comments

Comments
 (0)