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.

Pick-to: 6.6 6.5
Task-number: QTBUG-111228
Change-Id: I7f37e97631e11683b9ddd3842fc6233547bed5ff
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
This commit is contained in:
Edward Welbourne 2023-06-07 16:37:12 +02:00
parent 5fd4a65d95
commit 202b1dca5d
3 changed files with 30 additions and 35 deletions

View File

@ -5,6 +5,8 @@
#include "xbelreader.h" #include "xbelreader.h"
using namespace Qt::StringLiterals;
//! [0] //! [0]
XbelReader::XbelReader(QTreeWidget *treeWidget) XbelReader::XbelReader(QTreeWidget *treeWidget)
: treeWidget(treeWidget) : treeWidget(treeWidget)
@ -25,8 +27,8 @@ bool XbelReader::read(QIODevice *device)
xml.setDevice(device); xml.setDevice(device);
if (xml.readNextStartElement()) { if (xml.readNextStartElement()) {
if (xml.name() == QLatin1String("xbel") if (xml.name() == "xbel"_L1
&& xml.attributes().value(versionAttribute()) == QLatin1String("1.0")) { && xml.attributes().value("version"_L1) == "1.0"_L1) {
readXBEL(); readXBEL();
} else { } else {
xml.raiseError(QObject::tr("The file is not an XBEL version 1.0 file.")); xml.raiseError(QObject::tr("The file is not an XBEL version 1.0 file."));
@ -50,14 +52,14 @@ QString XbelReader::errorString() const
//! [3] //! [3]
void XbelReader::readXBEL() void XbelReader::readXBEL()
{ {
Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("xbel")); Q_ASSERT(xml.isStartElement() && xml.name() == "xbel"_L1);
while (xml.readNextStartElement()) { while (xml.readNextStartElement()) {
if (xml.name() == QLatin1String("folder")) if (xml.name() == "folder"_L1)
readFolder(nullptr); readFolder(nullptr);
else if (xml.name() == QLatin1String("bookmark")) else if (xml.name() == "bookmark"_L1)
readBookmark(nullptr); readBookmark(nullptr);
else if (xml.name() == QLatin1String("separator")) else if (xml.name() == "separator"_L1)
readSeparator(nullptr); readSeparator(nullptr);
else else
xml.skipCurrentElement(); xml.skipCurrentElement();
@ -68,7 +70,7 @@ void XbelReader::readXBEL()
//! [4] //! [4]
void XbelReader::readTitle(QTreeWidgetItem *item) 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()); item->setText(0, xml.readElementText());
} }
//! [4] //! [4]
@ -76,7 +78,7 @@ void XbelReader::readTitle(QTreeWidgetItem *item)
//! [5] //! [5]
void XbelReader::readSeparator(QTreeWidgetItem *item) 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); QTreeWidgetItem *separator = createChildItem(item);
separator->setFlags(item ? item->flags() & ~Qt::ItemIsSelectable : Qt::ItemFlags{}); separator->setFlags(item ? item->flags() & ~Qt::ItemIsSelectable : Qt::ItemFlags{});
@ -87,20 +89,20 @@ void XbelReader::readSeparator(QTreeWidgetItem *item)
void XbelReader::readFolder(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); QTreeWidgetItem *folder = createChildItem(item);
bool folded = (xml.attributes().value(foldedAttribute()) != QLatin1String("no")); bool folded = xml.attributes().value("folded"_L1) != "no"_L1;
folder->setExpanded(!folded); folder->setExpanded(!folded);
while (xml.readNextStartElement()) { while (xml.readNextStartElement()) {
if (xml.name() == QLatin1String("title")) if (xml.name() == "title"_L1)
readTitle(folder); readTitle(folder);
else if (xml.name() == QLatin1String("folder")) else if (xml.name() == "folder"_L1)
readFolder(folder); readFolder(folder);
else if (xml.name() == QLatin1String("bookmark")) else if (xml.name() == "bookmark"_L1)
readBookmark(folder); readBookmark(folder);
else if (xml.name() == QLatin1String("separator")) else if (xml.name() == "separator"_L1)
readSeparator(folder); readSeparator(folder);
else else
xml.skipCurrentElement(); xml.skipCurrentElement();
@ -109,16 +111,16 @@ void XbelReader::readFolder(QTreeWidgetItem *item)
void XbelReader::readBookmark(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); QTreeWidgetItem *bookmark = createChildItem(item);
bookmark->setFlags(bookmark->flags() | Qt::ItemIsEditable); bookmark->setFlags(bookmark->flags() | Qt::ItemIsEditable);
bookmark->setIcon(0, bookmarkIcon); bookmark->setIcon(0, bookmarkIcon);
bookmark->setText(0, QObject::tr("Unknown title")); 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()) { while (xml.readNextStartElement()) {
if (xml.name() == QLatin1String("title")) if (xml.name() == "title"_L1)
readTitle(bookmark); readTitle(bookmark);
else else
xml.skipCurrentElement(); xml.skipCurrentElement();

View File

@ -21,13 +21,8 @@ public:
//! [1] //! [1]
bool read(QIODevice *device); bool read(QIODevice *device);
QString errorString() const; 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: private:
//! [2] //! [2]
void readXBEL(); void readXBEL();

View File

@ -6,9 +6,7 @@
#include "xbelwriter.h" #include "xbelwriter.h"
#include "xbelreader.h" #include "xbelreader.h"
static inline QString yesValue() { return QStringLiteral("yes"); } using namespace Qt::StringLiterals;
static inline QString noValue() { return QStringLiteral("no"); }
static inline QString titleElement() { return QStringLiteral("title"); }
//! [0] //! [0]
XbelWriter::XbelWriter(const QTreeWidget *treeWidget) XbelWriter::XbelWriter(const QTreeWidget *treeWidget)
@ -24,9 +22,9 @@ bool XbelWriter::writeFile(QIODevice *device)
xml.setDevice(device); xml.setDevice(device);
xml.writeStartDocument(); xml.writeStartDocument();
xml.writeDTD(QStringLiteral("<!DOCTYPE xbel>")); xml.writeDTD("<!DOCTYPE xbel>"_L1);
xml.writeStartElement(QStringLiteral("xbel")); xml.writeStartElement("xbel"_L1);
xml.writeAttribute(XbelReader::versionAttribute(), QStringLiteral("1.0")); xml.writeAttribute("version"_L1, "1.0"_L1);
for (int i = 0; i < treeWidget->topLevelItemCount(); ++i) for (int i = 0; i < treeWidget->topLevelItemCount(); ++i)
writeItem(treeWidget->topLevelItem(i)); writeItem(treeWidget->topLevelItem(i));
@ -39,21 +37,21 @@ bool XbelWriter::writeFile(QIODevice *device)
void XbelWriter::writeItem(const QTreeWidgetItem *item) void XbelWriter::writeItem(const QTreeWidgetItem *item)
{ {
QString tagName = item->data(0, Qt::UserRole).toString(); QString tagName = item->data(0, Qt::UserRole).toString();
if (tagName == QLatin1String("folder")) { if (tagName == "folder"_L1) {
bool folded = !item->isExpanded(); bool folded = !item->isExpanded();
xml.writeStartElement(tagName); xml.writeStartElement(tagName);
xml.writeAttribute(XbelReader::foldedAttribute(), folded ? yesValue() : noValue()); xml.writeAttribute("folded"_L1, folded ? "yes"_L1 : "no"_L1);
xml.writeTextElement(titleElement(), item->text(0)); xml.writeTextElement("title"_L1, item->text(0));
for (int i = 0; i < item->childCount(); ++i) for (int i = 0; i < item->childCount(); ++i)
writeItem(item->child(i)); writeItem(item->child(i));
xml.writeEndElement(); xml.writeEndElement();
} else if (tagName == QLatin1String("bookmark")) { } else if (tagName == "bookmark"_L1) {
xml.writeStartElement(tagName); xml.writeStartElement(tagName);
if (!item->text(1).isEmpty()) if (!item->text(1).isEmpty())
xml.writeAttribute(XbelReader::hrefAttribute(), item->text(1)); xml.writeAttribute("href"_L1, item->text(1));
xml.writeTextElement(titleElement(), item->text(0)); xml.writeTextElement("title"_L1, item->text(0));
xml.writeEndElement(); xml.writeEndElement();
} else if (tagName == QLatin1String("separator")) { } else if (tagName == "separator"_L1) {
xml.writeEmptyElement(tagName); xml.writeEmptyElement(tagName);
} }
} }