From 4d8484382248d7bd9e86ccf0f3847b7240a13418 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Sun, 6 Nov 2022 14:40:33 +0200 Subject: QString, QByteArray: add removeAt/First/Last() convenience methods Requested in codereview.qt-project.org/c/qt/qtbase/+/441770 [ChangeLog][QtCore][Text] Add removeAt/First/Last() convenience methods to QString and QByteArray Change-Id: I48a803e456e70cc51d51726a5e3aa7c125aedb1c Reviewed-by: Paul Wicking Reviewed-by: Thiago Macieira --- tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp | 17 +++++++++++++++++ tests/auto/corelib/text/qstring/tst_qstring.cpp | 15 +++++++++++++++ 2 files changed, 32 insertions(+) (limited to 'tests') diff --git a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp index 9794247965..34bee7bce8 100644 --- a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp +++ b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp @@ -53,6 +53,7 @@ private slots: void insertExtended(); void remove_data(); void remove(); + void remove_extra(); void removeIf(); void erase(); void erase_single_arg(); @@ -1070,6 +1071,22 @@ void tst_QByteArray::remove() QCOMPARE(ba2.remove(position, length), expected); } +void tst_QByteArray::remove_extra() +{ + QByteArray ba = "Clock"; + ba.removeFirst(); + QCOMPARE(ba, "lock"); + ba.removeLast(); + QCOMPARE(ba, "loc"); + ba.removeAt(ba.indexOf('o')); + QCOMPARE(ba, "lc"); + ba.clear(); + // No crash on empty byte arrays + ba.removeFirst(); + ba.removeLast(); + ba.removeAt(2); +} + void tst_QByteArray::removeIf() { auto removeA = [](const char c) { return c == 'a' || c == 'A'; }; diff --git a/tests/auto/corelib/text/qstring/tst_qstring.cpp b/tests/auto/corelib/text/qstring/tst_qstring.cpp index 230182889c..8b9e762d5d 100644 --- a/tests/auto/corelib/text/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/text/qstring/tst_qstring.cpp @@ -3452,6 +3452,21 @@ void tst_QString::remove_extra() s1.remove(0, 1); QCOMPARE(s1, s); } + + { + QString s = "Clock"; + s.removeFirst(); + QCOMPARE(s, "lock"); + s.removeLast(); + QCOMPARE(s, "loc"); + s.removeAt(s.indexOf('o')); + QCOMPARE(s, "lc"); + s.clear(); + // No crash on empty strings + s.removeFirst(); + s.removeLast(); + s.removeAt(2); + } } void tst_QString::erase_single_arg() -- cgit v1.2.3