QLatin1String: Add a constructor taking QByteArrayView

Change-Id: Ie90645486431d7af3fe8128417b0fb6bd02a88b5
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Mårten Nordheim 2021-06-10 16:52:17 +02:00
parent 77d62727d0
commit e0ae1af278
3 changed files with 46 additions and 0 deletions

View File

@ -8910,6 +8910,20 @@ QString &QString::setRawData(const QChar *unicode, qsizetype size)
\sa latin1()
*/
/*! \fn QLatin1String::QLatin1String(QByteArrayView str)
\since 6.3
Constructs a QLatin1String object that stores \a str.
The string data is \e not copied. The caller must be able to
guarantee that the data which \a str is pointing to will not
be deleted or modified as long as the QLatin1String object
exists. The size is obtained from \a str as-is, without checking
for a null-terminator.
\sa latin1()
*/
/*!
\fn QString QLatin1String::toString() const
\since 6.0

View File

@ -89,6 +89,7 @@ public:
: QLatin1String(f, qsizetype(l - f)) {}
constexpr inline explicit QLatin1String(const char *s, qsizetype sz) noexcept : m_size(sz), m_data(s) {}
explicit QLatin1String(const QByteArray &s) noexcept : m_size(qsizetype(qstrnlen(s.constData(), s.size()))), m_data(s.constData()) {}
constexpr explicit QLatin1String(QByteArrayView s) noexcept : m_size(s.size()), m_data(s.data()) {}
inline QString toString() const;

View File

@ -44,6 +44,7 @@ class tst_QLatin1String : public QObject
Q_OBJECT
private Q_SLOTS:
void construction();
void at();
void arg() const;
void midLeftRight();
@ -54,6 +55,36 @@ private Q_SLOTS:
void relationalOperators();
};
void tst_QLatin1String::construction()
{
{
const char str[6] = "hello";
QLatin1String l1s(str);
QCOMPARE(l1s.size(), 5);
QCOMPARE(l1s.latin1(), reinterpret_cast<const void *>(&str[0]));
QCOMPARE(l1s.latin1(), "hello");
QByteArrayView helloView(str);
helloView = helloView.first(4);
l1s = QLatin1String(helloView);
QCOMPARE(l1s.latin1(), helloView.data());
QCOMPARE(l1s.latin1(), reinterpret_cast<const void *>(helloView.data()));
QCOMPARE(l1s.size(), helloView.size());
}
{
const QByteArray helloArray("hello");
QLatin1String l1s(helloArray);
QCOMPARE(l1s.latin1(), helloArray.data());
QCOMPARE(l1s.size(), helloArray.size());
QByteArrayView helloView(helloArray);
helloView = helloView.first(4);
l1s = QLatin1String(helloView);
QCOMPARE(l1s.latin1(), helloView.data());
QCOMPARE(l1s.size(), helloView.size());
}
}
void tst_QLatin1String::at()
{