diff options
Diffstat (limited to 'src/libs/3rdparty/syntax-highlighting/src/lib/keywordlist.cpp')
-rw-r--r-- | src/libs/3rdparty/syntax-highlighting/src/lib/keywordlist.cpp | 106 |
1 files changed, 52 insertions, 54 deletions
diff --git a/src/libs/3rdparty/syntax-highlighting/src/lib/keywordlist.cpp b/src/libs/3rdparty/syntax-highlighting/src/lib/keywordlist.cpp index f042baac27..847f6af6d4 100644 --- a/src/libs/3rdparty/syntax-highlighting/src/lib/keywordlist.cpp +++ b/src/libs/3rdparty/syntax-highlighting/src/lib/keywordlist.cpp @@ -1,39 +1,43 @@ /* - Copyright (C) 2016 Volker Krause <vkrause@kde.org> - - Permission is hereby granted, free of charge, to any person obtaining - a copy of this software and associated documentation files (the - "Software"), to deal in the Software without restriction, including - without limitation the rights to use, copy, modify, merge, publish, - distribute, sublicense, and/or sell copies of the Software, and to - permit persons to whom the Software is furnished to do so, subject to - the following conditions: - - The above copyright notice and this permission notice shall be included - in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org> + SPDX-FileCopyrightText: 2020 Jonathan Poelen <jonathan.poelen@gmail.com> + + SPDX-License-Identifier: MIT */ -#include "keywordlist_p.h" -#include "repository.h" #include "definition_p.h" +#include "keywordlist_p.h" #include "ksyntaxhighlighting_logging.h" +#include "repository.h" -#include <QDebug> #include <QXmlStreamReader> #include <algorithm> using namespace KSyntaxHighlighting; -bool KeywordList::contains(const QStringRef &str, Qt::CaseSensitivity caseSensitive) const +namespace +{ +struct KeywordComparator { + Qt::CaseSensitivity caseSensitive; + + bool operator()(QStringView a, QStringView b) const + { + if (a.size() < b.size()) { + return true; + } + + if (a.size() > b.size()) { + return false; + } + + return a.compare(b, caseSensitive) < 0; + } +}; + +} + +bool KeywordList::contains(QStringView str, Qt::CaseSensitivity caseSensitive) const { /** * get right vector to search in @@ -43,37 +47,36 @@ bool KeywordList::contains(const QStringRef &str, Qt::CaseSensitivity caseSensit /** * search with right predicate */ - return std::binary_search(vectorToSearch.begin(), vectorToSearch.end(), str, [caseSensitive] (const QStringRef &a, const QStringRef &b) { return a.compare(b, caseSensitive) < 0; }); + return std::binary_search(vectorToSearch.begin(), vectorToSearch.end(), str, KeywordComparator{caseSensitive}); } -void KeywordList::load(QXmlStreamReader& reader) +void KeywordList::load(QXmlStreamReader &reader) { Q_ASSERT(reader.name() == QLatin1String("list")); Q_ASSERT(reader.tokenType() == QXmlStreamReader::StartElement); - m_name = reader.attributes().value(QStringLiteral("name")).toString(); + m_name = reader.attributes().value(QLatin1String("name")).toString(); while (!reader.atEnd()) { switch (reader.tokenType()) { - case QXmlStreamReader::StartElement: - if (reader.name() == QLatin1String("item")) { - m_keywords.append(reader.readElementText().trimmed()); - reader.readNextStartElement(); - break; - } - else if (reader.name() == QLatin1String("include")) { - m_includes.append(reader.readElementText().trimmed()); - reader.readNextStartElement(); - break; - } - reader.readNext(); + case QXmlStreamReader::StartElement: + if (reader.name() == QLatin1String("item")) { + m_keywords.append(reader.readElementText().trimmed()); + reader.readNextStartElement(); break; - case QXmlStreamReader::EndElement: - reader.readNext(); - return; - default: - reader.readNext(); + } else if (reader.name() == QLatin1String("include")) { + m_includes.append(reader.readElementText().trimmed()); + reader.readNextStartElement(); break; + } + reader.readNext(); + break; + case QXmlStreamReader::EndElement: + reader.readNext(); + return; + default: + reader.readNext(); + break; } } } @@ -100,15 +103,12 @@ void KeywordList::initLookupForCaseSensitivity(Qt::CaseSensitivity caseSensitive /** * fill vector with refs to keywords */ - vectorToSort.reserve(m_keywords.size()); - for (const auto &keyword : qAsConst(m_keywords)) { - vectorToSort.push_back(&keyword); - } + vectorToSort.assign(m_keywords.constBegin(), m_keywords.constEnd()); /** * sort with right predicate */ - std::sort(vectorToSort.begin(), vectorToSort.end(), [caseSensitive] (const QStringRef &a, const QStringRef &b) { return a.compare(b, caseSensitive) < 0; }); + std::sort(vectorToSort.begin(), vectorToSort.end(), KeywordComparator{caseSensitive}); } void KeywordList::resolveIncludeKeywords(DefinitionData &def) @@ -121,15 +121,14 @@ void KeywordList::resolveIncludeKeywords(DefinitionData &def) KeywordList *keywords = nullptr; if (idx >= 0) { - auto listName = kw_include.left(idx); auto defName = kw_include.mid(idx + 2); auto includeDef = def.repo->definitionForName(defName); if (includeDef.isValid()) { + auto listName = kw_include.left(idx); auto defData = DefinitionData::get(includeDef); defData->load(DefinitionData::OnlyKeywords(true)); keywords = defData->keywordList(listName); - } - else { + } else { qCWarning(Log) << "Unable to resolve external include keyword for definition" << defName << "in" << def.name; } } else { @@ -141,8 +140,7 @@ void KeywordList::resolveIncludeKeywords(DefinitionData &def) keywords->resolveIncludeKeywords(def); } m_keywords += keywords->m_keywords; - } - else { + } else { qCWarning(Log) << "Unresolved include keyword" << kw_include << "in" << def.name; } } |