diff options
| author | Ahmad Samir <a.samirh78@gmail.com> | 2021-05-02 17:50:05 +0200 | 
|---|---|---|
| committer | David Faure <faure@kde.org> | 2021-05-02 18:09:09 +0000 | 
| commit | 1780fb2a237af80ddc1f9cfb70cb892b53b91990 (patch) | |
| tree | 889e7adf0ec8d513d75e30a5e7ad939e149ba345 | |
| parent | 1164ef55e93b4171306899ab786bfd0acaa00847 (diff) | |
| download | kconfig-1780fb2a237af80ddc1f9cfb70cb892b53b91990.tar.gz kconfig-1780fb2a237af80ddc1f9cfb70cb892b53b91990.tar.bz2 | |
Minor code refactoring
Some methods in ConfigLoaderHandler always returned true, change them to return
void instead. Also port them to take a QStringView instead of QStringRef, this
doesn't require a lot of changes because a QStringView can be constructed from
a QStringRef.
QXmlStreamAttribute methods like value() and name() return QStringRef in Qt5
and QStringView in Qt6, "fix" the issue by using auto keyword, which works
in both cases.
QStringView::toInt() isn't efficient in Qt5 so make the build conditional.
NO_CHANGELOG
| -rw-r--r-- | src/core/kconfig.cpp | 10 | ||||
| -rw-r--r-- | src/gui/kconfigloader.cpp | 68 | ||||
| -rw-r--r-- | src/gui/kconfigloaderhandler_p.h | 5 | ||||
| -rw-r--r-- | src/kconf_update/kconfigutils.cpp | 4 | 
4 files changed, 39 insertions, 48 deletions
| diff --git a/src/core/kconfig.cpp b/src/core/kconfig.cpp index cc3700e1..8118ea51 100644 --- a/src/core/kconfig.cpp +++ b/src/core/kconfig.cpp @@ -174,21 +174,21 @@ QString KConfigPrivate::expandString(const QString &value)      int nDollarPos = aValue.indexOf(QLatin1Char('$'));      while (nDollarPos != -1 && nDollarPos + 1 < aValue.length()) {          // there is at least one $ -        if (aValue[nDollarPos + 1] != QLatin1Char('$')) { +        if (aValue.at(nDollarPos + 1) != QLatin1Char('$')) {              int nEndPos = nDollarPos + 1;              // the next character is not $ -            QStringRef aVarName; -            if (aValue[nEndPos] == QLatin1Char('{')) { +            QStringView aVarName; +            if (aValue.at(nEndPos) == QLatin1Char('{')) {                  while ((nEndPos <= aValue.length()) && (aValue[nEndPos] != QLatin1Char('}'))) {                      ++nEndPos;                  }                  ++nEndPos; -                aVarName = aValue.midRef(nDollarPos + 2, nEndPos - nDollarPos - 3); +                aVarName = QStringView(aValue).mid(nDollarPos + 2, nEndPos - nDollarPos - 3);              } else {                  while (nEndPos < aValue.length() && (aValue[nEndPos].isNumber() || aValue[nEndPos].isLetter() || aValue[nEndPos] == QLatin1Char('_'))) {                      ++nEndPos;                  } -                aVarName = aValue.midRef(nDollarPos + 1, nEndPos - nDollarPos - 1); +                aVarName = QStringView(aValue).mid(nDollarPos + 1, nEndPos - nDollarPos - 1);              }              QString env;              if (!aVarName.isEmpty()) { diff --git a/src/gui/kconfigloader.cpp b/src/gui/kconfigloader.cpp index bc1751f4..940613b8 100644 --- a/src/gui/kconfigloader.cpp +++ b/src/gui/kconfigloader.cpp @@ -49,20 +49,14 @@ bool ConfigLoaderHandler::parse(QIODevice *input)          switch (reader.tokenType()) {          case QXmlStreamReader::StartElement: -            if (!startElement(reader.name(), reader.attributes())) { -                return false; -            } +            startElement(reader.name(), reader.attributes());              break;          case QXmlStreamReader::EndElement: -            if (!endElement(reader.name())) { -                return false; -            } +            endElement(reader.name());              break;          case QXmlStreamReader::Characters:              if (!reader.isWhitespace() && !reader.text().trimmed().isEmpty()) { -                if (!characters(reader.text())) { -                    return false; -                } +                m_cdata.append(reader.text());              }              break;          default: @@ -77,15 +71,19 @@ bool ConfigLoaderHandler::parse(QIODevice *input)      return true;  } -bool ConfigLoaderHandler::startElement(const QStringRef &localName, const QXmlStreamAttributes &attrs) +static bool caseInsensitiveCompare(const QStringView a, const QLatin1String b) +{ +    return a.compare(b, Qt::CaseInsensitive) == 0; +} + +void ConfigLoaderHandler::startElement(const QStringView localName, const QXmlStreamAttributes &attrs)  {      // qDebug() << "ConfigLoaderHandler::startElement(" << localName << qName; -    const QString tag = localName.toString().toLower(); -    if (tag == QLatin1String("group")) { +    if (caseInsensitiveCompare(localName, QLatin1String("group"))) {          QString group;          for (const auto &attr : attrs) { -            const QStringRef name = attr.name(); -            if (name.compare(QLatin1String("name"), Qt::CaseInsensitive) == 0) { +            const auto name = attr.name(); +            if (caseInsensitiveCompare(name, QLatin1String("name"))) {                  // qDebug() << "set group to" << attrs.value(i);                  group = attr.value().toString();              } @@ -102,71 +100,61 @@ bool ConfigLoaderHandler::startElement(const QStringRef &localName, const QXmlSt          if (m_config) {              m_config->setCurrentGroup(group);          } -    } else if (tag == QLatin1String("entry")) { +    } else if (caseInsensitiveCompare(localName, QLatin1String("entry"))) {          for (const auto &attr : attrs) { -            const QStringRef name = attr.name(); -            if (name.compare(QLatin1String("name"), Qt::CaseInsensitive) == 0) { +            const auto name = attr.name(); +            if (caseInsensitiveCompare(name, QLatin1String("name"))) {                  m_name = attr.value().trimmed().toString(); -            } else if (name.compare(QLatin1String("type"), Qt::CaseInsensitive) == 0) { +            } else if (caseInsensitiveCompare(name, QLatin1String("type"))) {                  m_type = attr.value().toString().toLower(); -            } else if (name.compare(QLatin1String("key"), Qt::CaseInsensitive) == 0) { +            } else if (caseInsensitiveCompare(name, QLatin1String("key"))) {                  m_key = attr.value().trimmed().toString();              }          } -    } else if (tag == QLatin1String("choice")) { +    } else if (caseInsensitiveCompare(localName, QLatin1String("choice"))) {          m_choice.name.clear();          m_choice.label.clear();          m_choice.whatsThis.clear();          for (const auto &attr : attrs) { -            const QStringRef name = attr.name(); -            if (name.compare(QLatin1String("name"), Qt::CaseInsensitive) == 0) { +            const auto name = attr.name(); +            if (caseInsensitiveCompare(name, QLatin1String("name"))) {                  m_choice.name = attr.value().toString();              }          }          m_inChoice = true;      } - -    return true; -} - -bool ConfigLoaderHandler::characters(const QStringRef &ch) -{ -    m_cdata.append(ch.toString()); -    return true;  } -bool ConfigLoaderHandler::endElement(const QStringRef &localName) +void ConfigLoaderHandler::endElement(const QStringView localName)  {      //     qDebug() << "ConfigLoaderHandler::endElement(" << localName << qName; -    const QStringRef tag = localName; -    if (tag.compare(QLatin1String("entry"), Qt::CaseInsensitive) == 0) { +    if (caseInsensitiveCompare(localName, QLatin1String("entry"))) {          addItem();          resetState(); -    } else if (tag.compare(QLatin1String("label"), Qt::CaseInsensitive) == 0) { +    } else if (caseInsensitiveCompare(localName, QLatin1String("label"))) {          if (m_inChoice) {              m_choice.label = m_cdata.trimmed();          } else {              m_label = m_cdata.trimmed();          } -    } else if (tag.compare(QLatin1String("whatsthis"), Qt::CaseInsensitive) == 0) { +    } else if (caseInsensitiveCompare(localName, QLatin1String("whatsthis"))) {          if (m_inChoice) {              m_choice.whatsThis = m_cdata.trimmed();          } else {              m_whatsThis = m_cdata.trimmed();          } -    } else if (tag.compare(QLatin1String("default"), Qt::CaseInsensitive) == 0) { +    } else if (caseInsensitiveCompare(localName, QLatin1String("default"))) {          m_default = m_cdata.trimmed(); -    } else if (tag.compare(QLatin1String("min"), Qt::CaseInsensitive) == 0) { +    } else if (caseInsensitiveCompare(localName, QLatin1String("min"))) {          m_min = m_cdata.toInt(&m_haveMin); -    } else if (tag.compare(QLatin1String("max"), Qt::CaseInsensitive) == 0) { +    } else if (caseInsensitiveCompare(localName, QLatin1String("max"))) {          m_max = m_cdata.toInt(&m_haveMax); -    } else if (tag.compare(QLatin1String("choice"), Qt::CaseInsensitive) == 0) { +    } else if (caseInsensitiveCompare(localName, QLatin1String("choice"))) {          m_enumChoices.append(m_choice);          m_inChoice = false;      }      m_cdata.clear(); -    return true;  }  void ConfigLoaderHandler::addItem() diff --git a/src/gui/kconfigloaderhandler_p.h b/src/gui/kconfigloaderhandler_p.h index bc199e0e..297e5462 100644 --- a/src/gui/kconfigloaderhandler_p.h +++ b/src/gui/kconfigloaderhandler_p.h @@ -17,9 +17,8 @@ public:      bool parse(QIODevice *input); -    bool startElement(const QStringRef &localName, const QXmlStreamAttributes &attrs); -    bool endElement(const QStringRef &localName); -    bool characters(const QStringRef &ch); +    void startElement(const QStringView localName, const QXmlStreamAttributes &attrs); +    void endElement(const QStringView localName);  private:      void addItem(); diff --git a/src/kconf_update/kconfigutils.cpp b/src/kconf_update/kconfigutils.cpp index 49d5ac3c..9608f21c 100644 --- a/src/kconf_update/kconfigutils.cpp +++ b/src/kconf_update/kconfigutils.cpp @@ -89,7 +89,11 @@ QString unescapeString(const QString &src, bool *ok, QString *error)                  break;              case L'x': {                  if (pos + 2 < length) { +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +                    char value = QStringView(src).mid(pos + 1, 2).toInt(ok, 16); +#else                      char value = src.midRef(pos + 1, 2).toInt(ok, 16); +#endif                      if (*ok) {                          dst += QLatin1Char{value};                          pos += 2; | 
