From f2995dc15af64dbc7b0cd8eec64cae4df9892609 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Wed, 7 Jun 2023 16:37:12 +0200 Subject: [PATCH] XBEL example - modernize strings: use "..."_L1 for literals The XML stream reader and writer accept QAnyStringView arguments these days, so passing a QLatin1StringView is entirely sufficient. This makes static functions to provide access to unique QString instances redundant. Linkers are allowed to uniquify the literals the "..."_L1 reference. Task-number: QTBUG-111228 Change-Id: I7f37e97631e11683b9ddd3842fc6233547bed5ff Reviewed-by: Marc Mutz (cherry picked from commit 202b1dca5d96f225d9e576d5f9e83696c3e02b5a) --- .../streambookmarks/xbelreader.cpp | 42 ++++++++++--------- .../streambookmarks/xbelreader.h | 5 --- .../streambookmarks/xbelwriter.cpp | 24 +++++------ 3 files changed, 33 insertions(+), 38 deletions(-) diff --git a/examples/corelib/serialization/streambookmarks/xbelreader.cpp b/examples/corelib/serialization/streambookmarks/xbelreader.cpp index c4b94bc70a2..d062a3bd1da 100644 --- a/examples/corelib/serialization/streambookmarks/xbelreader.cpp +++ b/examples/corelib/serialization/streambookmarks/xbelreader.cpp @@ -6,6 +6,8 @@ #include #include +using namespace Qt::StringLiterals; + //! [0] XbelReader::XbelReader(QTreeWidget *treeWidget) : treeWidget(treeWidget) @@ -26,8 +28,8 @@ bool XbelReader::read(QIODevice *device) xml.setDevice(device); if (xml.readNextStartElement()) { - if (xml.name() == QLatin1String("xbel") - && xml.attributes().value(versionAttribute()) == QLatin1String("1.0")) { + if (xml.name() == "xbel"_L1 + && xml.attributes().value("version"_L1) == "1.0"_L1) { readXBEL(); } else { xml.raiseError(QObject::tr("The file is not an XBEL version 1.0 file.")); @@ -51,15 +53,15 @@ QString XbelReader::errorString() const //! [3] void XbelReader::readXBEL() { - Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("xbel")); + Q_ASSERT(xml.isStartElement() && xml.name() == "xbel"_L1); while (xml.readNextStartElement()) { - if (xml.name() == QLatin1String("folder")) - readFolder(0); - else if (xml.name() == QLatin1String("bookmark")) - readBookmark(0); - else if (xml.name() == QLatin1String("separator")) - readSeparator(0); + if (xml.name() == "folder"_L1) + readFolder(nullptr); + else if (xml.name() == "bookmark"_L1) + readBookmark(nullptr); + else if (xml.name() == "separator"_L1) + readSeparator(nullptr); else xml.skipCurrentElement(); } @@ -69,7 +71,7 @@ void XbelReader::readXBEL() //! [4] void XbelReader::readTitle(QTreeWidgetItem *item) { - Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("title")); + Q_ASSERT(xml.isStartElement() && xml.name() == "title"_L1); item->setText(0, xml.readElementText()); } //! [4] @@ -77,7 +79,7 @@ void XbelReader::readTitle(QTreeWidgetItem *item) //! [5] void XbelReader::readSeparator(QTreeWidgetItem *item) { - Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("separator")); + Q_ASSERT(xml.isStartElement() && xml.name() == "separator"_L1); QTreeWidgetItem *separator = createChildItem(item); separator->setFlags(item ? item->flags() & ~Qt::ItemIsSelectable : Qt::ItemFlags{}); @@ -88,20 +90,20 @@ void XbelReader::readSeparator(QTreeWidgetItem *item) void XbelReader::readFolder(QTreeWidgetItem *item) { - Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("folder")); + Q_ASSERT(xml.isStartElement() && xml.name() == "folder"_L1); QTreeWidgetItem *folder = createChildItem(item); - bool folded = (xml.attributes().value(foldedAttribute()) != QLatin1String("no")); + bool folded = xml.attributes().value("folded"_L1) != "no"_L1; folder->setExpanded(!folded); while (xml.readNextStartElement()) { - if (xml.name() == QLatin1String("title")) + if (xml.name() == "title"_L1) readTitle(folder); - else if (xml.name() == QLatin1String("folder")) + else if (xml.name() == "folder"_L1) readFolder(folder); - else if (xml.name() == QLatin1String("bookmark")) + else if (xml.name() == "bookmark"_L1) readBookmark(folder); - else if (xml.name() == QLatin1String("separator")) + else if (xml.name() == "separator"_L1) readSeparator(folder); else xml.skipCurrentElement(); @@ -110,16 +112,16 @@ void XbelReader::readFolder(QTreeWidgetItem *item) void XbelReader::readBookmark(QTreeWidgetItem *item) { - Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("bookmark")); + Q_ASSERT(xml.isStartElement() && xml.name() == "bookmark"_L1); QTreeWidgetItem *bookmark = createChildItem(item); bookmark->setFlags(bookmark->flags() | Qt::ItemIsEditable); bookmark->setIcon(0, bookmarkIcon); bookmark->setText(0, QObject::tr("Unknown title")); - bookmark->setText(1, xml.attributes().value(hrefAttribute()).toString()); + bookmark->setText(1, xml.attributes().value("href"_L1).toString()); while (xml.readNextStartElement()) { - if (xml.name() == QLatin1String("title")) + if (xml.name() == "title"_L1) readTitle(bookmark); else xml.skipCurrentElement(); diff --git a/examples/corelib/serialization/streambookmarks/xbelreader.h b/examples/corelib/serialization/streambookmarks/xbelreader.h index 81a59b32b5f..a3fa59d8136 100644 --- a/examples/corelib/serialization/streambookmarks/xbelreader.h +++ b/examples/corelib/serialization/streambookmarks/xbelreader.h @@ -21,13 +21,8 @@ public: //! [1] bool read(QIODevice *device); - QString errorString() const; - static inline QString versionAttribute() { return QStringLiteral("version"); } - static inline QString hrefAttribute() { return QStringLiteral("href"); } - static inline QString foldedAttribute() { return QStringLiteral("folded"); } - private: //! [2] void readXBEL(); diff --git a/examples/corelib/serialization/streambookmarks/xbelwriter.cpp b/examples/corelib/serialization/streambookmarks/xbelwriter.cpp index 531e17959c3..c3770ff4d66 100644 --- a/examples/corelib/serialization/streambookmarks/xbelwriter.cpp +++ b/examples/corelib/serialization/streambookmarks/xbelwriter.cpp @@ -6,9 +6,7 @@ #include -static inline QString yesValue() { return QStringLiteral("yes"); } -static inline QString noValue() { return QStringLiteral("no"); } -static inline QString titleElement() { return QStringLiteral("title"); } +using namespace Qt::StringLiterals; //! [0] XbelWriter::XbelWriter(const QTreeWidget *treeWidget) @@ -24,9 +22,9 @@ bool XbelWriter::writeFile(QIODevice *device) xml.setDevice(device); xml.writeStartDocument(); - xml.writeDTD(QStringLiteral("")); - xml.writeStartElement(QStringLiteral("xbel")); - xml.writeAttribute(XbelReader::versionAttribute(), QStringLiteral("1.0")); + xml.writeDTD(""_L1); + xml.writeStartElement("xbel"_L1); + xml.writeAttribute("version"_L1, "1.0"_L1); for (int i = 0; i < treeWidget->topLevelItemCount(); ++i) writeItem(treeWidget->topLevelItem(i)); @@ -39,21 +37,21 @@ bool XbelWriter::writeFile(QIODevice *device) void XbelWriter::writeItem(const QTreeWidgetItem *item) { QString tagName = item->data(0, Qt::UserRole).toString(); - if (tagName == QLatin1String("folder")) { + if (tagName == "folder"_L1) { bool folded = !item->isExpanded(); xml.writeStartElement(tagName); - xml.writeAttribute(XbelReader::foldedAttribute(), folded ? yesValue() : noValue()); - xml.writeTextElement(titleElement(), item->text(0)); + xml.writeAttribute("folded"_L1, folded ? "yes"_L1 : "no"_L1); + xml.writeTextElement("title"_L1, item->text(0)); for (int i = 0; i < item->childCount(); ++i) writeItem(item->child(i)); xml.writeEndElement(); - } else if (tagName == QLatin1String("bookmark")) { + } else if (tagName == "bookmark"_L1) { xml.writeStartElement(tagName); if (!item->text(1).isEmpty()) - xml.writeAttribute(XbelReader::hrefAttribute(), item->text(1)); - xml.writeTextElement(titleElement(), item->text(0)); + xml.writeAttribute("href"_L1, item->text(1)); + xml.writeTextElement("title"_L1, item->text(0)); xml.writeEndElement(); - } else if (tagName == QLatin1String("separator")) { + } else if (tagName == "separator"_L1) { xml.writeEmptyElement(tagName); } }