diff options
-rw-r--r-- | src/corelib/text/qstring.cpp | 14 | ||||
-rw-r--r-- | src/corelib/text/qstring.h | 1 | ||||
-rw-r--r-- | tests/auto/corelib/text/qlatin1string/tst_qlatin1string.cpp | 31 |
3 files changed, 46 insertions, 0 deletions
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 3e4d95188d..a8f0c0b142 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -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 diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index e17bcbfbf6..cce57cd578 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -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; diff --git a/tests/auto/corelib/text/qlatin1string/tst_qlatin1string.cpp b/tests/auto/corelib/text/qlatin1string/tst_qlatin1string.cpp index 41c4f26c2c..039a024d64 100644 --- a/tests/auto/corelib/text/qlatin1string/tst_qlatin1string.cpp +++ b/tests/auto/corelib/text/qlatin1string/tst_qlatin1string.cpp @@ -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() { |