corelib/serialization examples: use string literals more
A couple more compilation units could use Qt::StringLiterals. Prefer QL1SV for the code constants, to keep code small. Convert fpToString() to take QL1SV instead of const char *, with suffix empty by default. Also rearranged some spacing, some if it suggested by clang-tidy. Pick-to: 6.6 6.5 Task-number: QTBUG-111228 Change-Id: I03d810d52afcd4a760d18f2553914b75af716b74 Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
This commit is contained in:
parent
e4d545b4c6
commit
319b2e0e86
@ -14,6 +14,8 @@
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
using namespace Qt::StringLiterals;
|
||||
|
||||
/*
|
||||
* To regenerate:
|
||||
* curl -O https://www.iana.org/assignments/cbor-tags/cbor-tags.xml
|
||||
@ -252,31 +254,32 @@ template <typename T> static inline bool canConvertTo(double v)
|
||||
return v == floor(v);
|
||||
}
|
||||
|
||||
static QString fpToString(double v, const char *suffix)
|
||||
static QString fpToString(double v, QLatin1StringView suffix = ""_L1)
|
||||
{
|
||||
if (qIsInf(v))
|
||||
return v < 0 ? QStringLiteral("-inf") : QStringLiteral("inf");
|
||||
return v < 0 ? "-inf"_L1 : "inf"_L1;
|
||||
if (qIsNaN(v))
|
||||
return QStringLiteral("nan");
|
||||
return "nan"_L1;
|
||||
if (canConvertTo<qint64>(v))
|
||||
return QString::number(qint64(v)) + ".0" + suffix;
|
||||
return QString::number(qint64(v)) + ".0"_L1 + suffix;
|
||||
if (canConvertTo<quint64>(v))
|
||||
return QString::number(quint64(v)) + ".0" + suffix;
|
||||
return QString::number(quint64(v)) + ".0"_L1 + suffix;
|
||||
|
||||
QString s = QString::number(v, 'g', QLocale::FloatingPointShortest);
|
||||
if (!s.contains('.') && !s.contains('e'))
|
||||
s += '.';
|
||||
s += suffix;
|
||||
if (!s.contains(u'.') && !s.contains(u'e'))
|
||||
s += u'.';
|
||||
if (suffix.size())
|
||||
s += suffix;
|
||||
return s;
|
||||
};
|
||||
|
||||
void CborDumper::dumpOne(int nestingLevel)
|
||||
{
|
||||
QString indent(1, QLatin1Char(' '));
|
||||
QString indent(1, u' ');
|
||||
QString indented = indent;
|
||||
if (!opts.testFlag(ShowCompact)) {
|
||||
indent = QLatin1Char('\n') + QString(4 * nestingLevel, QLatin1Char(' '));
|
||||
indented = QLatin1Char('\n') + QString(4 + 4 * nestingLevel, QLatin1Char(' '));
|
||||
indent = u'\n' + QString(4 * nestingLevel, u' ');
|
||||
indented = u'\n' + QString(4 + 4 * nestingLevel, u' ');
|
||||
}
|
||||
|
||||
switch (reader.type()) {
|
||||
@ -316,7 +319,7 @@ void CborDumper::dumpOne(int nestingLevel)
|
||||
printStringWidthIndicator(r.data.size());
|
||||
|
||||
r = reader.readByteArray();
|
||||
comma = QLatin1Char(',') + indented;
|
||||
comma = u',' + indented;
|
||||
}
|
||||
} else {
|
||||
auto r = reader.readString();
|
||||
@ -325,7 +328,7 @@ void CborDumper::dumpOne(int nestingLevel)
|
||||
printStringWidthIndicator(r.data.toUtf8().size());
|
||||
|
||||
r = reader.readString();
|
||||
comma = QLatin1Char(',') + indented;
|
||||
comma = u',' + indented;
|
||||
}
|
||||
}
|
||||
|
||||
@ -413,15 +416,15 @@ void CborDumper::dumpOne(int nestingLevel)
|
||||
break;
|
||||
|
||||
case QCborStreamReader::Float16:
|
||||
printf("%s", qPrintable(fpToString(reader.toFloat16(), "f16")));
|
||||
printf("%s", qPrintable(fpToString(reader.toFloat16(), "f16"_L1)));
|
||||
reader.next();
|
||||
break;
|
||||
case QCborStreamReader::Float:
|
||||
printf("%s", qPrintable(fpToString(reader.toFloat(), "f")));
|
||||
printf("%s", qPrintable(fpToString(reader.toFloat(), "f"_L1)));
|
||||
reader.next();
|
||||
break;
|
||||
case QCborStreamReader::Double:
|
||||
printf("%s", qPrintable(fpToString(reader.toDouble(), "")));
|
||||
printf("%s", qPrintable(fpToString(reader.toDouble())));
|
||||
reader.next();
|
||||
break;
|
||||
case QCborStreamReader::Invalid:
|
||||
@ -474,7 +477,7 @@ void CborDumper::dumpOneDetailed(int nestingLevel)
|
||||
};
|
||||
|
||||
auto printFp = [=](const char *descr, double d) {
|
||||
QString s = fpToString(d, "");
|
||||
QString s = fpToString(d);
|
||||
if (s.size() <= 6)
|
||||
return print(descr, "%s", qPrintable(s));
|
||||
return print(descr, "%a", d);
|
||||
@ -726,23 +729,20 @@ int main(int argc, char *argv[])
|
||||
setlocale(LC_ALL, "C");
|
||||
|
||||
QCommandLineParser parser;
|
||||
parser.setApplicationDescription(QStringLiteral("CBOR Dumper tool"));
|
||||
parser.setApplicationDescription("CBOR Dumper tool"_L1);
|
||||
parser.addHelpOption();
|
||||
|
||||
QCommandLineOption compact({QStringLiteral("c"), QStringLiteral("compact")},
|
||||
QStringLiteral("Use compact form (no line breaks)"));
|
||||
QCommandLineOption compact({"c"_L1, "compact"_L1}, "Use compact form (no line breaks)"_L1);
|
||||
parser.addOption(compact);
|
||||
|
||||
QCommandLineOption showIndicators({QStringLiteral("i"), QStringLiteral("indicators")},
|
||||
QStringLiteral("Show indicators for width of lengths and integrals"));
|
||||
QCommandLineOption showIndicators({ "i"_L1, "indicators"_L1 },
|
||||
"Show indicators for width of lengths and integrals"_L1);
|
||||
parser.addOption(showIndicators);
|
||||
|
||||
QCommandLineOption verbose({QStringLiteral("a"), QStringLiteral("annotated")},
|
||||
QStringLiteral("Show bytes and annotated decoding"));
|
||||
QCommandLineOption verbose({"a"_L1, "annotated"_L1}, "Show bytes and annotated decoding"_L1);
|
||||
parser.addOption(verbose);
|
||||
|
||||
parser.addPositionalArgument(QStringLiteral("[source]"),
|
||||
QStringLiteral("CBOR file to read from"));
|
||||
parser.addPositionalArgument("[source]"_L1, "CBOR file to read from"_L1);
|
||||
|
||||
parser.process(app);
|
||||
|
||||
|
@ -16,6 +16,8 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
using namespace Qt::StringLiterals;
|
||||
|
||||
static CborConverter cborConverter;
|
||||
static CborDiagnosticDumper cborDiagnosticDumper;
|
||||
|
||||
@ -120,7 +122,7 @@ static QCborValue convertFromVariant(const QVariant &v, TrimFloatingPoint fpTrim
|
||||
|
||||
QString CborDiagnosticDumper::name()
|
||||
{
|
||||
return QStringLiteral("cbor-dump");
|
||||
return "cbor-dump"_L1;
|
||||
}
|
||||
|
||||
Converter::Direction CborDiagnosticDumper::directions()
|
||||
@ -213,7 +215,7 @@ const char *CborConverter::optionsHelp()
|
||||
bool CborConverter::probeFile(QIODevice *f)
|
||||
{
|
||||
if (QFile *file = qobject_cast<QFile *>(f)) {
|
||||
if (file->fileName().endsWith(QLatin1String(".cbor")))
|
||||
if (file->fileName().endsWith(".cbor"_L1))
|
||||
return true;
|
||||
}
|
||||
return f->isReadable() && f->peek(3) == QByteArray("\xd9\xd9\xf7", 3);
|
||||
|
@ -7,6 +7,8 @@
|
||||
#include <QDebug>
|
||||
#include <QTextStream>
|
||||
|
||||
using namespace Qt::StringLiterals;
|
||||
|
||||
static const char dataStreamOptionHelp[] =
|
||||
"byteorder=host|big|little Byte order to use.\n"
|
||||
"version=<n> QDataStream version (default: Qt 6.0).\n"
|
||||
@ -43,33 +45,33 @@ QDataStream &operator>>(QDataStream &ds, VariantOrderedMap &map)
|
||||
}
|
||||
|
||||
|
||||
static QString dumpVariant(const QVariant &v, const QString &indent = QLatin1String("\n"))
|
||||
static QString dumpVariant(const QVariant &v, const QString &indent = "\n"_L1)
|
||||
{
|
||||
QString result;
|
||||
QString indented = indent + QLatin1String(" ");
|
||||
QString indented = indent + " "_L1;
|
||||
|
||||
int type = v.userType();
|
||||
if (type == qMetaTypeId<VariantOrderedMap>() || type == QMetaType::QVariantMap) {
|
||||
const auto map = (type == QMetaType::QVariantMap) ?
|
||||
VariantOrderedMap(v.toMap()) : qvariant_cast<VariantOrderedMap>(v);
|
||||
|
||||
result = QLatin1String("Map {");
|
||||
result = "Map {"_L1;
|
||||
for (const auto &pair : map) {
|
||||
result += indented + dumpVariant(pair.first, indented);
|
||||
result.chop(1); // remove comma
|
||||
result += QLatin1String(" => ") + dumpVariant(pair.second, indented);
|
||||
result += " => "_L1 + dumpVariant(pair.second, indented);
|
||||
|
||||
}
|
||||
result.chop(1); // remove comma
|
||||
result += indent + QLatin1String("},");
|
||||
result += indent + "},"_L1;
|
||||
} else if (type == QMetaType::QVariantList) {
|
||||
const QVariantList list = v.toList();
|
||||
|
||||
result = QLatin1String("List [");
|
||||
result = "List ["_L1;
|
||||
for (const auto &item : list)
|
||||
result += indented + dumpVariant(item, indented);
|
||||
result.chop(1); // remove comma
|
||||
result += indent + QLatin1String("],");
|
||||
result += indent + "],"_L1;
|
||||
} else {
|
||||
QDebug debug(&result);
|
||||
debug.nospace() << v << ',';
|
||||
@ -79,7 +81,7 @@ static QString dumpVariant(const QVariant &v, const QString &indent = QLatin1Str
|
||||
|
||||
QString DataStreamDumper::name()
|
||||
{
|
||||
return QStringLiteral("datastream-dump");
|
||||
return "datastream-dump"_L1;
|
||||
}
|
||||
|
||||
Converter::Direction DataStreamDumper::directions()
|
||||
@ -128,7 +130,7 @@ DataStreamConverter::DataStreamConverter()
|
||||
|
||||
QString DataStreamConverter::name()
|
||||
{
|
||||
return QStringLiteral("datastream");
|
||||
return "datastream"_L1;
|
||||
}
|
||||
|
||||
Converter::Direction DataStreamConverter::directions()
|
||||
|
@ -9,6 +9,8 @@
|
||||
#include <QJsonObject>
|
||||
#include <QJsonValue>
|
||||
|
||||
using namespace Qt::StringLiterals;
|
||||
|
||||
static JsonConverter jsonConverter;
|
||||
|
||||
static const char jsonOptionHelp[] =
|
||||
@ -30,7 +32,7 @@ JsonConverter::JsonConverter()
|
||||
|
||||
QString JsonConverter::name()
|
||||
{
|
||||
return "json";
|
||||
return "json"_L1;
|
||||
}
|
||||
|
||||
Converter::Direction JsonConverter::directions()
|
||||
@ -51,7 +53,7 @@ const char *JsonConverter::optionsHelp()
|
||||
bool JsonConverter::probeFile(QIODevice *f)
|
||||
{
|
||||
if (QFile *file = qobject_cast<QFile *>(f)) {
|
||||
if (file->fileName().endsWith(QLatin1String(".json")))
|
||||
if (file->fileName().endsWith(".json"_L1))
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -91,9 +93,9 @@ void JsonConverter::saveFile(QIODevice *f, const QVariant &contents, const QStri
|
||||
{
|
||||
QJsonDocument::JsonFormat format = QJsonDocument::Indented;
|
||||
for (const QString &s : options) {
|
||||
if (s == QLatin1String("compact=no")) {
|
||||
if (s == "compact=no"_L1) {
|
||||
format = QJsonDocument::Indented;
|
||||
} else if (s == QLatin1String("compact=yes")) {
|
||||
} else if (s == "compact=yes"_L1) {
|
||||
format = QJsonDocument::Compact;
|
||||
} else {
|
||||
fprintf(stderr, "Unknown option '%s' to JSON output. Valid options are:\n%s",
|
||||
|
@ -11,6 +11,8 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
using namespace Qt::StringLiterals;
|
||||
|
||||
static QList<Converter *> *availableConverters;
|
||||
|
||||
Converter::Converter()
|
||||
@ -41,42 +43,44 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
inputFormats.sort();
|
||||
outputFormats.sort();
|
||||
inputFormats.prepend("auto");
|
||||
outputFormats.prepend("auto");
|
||||
inputFormats.prepend("auto"_L1);
|
||||
outputFormats.prepend("auto"_L1);
|
||||
|
||||
QCommandLineParser parser;
|
||||
parser.setApplicationDescription(QStringLiteral("Qt file format conversion tool"));
|
||||
parser.setApplicationDescription("Qt file format conversion tool"_L1);
|
||||
parser.addHelpOption();
|
||||
|
||||
QCommandLineOption inputFormatOption(QStringList{"I", "input-format"});
|
||||
inputFormatOption.setDescription(QLatin1String("Select the input format for the input file. Available formats: ") +
|
||||
inputFormats.join(", "));
|
||||
inputFormatOption.setValueName("format");
|
||||
QCommandLineOption inputFormatOption(QStringList{ "I"_L1, "input-format"_L1 });
|
||||
inputFormatOption.setDescription(
|
||||
"Select the input format for the input file. Available formats: "_L1
|
||||
+ inputFormats.join(", "_L1));
|
||||
inputFormatOption.setValueName("format"_L1);
|
||||
inputFormatOption.setDefaultValue(inputFormats.constFirst());
|
||||
parser.addOption(inputFormatOption);
|
||||
|
||||
QCommandLineOption outputFormatOption(QStringList{"O", "output-format"});
|
||||
outputFormatOption.setDescription(QLatin1String("Select the output format for the output file. Available formats: ") +
|
||||
outputFormats.join(", "));
|
||||
outputFormatOption.setValueName("format");
|
||||
QCommandLineOption outputFormatOption(QStringList{ "O"_L1, "output-format"_L1 });
|
||||
outputFormatOption.setDescription(
|
||||
"Select the output format for the output file. Available formats: "_L1
|
||||
+ outputFormats.join(", "_L1));
|
||||
outputFormatOption.setValueName("format"_L1);
|
||||
outputFormatOption.setDefaultValue(outputFormats.constFirst());
|
||||
parser.addOption(outputFormatOption);
|
||||
|
||||
QCommandLineOption optionOption(QStringList{"o", "option"});
|
||||
optionOption.setDescription(QStringLiteral("Format-specific options. Use --format-options to find out what options are available."));
|
||||
optionOption.setValueName("options...");
|
||||
QCommandLineOption optionOption(QStringList{ "o"_L1, "option"_L1 });
|
||||
optionOption.setDescription(
|
||||
"Format-specific options. Use --format-options to find out what options are available."_L1);
|
||||
optionOption.setValueName("options..."_L1);
|
||||
optionOption.setDefaultValues({});
|
||||
parser.addOption(optionOption);
|
||||
|
||||
QCommandLineOption formatOptionsOption("format-options");
|
||||
formatOptionsOption.setDescription(QStringLiteral("Prints the list of valid options for --option for the converter format <format>."));
|
||||
formatOptionsOption.setValueName("format");
|
||||
QCommandLineOption formatOptionsOption("format-options"_L1);
|
||||
formatOptionsOption.setDescription(
|
||||
"Prints the list of valid options for --option for the converter format <format>."_L1);
|
||||
formatOptionsOption.setValueName("format"_L1);
|
||||
parser.addOption(formatOptionsOption);
|
||||
|
||||
parser.addPositionalArgument(QStringLiteral("[source]"),
|
||||
QStringLiteral("File to read from (stdin if none)"));
|
||||
parser.addPositionalArgument(QStringLiteral("[destination]"),
|
||||
QStringLiteral("File to write to (stdout if none)"));
|
||||
parser.addPositionalArgument("[source]"_L1, "File to read from (stdin if none)"_L1);
|
||||
parser.addPositionalArgument("[destination]"_L1, "File to write to (stdout if none)"_L1);
|
||||
|
||||
parser.process(app);
|
||||
|
||||
@ -99,7 +103,7 @@ int main(int argc, char *argv[])
|
||||
|
||||
Converter *inconv = nullptr;
|
||||
QString format = parser.value(inputFormatOption);
|
||||
if (format != "auto") {
|
||||
if (format != "auto"_L1) {
|
||||
for (Converter *conv : std::as_const(*availableConverters)) {
|
||||
if (conv->name() == format) {
|
||||
inconv = conv;
|
||||
@ -115,7 +119,7 @@ int main(int argc, char *argv[])
|
||||
|
||||
Converter *outconv = nullptr;
|
||||
format = parser.value(outputFormatOption);
|
||||
if (format != "auto") {
|
||||
if (format != "auto"_L1) {
|
||||
for (Converter *conv : std::as_const(*availableConverters)) {
|
||||
if (conv->name() == format) {
|
||||
outconv = conv;
|
||||
|
@ -3,12 +3,14 @@
|
||||
|
||||
#include "nullconverter.h"
|
||||
|
||||
using namespace Qt::StringLiterals;
|
||||
|
||||
static NullConverter nullConverter;
|
||||
Converter *Converter::null = &nullConverter;
|
||||
|
||||
QString NullConverter::name()
|
||||
{
|
||||
return QLatin1String("null");
|
||||
return "null"_L1;
|
||||
}
|
||||
|
||||
Converter::Direction NullConverter::directions()
|
||||
|
@ -6,6 +6,8 @@
|
||||
#include <QFile>
|
||||
#include <QTextStream>
|
||||
|
||||
using namespace Qt::StringLiterals;
|
||||
|
||||
static void dumpVariant(QTextStream &out, const QVariant &v)
|
||||
{
|
||||
switch (v.userType()) {
|
||||
@ -44,7 +46,7 @@ static void dumpVariant(QTextStream &out, const QVariant &v)
|
||||
|
||||
QString TextConverter::name()
|
||||
{
|
||||
return QStringLiteral("text");
|
||||
return "text"_L1;
|
||||
}
|
||||
|
||||
Converter::Direction TextConverter::directions()
|
||||
@ -65,7 +67,7 @@ const char *TextConverter::optionsHelp()
|
||||
bool TextConverter::probeFile(QIODevice *f)
|
||||
{
|
||||
if (QFile *file = qobject_cast<QFile *>(f))
|
||||
return file->fileName().endsWith(QLatin1String(".txt"));
|
||||
return file->fileName().endsWith(".txt"_L1);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -13,6 +13,8 @@
|
||||
#include <QXmlStreamReader>
|
||||
#include <QXmlStreamWriter>
|
||||
|
||||
using namespace Qt::StringLiterals;
|
||||
|
||||
static const char xmlOptionHelp[] =
|
||||
"compact=no|yes Use compact XML form.\n";
|
||||
|
||||
@ -23,7 +25,7 @@ static QVariant variantFromXml(QXmlStreamReader &xml, Converter::Options options
|
||||
static QVariantList listFromXml(QXmlStreamReader &xml, Converter::Options options)
|
||||
{
|
||||
QVariantList list;
|
||||
while (!xml.atEnd() && !(xml.isEndElement() && xml.name() == QLatin1String("list"))) {
|
||||
while (!xml.atEnd() && !(xml.isEndElement() && xml.name() == "list"_L1)) {
|
||||
xml.readNext();
|
||||
switch (xml.tokenType()) {
|
||||
case QXmlStreamReader::StartElement:
|
||||
@ -60,7 +62,7 @@ static QVariantList listFromXml(QXmlStreamReader &xml, Converter::Options option
|
||||
static VariantOrderedMap::value_type mapEntryFromXml(QXmlStreamReader &xml, Converter::Options options)
|
||||
{
|
||||
QVariant key, value;
|
||||
while (!xml.atEnd() && !(xml.isEndElement() && xml.name() == QLatin1String("entry"))) {
|
||||
while (!xml.atEnd() && !(xml.isEndElement() && xml.name() == "entry"_L1)) {
|
||||
xml.readNext();
|
||||
switch (xml.tokenType()) {
|
||||
case QXmlStreamReader::StartElement:
|
||||
@ -103,11 +105,11 @@ static QVariant mapFromXml(QXmlStreamReader &xml, Converter::Options options)
|
||||
QVariantMap map1;
|
||||
VariantOrderedMap map2;
|
||||
|
||||
while (!xml.atEnd() && !(xml.isEndElement() && xml.name() == QLatin1String("map"))) {
|
||||
while (!xml.atEnd() && !(xml.isEndElement() && xml.name() == "map"_L1)) {
|
||||
xml.readNext();
|
||||
switch (xml.tokenType()) {
|
||||
case QXmlStreamReader::StartElement:
|
||||
if (xml.name() == QLatin1String("entry")) {
|
||||
if (xml.name() == "entry"_L1) {
|
||||
auto pair = mapEntryFromXml(xml, options);
|
||||
if (options & Converter::SupportsArbitraryMapKeys)
|
||||
map2.append(pair);
|
||||
@ -149,18 +151,18 @@ static QVariant mapFromXml(QXmlStreamReader &xml, Converter::Options options)
|
||||
static QVariant variantFromXml(QXmlStreamReader &xml, Converter::Options options)
|
||||
{
|
||||
QStringView name = xml.name();
|
||||
if (name == QLatin1String("list"))
|
||||
if (name == "list"_L1)
|
||||
return listFromXml(xml, options);
|
||||
if (name == QLatin1String("map"))
|
||||
if (name == "map"_L1)
|
||||
return mapFromXml(xml, options);
|
||||
if (name != QLatin1String("value")) {
|
||||
if (name != "value"_L1) {
|
||||
fprintf(stderr, "%lld:%lld: Invalid XML key '%s'.\n",
|
||||
xml.lineNumber(), xml.columnNumber(), qPrintable(name.toString()));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
QXmlStreamAttributes attrs = xml.attributes();
|
||||
QStringView type = attrs.value(QLatin1String("type"));
|
||||
QStringView type = attrs.value("type"_L1);
|
||||
|
||||
forever {
|
||||
xml.readNext();
|
||||
@ -182,7 +184,7 @@ static QVariant variantFromXml(QXmlStreamReader &xml, Converter::Options options
|
||||
QVariant result;
|
||||
if (type.isEmpty()) {
|
||||
// ok
|
||||
} else if (type == QLatin1String("number")) {
|
||||
} else if (type == "number"_L1) {
|
||||
// try integer first
|
||||
bool ok;
|
||||
qint64 v = text.toLongLong(&ok);
|
||||
@ -198,27 +200,27 @@ static QVariant variantFromXml(QXmlStreamReader &xml, Converter::Options options
|
||||
}
|
||||
result = d;
|
||||
}
|
||||
} else if (type == QLatin1String("bytes")) {
|
||||
} else if (type == "bytes"_L1) {
|
||||
QByteArray data = text.toLatin1();
|
||||
QStringView encoding = attrs.value("encoding");
|
||||
if (encoding == QLatin1String("base64url")) {
|
||||
if (encoding == "base64url"_L1) {
|
||||
result = QByteArray::fromBase64(data, QByteArray::Base64UrlEncoding);
|
||||
} else if (encoding == QLatin1String("hex")) {
|
||||
} else if (encoding == "hex"_L1) {
|
||||
result = QByteArray::fromHex(data);
|
||||
} else if (encoding.isEmpty() || encoding == QLatin1String("base64")) {
|
||||
} else if (encoding.isEmpty() || encoding == "base64"_L1) {
|
||||
result = QByteArray::fromBase64(data);
|
||||
} else {
|
||||
fprintf(stderr, "%lld:%lld: Invalid XML: unknown encoding '%s' for bytes.\n",
|
||||
xml.lineNumber(), xml.columnNumber(), qPrintable(encoding.toString()));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
} else if (type == QLatin1String("string")) {
|
||||
} else if (type == "string"_L1) {
|
||||
result = text.toString();
|
||||
} else if (type == QLatin1String("null")) {
|
||||
} else if (type == "null"_L1) {
|
||||
result = QVariant::fromValue(nullptr);
|
||||
} else if (type == QLatin1String("CBOR simple type")) {
|
||||
} else if (type == "CBOR simple type"_L1) {
|
||||
result = QVariant::fromValue(QCborSimpleType(text.toShort()));
|
||||
} else if (type == QLatin1String("bits")) {
|
||||
} else if (type == "bits"_L1) {
|
||||
QBitArray ba;
|
||||
ba.resize(text.size());
|
||||
qsizetype n = 0;
|
||||
@ -238,13 +240,13 @@ static QVariant variantFromXml(QXmlStreamReader &xml, Converter::Options options
|
||||
result = ba;
|
||||
} else {
|
||||
int id = QMetaType::UnknownType;
|
||||
if (type == QLatin1String("datetime"))
|
||||
if (type == "datetime"_L1)
|
||||
id = QMetaType::QDateTime;
|
||||
else if (type == QLatin1String("url"))
|
||||
else if (type == "url"_L1)
|
||||
id = QMetaType::QUrl;
|
||||
else if (type == QLatin1String("uuid"))
|
||||
else if (type == "uuid"_L1)
|
||||
id = QMetaType::QUuid;
|
||||
else if (type == QLatin1String("regex"))
|
||||
else if (type == "regex"_L1)
|
||||
id = QMetaType::QRegularExpression;
|
||||
else
|
||||
id = QMetaType::fromName(type.toLatin1()).id();
|
||||
@ -301,7 +303,7 @@ static void variantToXml(QXmlStreamWriter &xml, const QVariant &v)
|
||||
xml.writeEndElement();
|
||||
} else {
|
||||
xml.writeStartElement("value");
|
||||
QString typeString = QStringLiteral("type");
|
||||
QString typeString = "type"_L1;
|
||||
switch (type) {
|
||||
case QMetaType::Short:
|
||||
case QMetaType::UShort:
|
||||
@ -401,7 +403,7 @@ static void variantToXml(QXmlStreamWriter &xml, const QVariant &v)
|
||||
|
||||
QString XmlConverter::name()
|
||||
{
|
||||
return QStringLiteral("xml");
|
||||
return "xml"_L1;
|
||||
}
|
||||
|
||||
Converter::Direction XmlConverter::directions()
|
||||
@ -422,7 +424,7 @@ const char *XmlConverter::optionsHelp()
|
||||
bool XmlConverter::probeFile(QIODevice *f)
|
||||
{
|
||||
if (QFile *file = qobject_cast<QFile *>(f)) {
|
||||
if (file->fileName().endsWith(QLatin1String(".xml")))
|
||||
if (file->fileName().endsWith(".xml"_L1))
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -449,9 +451,9 @@ void XmlConverter::saveFile(QIODevice *f, const QVariant &contents, const QStrin
|
||||
{
|
||||
bool compact = false;
|
||||
for (const QString &s : options) {
|
||||
if (s == QLatin1String("compact=no")) {
|
||||
if (s == "compact=no"_L1) {
|
||||
compact = false;
|
||||
} else if (s == QLatin1String("compact=yes")) {
|
||||
} else if (s == "compact=yes"_L1) {
|
||||
compact = true;
|
||||
} else {
|
||||
fprintf(stderr, "Unknown option '%s' to XML output. Valid options are:\n%s",
|
||||
|
@ -11,6 +11,8 @@
|
||||
#include <QRandomGenerator>
|
||||
#include <QTextStream>
|
||||
|
||||
using namespace Qt::StringLiterals;
|
||||
|
||||
Character Game::player() const
|
||||
{
|
||||
return mPlayer;
|
||||
@ -25,35 +27,35 @@ QList<Level> Game::levels() const
|
||||
void Game::newGame()
|
||||
{
|
||||
mPlayer = Character();
|
||||
mPlayer.setName(QStringLiteral("Hero"));
|
||||
mPlayer.setName("Hero"_L1);
|
||||
mPlayer.setClassType(Character::Archer);
|
||||
mPlayer.setLevel(QRandomGenerator::global()->bounded(15, 21));
|
||||
|
||||
mLevels.clear();
|
||||
mLevels.reserve(2);
|
||||
|
||||
Level village(QStringLiteral("Village"));
|
||||
Level village("Village"_L1);
|
||||
QList<Character> villageNpcs;
|
||||
villageNpcs.reserve(2);
|
||||
villageNpcs.append(Character(QStringLiteral("Barry the Blacksmith"),
|
||||
villageNpcs.append(Character("Barry the Blacksmith"_L1,
|
||||
QRandomGenerator::global()->bounded(8, 11),
|
||||
Character::Warrior));
|
||||
villageNpcs.append(Character(QStringLiteral("Terry the Trader"),
|
||||
villageNpcs.append(Character("Terry the Trader"_L1,
|
||||
QRandomGenerator::global()->bounded(6, 8),
|
||||
Character::Warrior));
|
||||
village.setNpcs(villageNpcs);
|
||||
mLevels.append(village);
|
||||
|
||||
Level dungeon(QStringLiteral("Dungeon"));
|
||||
Level dungeon("Dungeon"_L1);
|
||||
QList<Character> dungeonNpcs;
|
||||
dungeonNpcs.reserve(3);
|
||||
dungeonNpcs.append(Character(QStringLiteral("Eric the Evil"),
|
||||
dungeonNpcs.append(Character("Eric the Evil"_L1,
|
||||
QRandomGenerator::global()->bounded(18, 26),
|
||||
Character::Mage));
|
||||
dungeonNpcs.append(Character(QStringLiteral("Eric's Left Minion"),
|
||||
dungeonNpcs.append(Character("Eric's Left Minion"_L1,
|
||||
QRandomGenerator::global()->bounded(5, 7),
|
||||
Character::Warrior));
|
||||
dungeonNpcs.append(Character(QStringLiteral("Eric's Right Minion"),
|
||||
dungeonNpcs.append(Character("Eric's Right Minion"_L1,
|
||||
QRandomGenerator::global()->bounded(4, 9),
|
||||
Character::Warrior));
|
||||
dungeon.setNpcs(dungeonNpcs);
|
||||
@ -64,9 +66,7 @@ void Game::newGame()
|
||||
//! [loadGame]
|
||||
bool Game::loadGame(Game::SaveFormat saveFormat)
|
||||
{
|
||||
QFile loadFile(saveFormat == Json
|
||||
? QStringLiteral("save.json")
|
||||
: QStringLiteral("save.dat"));
|
||||
QFile loadFile(saveFormat == Json ? "save.json"_L1 : "save.dat"_L1);
|
||||
|
||||
if (!loadFile.open(QIODevice::ReadOnly)) {
|
||||
qWarning("Couldn't open save file.");
|
||||
@ -92,9 +92,7 @@ bool Game::loadGame(Game::SaveFormat saveFormat)
|
||||
//! [saveGame]
|
||||
bool Game::saveGame(Game::SaveFormat saveFormat) const
|
||||
{
|
||||
QFile saveFile(saveFormat == Json
|
||||
? QStringLiteral("save.json")
|
||||
: QStringLiteral("save.dat"));
|
||||
QFile saveFile(saveFormat == Json ? "save.json"_L1 : "save.dat"_L1);
|
||||
|
||||
if (!saveFile.open(QIODevice::WriteOnly)) {
|
||||
qWarning("Couldn't open save file.");
|
||||
|
Loading…
Reference in New Issue
Block a user