Utils: Fix SmallString::append(QStringView string)

It used the size of the destination string but it had to use the size of
the appended string to inquire the maximum required size.

Change-Id: I1c910abab0ac60ed5fec1b3dc3a358e438532281
Reviewed-by: Vikas Pachdha <vikas.pachdha@qt.io>
Reviewed-by: Qt CI Patch Build Bot <ci_patchbuild_bot@qt.io>
This commit is contained in:
Marco Bubke
2023-07-20 11:07:30 +02:00
parent 601308edcd
commit 21983a8377
2 changed files with 36 additions and 6 deletions

View File

@@ -455,7 +455,7 @@ public:
constexpr size_type temporaryArraySize = Size * 6; constexpr size_type temporaryArraySize = Size * 6;
size_type oldSize = size(); size_type oldSize = size();
size_type maximumRequiredSize = static_cast<size_type>(encoder.requiredSpace(oldSize)); size_type maximumRequiredSize = static_cast<size_type>(encoder.requiredSpace(string.size()));
char *newEnd = nullptr; char *newEnd = nullptr;
if (maximumRequiredSize > temporaryArraySize) { if (maximumRequiredSize > temporaryArraySize) {

View File

@@ -9,6 +9,8 @@
#include <utils/smallstringio.h> #include <utils/smallstringio.h>
#include <utils/smallstringvector.h> #include <utils/smallstringvector.h>
#include <random>
using Utils::PathString; using Utils::PathString;
using Utils::SmallString; using Utils::SmallString;
using Utils::SmallStringLiteral; using Utils::SmallStringLiteral;
@@ -613,16 +615,44 @@ TEST(SmallString, to_q_string)
ASSERT_THAT(qStringText, QStringLiteral("short string")); ASSERT_THAT(qStringText, QStringLiteral("short string"));
} }
TEST(SmallString, from_q_string) class FromQString : public testing::TestWithParam<qsizetype>
{ {
QString qStringText = QStringLiteral("short string"); protected:
QString randomString(qsizetype size)
{
static constexpr char16_t characters[] = u"0123456789"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
auto text = SmallString::fromQString(qStringText); static std::mt19937 generator{std::random_device{}()};
static std::uniform_int_distribution<std::size_t> pick(0, std::size(characters) - 1);
ASSERT_THAT(text, SmallString("short string")); QString string;
string.reserve(size);
std::generate_n(std::back_inserter(string), size, [&]() {
return characters[pick(generator)];
});
return string;
}
protected:
qsizetype size = GetParam();
};
INSTANTIATE_TEST_SUITE_P(SmallString, FromQString, testing::Range<qsizetype>(0, 10000, 300));
TEST_P(FromQString, from_qstring)
{
const QString qStringText = randomString(size);
auto text = SmallString(qStringText);
ASSERT_THAT(text, qStringText.toStdString());
} }
TEST(SmallString, from_q_byte_array) TEST(SmallString, from_q_byte_array)
{ {
QByteArray qByteArray = QByteArrayLiteral("short string"); QByteArray qByteArray = QByteArrayLiteral("short string");