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"
using namespace Qt::StringLiterals;
//! [0]
XbelReader::XbelReader(QTreeWidget *treeWidget)
: treeWidget(treeWidget)
@ -25,8 +27,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."));
@ -50,14 +52,14 @@ 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"))
if (xml.name() == "folder"_L1)
readFolder(nullptr);
else if (xml.name() == QLatin1String("bookmark"))
else if (xml.name() == "bookmark"_L1)
readBookmark(nullptr);
else if (xml.name() == QLatin1String("separator"))
else if (xml.name() == "separator"_L1)
readSeparator(nullptr);
else
xml.skipCurrentElement();
@ -68,7 +70,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]
@ -76,7 +78,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{});
@ -87,20 +89,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();
@ -109,16 +111,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();

View File

@ -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();

View File

@ -6,9 +6,7 @@
#include "xbelwriter.h"
#include "xbelreader.h"
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("<!DOCTYPE xbel>"));
xml.writeStartElement(QStringLiteral("xbel"));
xml.writeAttribute(XbelReader::versionAttribute(), QStringLiteral("1.0"));
xml.writeDTD("<!DOCTYPE xbel>"_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);
}
}