summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@qt.io>2023-03-31 12:04:01 +0200
committerMarc Mutz <marc.mutz@qt.io>2023-04-25 15:03:03 +0200
commite1818d9e9c7e9ce6b5857054926a5f7c7f44e0d9 (patch)
tree8a224afd422482f18b85d4df0f0c288c022a4388
parentc818acda97a7dc9f184e96fac4a0adbc56e1359e (diff)
QXmlStreamAttributes: port value()/hasAttribute() to QAnyStringView
[ChangeLog][QtCore][QXmlStreamAttributes] Ported value() and hasAttribute() to QAnyStringView. Change-Id: I771b9cede1d581d3f1142246e7a25c36bcc850d6 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
-rw-r--r--src/corelib/compat/removed_api.cpp28
-rw-r--r--src/corelib/serialization/qxmlstream.cpp55
-rw-r--r--src/corelib/serialization/qxmlstream.h14
-rw-r--r--tests/auto/corelib/serialization/qxmlstream/tst_qxmlstream.cpp8
4 files changed, 49 insertions, 56 deletions
diff --git a/src/corelib/compat/removed_api.cpp b/src/corelib/compat/removed_api.cpp
index e1d9c9eccf..399d7b15f5 100644
--- a/src/corelib/compat/removed_api.cpp
+++ b/src/corelib/compat/removed_api.cpp
@@ -524,6 +524,34 @@ qsizetype QString::toUcs4_helper(const ushort *uc, qsizetype length, uint *out)
reinterpret_cast<char32_t *>(out));
}
+#include "qxmlstream.h"
+
+QStringView QXmlStreamAttributes::value(const QString &namespaceUri, const QString &name) const
+{
+ return value(qToAnyStringViewIgnoringNull(namespaceUri), qToAnyStringViewIgnoringNull(name));
+}
+
+QStringView QXmlStreamAttributes::value(const QString &namespaceUri, QLatin1StringView name) const
+{
+ return value(qToAnyStringViewIgnoringNull(namespaceUri), QAnyStringView(name));
+}
+
+QStringView QXmlStreamAttributes::value(QLatin1StringView namespaceUri, QLatin1StringView name) const
+{
+ return value(QAnyStringView(namespaceUri), QAnyStringView(name));
+}
+
+QStringView QXmlStreamAttributes::value(const QString &qualifiedName) const
+{
+ return value(qToAnyStringViewIgnoringNull(qualifiedName));
+}
+
+QStringView QXmlStreamAttributes::value(QLatin1StringView qualifiedName) const
+{
+ return value(QAnyStringView(qualifiedName));
+}
+
+
// #include "qotherheader.h"
// // implement removed functions from qotherheader.h
// order sections alphabetically to reduce chances of merge conflicts
diff --git a/src/corelib/serialization/qxmlstream.cpp b/src/corelib/serialization/qxmlstream.cpp
index eebccc6cc5..5f536d0571 100644
--- a/src/corelib/serialization/qxmlstream.cpp
+++ b/src/corelib/serialization/qxmlstream.cpp
@@ -2575,37 +2575,11 @@ Returns the entity's value.
/*! Returns the value of the attribute \a name in the namespace
described with \a namespaceUri, or an empty string reference if the
attribute is not defined. The \a namespaceUri can be empty.
- */
-QStringView QXmlStreamAttributes::value(const QString &namespaceUri, const QString &name) const
-{
- for (const QXmlStreamAttribute &attribute : *this) {
- if (attribute.name() == name && attribute.namespaceUri() == namespaceUri)
- return attribute.value();
- }
- return QStringView();
-}
-/*!\overload
- Returns the value of the attribute \a name in the namespace
- described with \a namespaceUri, or an empty string reference if the
- attribute is not defined. The \a namespaceUri can be empty.
+ \note In Qt versions prior to 6.6, this function was implemented as an
+ overload set accepting combinations of QString and QLatin1StringView only.
*/
-QStringView QXmlStreamAttributes::value(const QString &namespaceUri, QLatin1StringView name) const
-{
- for (const QXmlStreamAttribute &attribute : *this) {
- if (attribute.name() == name && attribute.namespaceUri() == namespaceUri)
- return attribute.value();
- }
- return QStringView();
-}
-
-/*!\overload
- Returns the value of the attribute \a name in the namespace
- described with \a namespaceUri, or an empty string reference if the
- attribute is not defined. The \a namespaceUri can be empty.
- */
-QStringView QXmlStreamAttributes::value(QLatin1StringView namespaceUri,
- QLatin1StringView name) const
+QStringView QXmlStreamAttributes::value(QAnyStringView namespaceUri, QAnyStringView name) const noexcept
{
for (const QXmlStreamAttribute &attribute : *this) {
if (attribute.name() == name && attribute.namespaceUri() == namespaceUri)
@@ -2625,29 +2599,12 @@ QStringView QXmlStreamAttributes::value(QLatin1StringView namespaceUri,
different prefixes can point to the same namespace), you shouldn't
use qualified names, but a resolved namespaceUri and the attribute's
local name.
- */
-QStringView QXmlStreamAttributes::value(const QString &qualifiedName) const
-{
- for (const QXmlStreamAttribute &attribute : *this) {
- if (attribute.qualifiedName() == qualifiedName)
- return attribute.value();
- }
- return QStringView();
-}
-/*!\overload
+ \note In Qt versions prior to 6.6, this function was implemented as an
+ overload set accepting QString and QLatin1StringView only.
- Returns the value of the attribute with qualified name \a
- qualifiedName , or an empty string reference if the attribute is not
- defined. A qualified name is the raw name of an attribute in the XML
- data. It consists of the namespace prefix, followed by colon,
- followed by the attribute's local name. Since the namespace prefix
- is not unique (the same prefix can point to different namespaces and
- different prefixes can point to the same namespace), you shouldn't
- use qualified names, but a resolved namespaceUri and the attribute's
- local name.
*/
-QStringView QXmlStreamAttributes::value(QLatin1StringView qualifiedName) const
+QStringView QXmlStreamAttributes::value(QAnyStringView qualifiedName) const noexcept
{
for (const QXmlStreamAttribute &attribute : *this) {
if (attribute.qualifiedName() == qualifiedName)
diff --git a/src/corelib/serialization/qxmlstream.h b/src/corelib/serialization/qxmlstream.h
index fffb9b689c..230a45f683 100644
--- a/src/corelib/serialization/qxmlstream.h
+++ b/src/corelib/serialization/qxmlstream.h
@@ -75,25 +75,25 @@ class QXmlStreamAttributes : public QList<QXmlStreamAttribute>
{
public:
inline QXmlStreamAttributes() {}
+#if QT_CORE_REMOVED_SINCE(6, 6)
Q_CORE_EXPORT QStringView value(const QString &namespaceUri, const QString &name) const;
Q_CORE_EXPORT QStringView value(const QString &namespaceUri, QLatin1StringView name) const;
Q_CORE_EXPORT QStringView value(QLatin1StringView namespaceUri, QLatin1StringView name) const;
Q_CORE_EXPORT QStringView value(const QString &qualifiedName) const;
Q_CORE_EXPORT QStringView value(QLatin1StringView qualifiedName) const;
+#endif
+ Q_CORE_EXPORT QStringView value(QAnyStringView namespaceUri, QAnyStringView name) const noexcept;
+ Q_CORE_EXPORT QStringView value(QAnyStringView qualifiedName) const noexcept;
+
Q_CORE_EXPORT void append(const QString &namespaceUri, const QString &name, const QString &value);
Q_CORE_EXPORT void append(const QString &qualifiedName, const QString &value);
- inline bool hasAttribute(const QString &qualifiedName) const
- {
- return !value(qualifiedName).isNull();
- }
-
- inline bool hasAttribute(QLatin1StringView qualifiedName) const
+ bool hasAttribute(QAnyStringView qualifiedName) const
{
return !value(qualifiedName).isNull();
}
- inline bool hasAttribute(const QString &namespaceUri, const QString &name) const
+ bool hasAttribute(QAnyStringView namespaceUri, QAnyStringView name) const
{
return !value(namespaceUri, name).isNull();
}
diff --git a/tests/auto/corelib/serialization/qxmlstream/tst_qxmlstream.cpp b/tests/auto/corelib/serialization/qxmlstream/tst_qxmlstream.cpp
index 022c81a6ec..ea538ca79d 100644
--- a/tests/auto/corelib/serialization/qxmlstream/tst_qxmlstream.cpp
+++ b/tests/auto/corelib/serialization/qxmlstream/tst_qxmlstream.cpp
@@ -1287,6 +1287,14 @@ void tst_QXmlStream::hasAttribute() const
// α is not representable in L1...
QVERIFY(!atts.hasAttribute(QLatin1String("DOESNOTEXIST")));
+ /* string literals (UTF-8/16) */
+ QVERIFY(atts.hasAttribute(u8"atträbute"));
+ QVERIFY(atts.hasAttribute( u"atträbute"));
+ QVERIFY(atts.hasAttribute(u8"α"));
+ QVERIFY(atts.hasAttribute( u"α"));
+ QVERIFY(!atts.hasAttribute(u8"β"));
+ QVERIFY(!atts.hasAttribute( u"β"));
+
/* Test with an empty & null namespaces. */
QVERIFY(atts.hasAttribute(QString(), QLatin1String("attr2"))); /* A null string. */
QVERIFY(atts.hasAttribute(QLatin1String(""), QLatin1String("attr2"))); /* An empty string. */