QLatin1String: add missing APIs for compatibility with Qt string views

As a drive-by, fixed misleading wording used in docs.

[ChangeLog][QtCore][Potentially Source-Incompatible Changes][QLatin1String]
Added QLatin1String(std::nullptr_t) constructor, which makes
QLatin1String(0) call ambiguous. To fix the ambiguity, nullptr
must be passed instead of 0.

Task-number: QTBUG-98433
Change-Id: I2b888aa23469343d78aa640dc39a6028b77165dd
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
This commit is contained in:
Sona Kurazyan 2022-02-24 14:26:05 +01:00
parent 0e64165c2e
commit 736213bf66
3 changed files with 127 additions and 28 deletions

View File

@ -9108,6 +9108,14 @@ QString &QString::setRawData(const QChar *unicode, qsizetype size)
\sa data(), isEmpty(), isNull(), {Distinction Between Null and Empty Strings}
*/
/*! \fn QLatin1String::QLatin1String(std::nullptr_t)
\since 6.4
Constructs a QLatin1String object that stores a \nullptr.
\sa data(), isEmpty(), isNull(), {Distinction Between Null and Empty Strings}
*/
/*! \fn QLatin1String::QLatin1String(const char *str)
Constructs a QLatin1String object that stores \a str.
@ -9193,27 +9201,45 @@ QString &QString::setRawData(const QChar *unicode, qsizetype size)
/*! \fn const char *QLatin1String::latin1() const
Returns the Latin-1 string stored in this object.
Returns the start of the Latin-1 string referenced by this object.
*/
/*! \fn const char *QLatin1String::data() const
Returns the Latin-1 string stored in this object.
Returns the start of the Latin-1 string referenced by this object.
*/
/*! \fn const char *QLatin1String::constData() const
\since 6.4
Returns the start of the Latin-1 string referenced by this object.
This function is provided for compatibility with other Qt containers.
\sa data()
*/
/*! \fn qsizetype QLatin1String::size() const
Returns the size of the Latin-1 string stored in this object.
Returns the size of the Latin-1 string referenced by this object.
\note In version prior to Qt 6, this function returned \c{int},
restricting the amount of data that could be held in a QLatin1String
on 64-bit architectures.
*/
/*! \fn qsizetype QLatin1String::length() const
\since 6.4
Same as size().
This function is provided for compatibility with other Qt containers.
*/
/*! \fn bool QLatin1String::isNull() const
\since 5.10
Returns whether the Latin-1 string stored in this object is null
Returns whether the Latin-1 string referenced by this object is null
(\c{data() == nullptr}) or not.
\sa isEmpty(), data()
@ -9222,12 +9248,23 @@ QString &QString::setRawData(const QChar *unicode, qsizetype size)
/*! \fn bool QLatin1String::isEmpty() const
\since 5.10
Returns whether the Latin-1 string stored in this object is empty
Returns whether the Latin-1 string referenced by this object is empty
(\c{size() == 0}) or not.
\sa isNull(), size()
*/
/*! \fn bool QLatin1String::empty() const
\since 6.4
Returns whether the Latin-1 string referenced by this object is empty
(\c{size() == 0}) or not.
This function is provided for STL compatibility.
\sa isEmpty(), isNull(), size()
*/
/*! \fn QLatin1Char QLatin1String::at(qsizetype pos) const
\since 5.8
@ -9265,6 +9302,21 @@ QString &QString::setRawData(const QChar *unicode, qsizetype size)
\sa back(), at(), operator[]()
*/
/*!
\fn QLatin1Char QLatin1String::first() const
\since 6.4
Returns the first character in the string.
Same as \c{at(0)} or front().
This function is provided for compatibility with other Qt containers.
\warning Calling this function on an empty string constitutes
undefined behavior.
\sa last(), front(), back()
*/
/*!
\fn QLatin1Char QLatin1String::back() const
\since 5.10
@ -9280,6 +9332,21 @@ QString &QString::setRawData(const QChar *unicode, qsizetype size)
\sa front(), at(), operator[]()
*/
/*!
\fn QLatin1Char QLatin1String::last() const
\since 6.4
Returns the last character in the string.
Same as \c{at(size() - 1)} or back().
This function is provided for compatibility with other Qt containers.
\warning Calling this function on an empty string constitutes
undefined behavior.
\sa first(), back(), front()
*/
/*!
\fn int QLatin1String::compare(QStringView str, Qt::CaseSensitivity cs) const
\fn int QLatin1String::compare(QLatin1String l1, Qt::CaseSensitivity cs) const
@ -9446,6 +9513,17 @@ QString &QString::setRawData(const QChar *unicode, qsizetype size)
\sa cend(), begin(), crbegin(), data()
*/
/*!
\fn QLatin1String::const_iterator QLatin1String::constBegin() const
\since 6.4
Same as begin().
This function is provided for compatibility with other Qt containers.
\sa constEnd(), begin(), cbegin(), data()
*/
/*!
\fn QLatin1String::const_iterator QLatin1String::end() const
\since 5.10
@ -9468,6 +9546,16 @@ QString &QString::setRawData(const QChar *unicode, qsizetype size)
\sa cbegin(), end(), crend()
*/
/*! \fn QLatin1String::const_iterator QLatin1String::constEnd() const
\since 6.4
Same as end().
This function is provided for compatibility with other Qt containers.
\sa constBegin(), end(), cend(), crend()
*/
/*!
\fn QLatin1String::const_reverse_iterator QLatin1String::rbegin() const
\since 5.10

View File

@ -84,6 +84,7 @@ class QLatin1String
{
public:
constexpr inline QLatin1String() noexcept : m_size(0), m_data(nullptr) {}
constexpr QLatin1String(std::nullptr_t) noexcept : QLatin1String() {}
constexpr inline explicit QLatin1String(const char *s) noexcept : m_size(s ? qsizetype(strlen(s)) : 0), m_data(s) {}
constexpr explicit QLatin1String(const char *f, const char *l)
: QLatin1String(f, qsizetype(l - f)) {}
@ -96,10 +97,20 @@ public:
constexpr const char *latin1() const noexcept { return m_data; }
constexpr qsizetype size() const noexcept { return m_size; }
constexpr const char *data() const noexcept { return m_data; }
[[nodiscard]] constexpr const char *constData() const noexcept { return data(); }
[[nodiscard]] constexpr const char *constBegin() const noexcept { return begin(); }
[[nodiscard]] constexpr const char *constEnd() const noexcept { return end(); }
[[nodiscard]] constexpr QLatin1Char first() const { return front(); }
[[nodiscard]] constexpr QLatin1Char last() const { return back(); }
[[nodiscard]] constexpr qsizetype length() const noexcept { return size(); }
constexpr bool isNull() const noexcept { return !data(); }
constexpr bool isEmpty() const noexcept { return !size(); }
[[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; }
template <typename...Args>
[[nodiscard]] inline QString arg(Args &&...args) const;

View File

@ -2670,7 +2670,7 @@ void tst_QString::insert_special_cases()
QCOMPARE(a.insert(1, QLatin1String("ontr")), montreal);
QCOMPARE(a.insert(4, ""), montreal);
QCOMPARE(a.insert(3, QLatin1String("")), montreal);
QCOMPARE(a.insert(3, QLatin1String(0)), montreal);
QCOMPARE(a.insert(3, QLatin1String(nullptr)), montreal);
QCOMPARE(a.insert(3, static_cast<const char *>(0)), montreal);
QCOMPARE(a.insert(0, QLatin1String("a")), QLatin1String("aMontreal"));
@ -4242,7 +4242,7 @@ void tst_QString::startsWith()
QVERIFY( !a.startsWith(QLatin1String("C")) );
QVERIFY( !a.startsWith(QLatin1String("ABCDEF")) );
QVERIFY( a.startsWith(QLatin1String("")) );
QVERIFY( a.startsWith(QLatin1String(0)) );
QVERIFY( a.startsWith(QLatin1String(nullptr)) );
QVERIFY( a.startsWith("A", Qt::CaseSensitive) );
QVERIFY( a.startsWith("A", Qt::CaseInsensitive) );
@ -4277,7 +4277,7 @@ void tst_QString::startsWith()
QVERIFY( !a.startsWith(QLatin1String("c"), Qt::CaseInsensitive) );
QVERIFY( !a.startsWith(QLatin1String("abcdef"), Qt::CaseInsensitive) );
QVERIFY( a.startsWith(QLatin1String(""), Qt::CaseInsensitive) );
QVERIFY( a.startsWith(QLatin1String(0), Qt::CaseInsensitive) );
QVERIFY( a.startsWith(QLatin1String(nullptr), Qt::CaseInsensitive) );
QVERIFY( a.startsWith('A', Qt::CaseSensitive) );
QVERIFY( a.startsWith(QLatin1Char('A'), Qt::CaseSensitive) );
QVERIFY( a.startsWith(QChar('A'), Qt::CaseSensitive) );
@ -4298,7 +4298,7 @@ void tst_QString::startsWith()
QVERIFY( !a.startsWith("ABC") );
QVERIFY( a.startsWith(QLatin1String("")) );
QVERIFY( a.startsWith(QLatin1String(0)) );
QVERIFY( a.startsWith(QLatin1String(nullptr)) );
QVERIFY( !a.startsWith(QLatin1String("ABC")) );
QVERIFY( !a.startsWith(QLatin1Char(0)) );
@ -4311,7 +4311,7 @@ void tst_QString::startsWith()
QVERIFY( !a.startsWith("ABC") );
QVERIFY( !a.startsWith(QLatin1String("")) );
QVERIFY( a.startsWith(QLatin1String(0)) );
QVERIFY( a.startsWith(QLatin1String(nullptr)) );
QVERIFY( !a.startsWith(QLatin1String("ABC")) );
QVERIFY( !a.startsWith(QLatin1Char(0)) );
@ -4358,7 +4358,7 @@ void tst_QString::endsWith()
QVERIFY( !a.endsWith(QLatin1String("C")) );
QVERIFY( !a.endsWith(QLatin1String("ABCDEF")) );
QVERIFY( a.endsWith(QLatin1String("")) );
QVERIFY( a.endsWith(QLatin1String(0)) );
QVERIFY( a.endsWith(QLatin1String(nullptr)) );
QVERIFY( a.endsWith("B", Qt::CaseSensitive) );
QVERIFY( a.endsWith("B", Qt::CaseInsensitive) );
@ -4393,7 +4393,7 @@ void tst_QString::endsWith()
QVERIFY( !a.endsWith(QLatin1String("c"), Qt::CaseInsensitive) );
QVERIFY( !a.endsWith(QLatin1String("abcdef"), Qt::CaseInsensitive) );
QVERIFY( a.endsWith(QLatin1String(""), Qt::CaseInsensitive) );
QVERIFY( a.endsWith(QLatin1String(0), Qt::CaseInsensitive) );
QVERIFY( a.endsWith(QLatin1String(nullptr), Qt::CaseInsensitive) );
QVERIFY( a.endsWith('B', Qt::CaseSensitive) );
QVERIFY( a.endsWith(QLatin1Char('B'), Qt::CaseSensitive) );
QVERIFY( a.endsWith(QChar('B'), Qt::CaseSensitive) );
@ -4407,7 +4407,7 @@ void tst_QString::endsWith()
TEST_VIEW_ENDS_WITH(QLatin1String("C"), false);
TEST_VIEW_ENDS_WITH(QLatin1String("ABCDEF"), false);
TEST_VIEW_ENDS_WITH(QLatin1String(""), true);
TEST_VIEW_ENDS_WITH(QLatin1String(0), true);
TEST_VIEW_ENDS_WITH(QLatin1String(nullptr), true);
#undef TEST_VIEW_ENDS_WITH
a = "";
@ -4419,7 +4419,7 @@ void tst_QString::endsWith()
QVERIFY( !a.endsWith(QChar()) );
QVERIFY( a.endsWith(QLatin1String("")) );
QVERIFY( a.endsWith(QLatin1String(0)) );
QVERIFY( a.endsWith(QLatin1String(nullptr)) );
QVERIFY( !a.endsWith(QLatin1String("ABC")) );
a = QString();
@ -4428,7 +4428,7 @@ void tst_QString::endsWith()
QVERIFY( !a.endsWith("ABC") );
QVERIFY( !a.endsWith(QLatin1String("")) );
QVERIFY( a.endsWith(QLatin1String(0)) );
QVERIFY( a.endsWith(QLatin1String(nullptr)) );
QVERIFY( !a.endsWith(QLatin1String("ABC")) );
QVERIFY( !a.endsWith(QLatin1Char(0)) );
@ -5736,8 +5736,8 @@ void tst_QString::operator_smaller()
QVERIFY( foo > empty );
QVERIFY( !(foo < empty) );
QVERIFY( !(null < QLatin1String(0)) );
QVERIFY( !(null > QLatin1String(0)) );
QVERIFY( !(null < QLatin1String(nullptr)) );
QVERIFY( !(null > QLatin1String(nullptr)) );
QVERIFY( !(null < QLatin1String("")) );
QVERIFY( !(null > QLatin1String("")) );
@ -5746,28 +5746,28 @@ void tst_QString::operator_smaller()
QVERIFY( !(empty < QLatin1String("")) );
QVERIFY( !(empty > QLatin1String("")) );
QVERIFY( !(QLatin1String(0) < null) );
QVERIFY( !(QLatin1String(0) > null) );
QVERIFY( !(QLatin1String(nullptr) < null) );
QVERIFY( !(QLatin1String(nullptr) > null) );
QVERIFY( !(QLatin1String("") < null) );
QVERIFY( !(QLatin1String("") > null) );
QVERIFY( !(QLatin1String(0) < empty) );
QVERIFY( !(QLatin1String(0) > empty) );
QVERIFY( !(QLatin1String(nullptr) < empty) );
QVERIFY( !(QLatin1String(nullptr) > empty) );
QVERIFY( !(QLatin1String("") < empty) );
QVERIFY( !(QLatin1String("") > empty) );
QVERIFY( QLatin1String(0) < foo );
QVERIFY( !(QLatin1String(0) > foo) );
QVERIFY( QLatin1String(nullptr) < foo );
QVERIFY( !(QLatin1String(nullptr) > foo) );
QVERIFY( QLatin1String("") < foo );
QVERIFY( !(QLatin1String("") > foo) );
QVERIFY( foo > QLatin1String(0) );
QVERIFY( !(foo < QLatin1String(0)) );
QVERIFY( foo > QLatin1String(nullptr) );
QVERIFY( !(foo < QLatin1String(nullptr)) );
QVERIFY( foo > QLatin1String("") );
QVERIFY( !(foo < QLatin1String("")) );
QVERIFY( QLatin1String(0) == empty);
QVERIFY( QLatin1String(0) == null);
QVERIFY( QLatin1String(nullptr) == empty);
QVERIFY( QLatin1String(nullptr) == null);
QVERIFY( QLatin1String("") == empty);
QVERIFY( QLatin1String("") == null);
@ -7580,7 +7580,7 @@ void tst_QString::assignQLatin1String()
QVERIFY(empty.isEmpty());
QVERIFY(!empty.isNull());
QString null = QLatin1String(0);
QString null = QLatin1String(nullptr);
QVERIFY(null.isEmpty());
QVERIFY(null.isNull());