From c34efa071b66d3a9e880f67597699b7457b70558 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 7 May 2020 00:15:06 +0200 Subject: [PATCH] QTextOdfWriter: store all bulletChar()s as char16_t's MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ... and return them as QStringView from a file-static function so that the conversion to QString is centralized in just one place (and we can think of skipping the QString conversion once QXmlStreamWriter can write QStringViews and not just QStrings). Because the Style enum is ... weird (negative values), plaster the code with static_asserts so that we get to detect breakage when the enum values change. Change-Id: I4ca89b6c2601c6a1153e202de966356bb4f51651 Reviewed-by: MÃ¥rten Nordheim --- src/gui/text/qtextodfwriter.cpp | 45 ++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/src/gui/text/qtextodfwriter.cpp b/src/gui/text/qtextodfwriter.cpp index 1613fa201c..1b01284984 100644 --- a/src/gui/text/qtextodfwriter.cpp +++ b/src/gui/text/qtextodfwriter.cpp @@ -159,29 +159,32 @@ private: QString manifestNS; }; +static QStringView bullet_char(QTextListFormat::Style style) +{ + static_assert(int(QTextListFormat::ListDisc) == -1); + static_assert(int(QTextListFormat::ListUpperRoman) == -8); + static const char16_t chars[] = { + u'\x25cf', // bullet character + u'\x25cb', // white circle + u'\x25a1', // white square + u'1', + u'a', + u'A', + u'i', + u'I', + }; + const auto map = [](QTextListFormat::Style s) { return -int(s) - 1; }; + static_assert(uint(map(QTextListFormat::ListUpperRoman)) == std::size(chars) - 1); + const auto idx = map(style); + if (idx < 0) + return nullptr; + else + return {chars + idx, 1}; +} + static QString bulletChar(QTextListFormat::Style style) { - switch(style) { - case QTextListFormat::ListDisc: - return QChar(0x25cf); // bullet character - case QTextListFormat::ListCircle: - return QChar(0x25cb); // white circle - case QTextListFormat::ListSquare: - return QChar(0x25a1); // white square - case QTextListFormat::ListDecimal: - return QString::fromLatin1("1"); - case QTextListFormat::ListLowerAlpha: - return QString::fromLatin1("a"); - case QTextListFormat::ListUpperAlpha: - return QString::fromLatin1("A"); - case QTextListFormat::ListLowerRoman: - return QString::fromLatin1("i"); - case QTextListFormat::ListUpperRoman: - return QString::fromLatin1("I"); - default: - case QTextListFormat::ListStyleUndefined: - return QString(); - } + return bullet_char(style).toString(); } static QString borderStyleName(QTextFrameFormat::BorderStyle style)