String Views: optimize the QByteArray/QString constructors
By using {QString,QByteArray}::begin() instead of data() (for const objectst); both begin() methods, respectively [1], return nullptr for a const null {QString,QByteArray}. Whereas data() returns &_empty if QT5_NULL_STRINGS is defined. [1] QByteArray::begin() since a116b2ddfc9e91736b1ec4edda6500e9c8f5f301 QString::begin() since 287ace562ee5ddff22f7dbf4e49ae5f0520f2308 [ChangeLog][QtCore][QLatin1StringView] Creation of QLatin1StringView from QByteArray or QByteArrayView now preserves the origin's isNull() status. This matches the behavior of creating a QUtf8StringView from those types. Change-Id: Icbdfe282e315c08ca6a17201bcd32c1a3bba8bf5 Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> (cherry picked from commit 221d8fdfb14c16472267320f859105f1a3834952) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
e43f4a7142
commit
f1e15832dd
@ -166,7 +166,7 @@ public:
|
||||
#else
|
||||
template <typename ByteArray, if_compatible_qbytearray_like<ByteArray> = true>
|
||||
QByteArrayView(const ByteArray &ba) noexcept
|
||||
: QByteArrayView(ba.isNull() ? nullptr : ba.data(), qsizetype(ba.size())) {}
|
||||
: QByteArrayView{ba.begin(), ba.size()} {}
|
||||
#endif
|
||||
|
||||
template <typename Container, if_compatible_container<Container> = true>
|
||||
|
@ -40,7 +40,7 @@ public:
|
||||
: QLatin1StringView(f, qsizetype(l - f)) {}
|
||||
constexpr QLatin1StringView(const char *s, qsizetype sz) noexcept : m_data(s), m_size(sz) {}
|
||||
explicit QLatin1StringView(const QByteArray &s) noexcept
|
||||
: QLatin1StringView(s.constData(), s.size()) {}
|
||||
: QLatin1StringView{s.begin(), s.size()} {}
|
||||
constexpr explicit QLatin1StringView(QByteArrayView s) noexcept
|
||||
: QLatin1StringView(s.constData(), s.size()) {}
|
||||
#else
|
||||
@ -52,7 +52,7 @@ public:
|
||||
constexpr QLatin1String(const char *f, const char *l)
|
||||
: QLatin1String(f, qsizetype(l - f)) {}
|
||||
constexpr QLatin1String(const char *s, qsizetype sz) noexcept : m_size(sz), m_data(s) {}
|
||||
explicit QLatin1String(const QByteArray &s) noexcept : m_size(s.size()), m_data(s.constData()) {}
|
||||
explicit QLatin1String(const QByteArray &s) noexcept : QLatin1String(s.begin(), s.size()) {}
|
||||
constexpr explicit QLatin1String(QByteArrayView s) noexcept : m_size(s.size()), m_data(s.data()) {}
|
||||
#endif // !Q_L1S_VIEW_IS_PRIMARY
|
||||
|
||||
|
@ -1213,9 +1213,9 @@ template <bool UseChar8T>
|
||||
//
|
||||
|
||||
QAnyStringView::QAnyStringView(const QByteArray &str) noexcept
|
||||
: QAnyStringView{str.isNull() ? nullptr : str.data(), str.size()} {}
|
||||
: QAnyStringView{str.begin(), str.size()} {}
|
||||
QAnyStringView::QAnyStringView(const QString &str) noexcept
|
||||
: QAnyStringView{str.isNull() ? nullptr : str.data(), str.size()} {}
|
||||
: QAnyStringView{str.begin(), str.size()} {}
|
||||
|
||||
QString QAnyStringView::toString() const
|
||||
{ return QtPrivate::convertToQString(*this); }
|
||||
|
@ -160,7 +160,7 @@ public:
|
||||
#else
|
||||
template <typename String, if_compatible_qstring_like<String> = true>
|
||||
QStringView(const String &str) noexcept
|
||||
: QStringView(str.isNull() ? nullptr : str.data(), qsizetype(str.size())) {}
|
||||
: QStringView{str.begin(), str.size()} {}
|
||||
#endif
|
||||
|
||||
template <typename Container, if_compatible_container<Container> = true>
|
||||
|
@ -181,7 +181,7 @@ public:
|
||||
#else
|
||||
template <typename String, if_compatible_qstring_like<String> = true>
|
||||
QBasicUtf8StringView(const String &str) noexcept
|
||||
: QBasicUtf8StringView(str.isNull() ? nullptr : str.data(), qsizetype(str.size())) {}
|
||||
: QBasicUtf8StringView{str.begin(), str.size()} {}
|
||||
#endif
|
||||
|
||||
template <typename Container, if_compatible_container<Container> = true>
|
||||
|
@ -272,12 +272,10 @@ void tst_QLatin1StringView::nullString()
|
||||
QVERIFY(null.isNull());
|
||||
|
||||
QLatin1StringView l1(null);
|
||||
QEXPECT_FAIL("", "null QByteArrays become non-null QLatin1Strings...", Continue);
|
||||
QCOMPARE(static_cast<const void*>(l1.data()), static_cast<const void*>(nullptr));
|
||||
QCOMPARE(l1.size(), 0);
|
||||
|
||||
QString s = l1;
|
||||
QEXPECT_FAIL("", "null QByteArrays become non-null QLatin1Strings become non-null QStrings...", Continue);
|
||||
QVERIFY(s.isNull());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user