KSyntaxHighlighting: Use QStringView instead of QStringRef

QStringRef will be removed for Qt6, or moved into a Qt 5 compatibility
library, but the QString API will be removed.
Switch to QStringView instead.

Task-number: QTCREATORBUG-24098
Change-Id: Ia3cab3de24ba36b5db64e1eff18d92e66ccd3d94
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Eike Ziller
2020-09-07 09:56:49 +02:00
parent 5de5686179
commit 6b9cad8b05
10 changed files with 38 additions and 31 deletions

View File

@@ -44,7 +44,7 @@ Context *ContextSwitch::context() const
return m_context;
}
void ContextSwitch::parse(const QStringRef &contextInstr)
void ContextSwitch::parse(const QStringView &contextInstr)
{
if (contextInstr.isEmpty() || contextInstr == QLatin1String("#stay"))
return;

View File

@@ -42,7 +42,7 @@ public:
int popCount() const;
Context *context() const;
void parse(const QStringRef &contextInstr);
void parse(const QStringView &contextInstr);
void resolve(const Definition &def);
private:

View File

@@ -779,15 +779,15 @@ void DefinitionData::loadSpellchecking(QXmlStreamReader &reader)
}
}
bool DefinitionData::checkKateVersion(const QStringRef &verStr)
bool DefinitionData::checkKateVersion(const QStringView &verStr)
{
const auto idx = verStr.indexOf(QLatin1Char('.'));
if (idx <= 0) {
qCWarning(Log) << "Skipping" << fileName << "due to having no valid kateversion attribute:" << verStr;
return false;
}
const auto major = verStr.left(idx).toInt();
const auto minor = verStr.mid(idx + 1).toInt();
const auto major = verStr.left(idx).toString().toInt();
const auto minor = verStr.mid(idx + 1).toString().toInt();
if (major > SyntaxHighlighting_VERSION_MAJOR || (major == SyntaxHighlighting_VERSION_MAJOR && minor > SyntaxHighlighting_VERSION_MINOR)) {
qCWarning(Log) << "Skipping" << fileName << "due to being too new, version:" << verStr;

View File

@@ -68,7 +68,7 @@ public:
void loadComments(QXmlStreamReader &reader);
void loadFoldingIgnoreList(QXmlStreamReader &reader);
void loadSpellchecking(QXmlStreamReader &reader);
bool checkKateVersion(const QStringRef &verStr);
bool checkKateVersion(const QStringView &verStr);
void resolveIncludeKeywords();

View File

@@ -35,7 +35,7 @@
using namespace KSyntaxHighlighting;
static Theme::TextStyle stringToDefaultFormat(const QStringRef &str)
static Theme::TextStyle stringToDefaultFormat(const QStringView &str)
{
if (!str.startsWith(QLatin1String("ds")))
return Theme::Normal;
@@ -243,7 +243,7 @@ void FormatPrivate::load(QXmlStreamReader &reader)
name = reader.attributes().value(QStringLiteral("name")).toString();
defaultStyle = stringToDefaultFormat(reader.attributes().value(QStringLiteral("defStyleNum")));
QStringRef attribute = reader.attributes().value(QStringLiteral("color"));
QStringView attribute = reader.attributes().value(QStringLiteral("color"));
if (!attribute.isEmpty()) {
style.textColor = QColor(attribute.toString()).rgba();
}

View File

@@ -32,7 +32,7 @@
using namespace KSyntaxHighlighting;
bool KeywordList::contains(const QStringRef &str, Qt::CaseSensitivity caseSensitive) const
bool KeywordList::contains(const QStringView &str, Qt::CaseSensitivity caseSensitive) const
{
/**
* get right vector to search in
@@ -42,7 +42,12 @@ 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,
[caseSensitive](const QStringView &a, const QStringView &b) {
return a.compare(b, caseSensitive) < 0;
});
}
void KeywordList::load(QXmlStreamReader &reader)
@@ -100,13 +105,17 @@ void KeywordList::initLookupForCaseSensitivity(Qt::CaseSensitivity caseSensitive
*/
vectorToSort.reserve(m_keywords.size());
for (const auto &keyword : qAsConst(m_keywords)) {
vectorToSort.push_back(&keyword);
vectorToSort.emplace_back(keyword);
}
/**
* 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(),
[caseSensitive](const QStringView &a, const QStringView &b) {
return a.compare(b, caseSensitive) < 0;
});
}
void KeywordList::resolveIncludeKeywords(DefinitionData &def)

View File

@@ -68,13 +68,10 @@ public:
}
/** Checks if @p str is a keyword in this list. */
bool contains(const QStringRef &str) const
{
return contains(str, m_caseSensitive);
}
bool contains(const QStringView &str) const { return contains(str, m_caseSensitive); }
/** Checks if @p str is a keyword in this list, overriding the global case-sensitivity setting. */
bool contains(const QStringRef &str, Qt::CaseSensitivity caseSensitive) const;
bool contains(const QStringView &str, Qt::CaseSensitivity caseSensitive) const;
void load(QXmlStreamReader &reader);
void setCaseSensitivity(Qt::CaseSensitivity caseSensitive);
@@ -105,12 +102,12 @@ private:
/**
* case-sensitive sorted string references to m_keywords for lookup
*/
std::vector<QStringRef> m_keywordsSortedCaseSensitive;
std::vector<QStringView> m_keywordsSortedCaseSensitive;
/**
* case-insensitive sorted string references to m_keywords for lookup
*/
std::vector<QStringRef> m_keywordsSortedCaseInsensitive;
std::vector<QStringView> m_keywordsSortedCaseInsensitive;
};
}

View File

@@ -134,7 +134,7 @@ void Rule::resolveContext()
m_context.resolve(m_def.definition());
// cache for DefinitionData::wordDelimiters, is accessed VERY often
m_wordDelimiter = &DefinitionData::get(m_def.definition())->wordDelimiters;
m_wordDelimiter = DefinitionData::get(m_def.definition())->wordDelimiters;
}
void Rule::resolveAttributeFormat(Context *lookupContext)
@@ -156,7 +156,7 @@ bool Rule::doLoad(QXmlStreamReader &reader)
return true;
}
Rule::Ptr Rule::create(const QStringRef &name)
Rule::Ptr Rule::create(const QStringView &name)
{
Rule *rule = nullptr;
if (name == QLatin1String("AnyChar"))
@@ -418,7 +418,7 @@ bool IncludeRules::includeAttribute() const
bool IncludeRules::doLoad(QXmlStreamReader &reader)
{
const auto s = reader.attributes().value(QLatin1String("context"));
const auto s = reader.attributes().value(QLatin1String("context")).toString();
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
const auto split = s.split(QLatin1String("##"), QString::KeepEmptyParts);
#else
@@ -426,9 +426,9 @@ bool IncludeRules::doLoad(QXmlStreamReader &reader)
#endif
if (split.isEmpty())
return false;
m_contextName = split.at(0).toString();
m_contextName = split.at(0);
if (split.size() > 1)
m_defName = split.at(1).toString();
m_defName = split.at(1);
m_includeAttribute = Xml::attrToBool(reader.attributes().value(QLatin1String("includeAttrib")));
return !m_contextName.isEmpty() || !m_defName.isEmpty();
@@ -486,10 +486,11 @@ MatchResult KeywordListRule::doMatch(const QString &text, int offset, const QStr
return offset;
if (m_hasCaseSensitivityOverride) {
if (m_keywordList->contains(text.midRef(offset, newOffset - offset), m_caseSensitivityOverride))
if (m_keywordList->contains(QStringView(text).mid(offset, newOffset - offset),
m_caseSensitivityOverride))
return newOffset;
} else {
if (m_keywordList->contains(text.midRef(offset, newOffset - offset)))
if (m_keywordList->contains(QStringView(text).mid(offset, newOffset - offset)))
return newOffset;
}
@@ -613,7 +614,7 @@ MatchResult StringDetect::doMatch(const QString &text, int offset, const QString
*/
const auto &pattern = m_dynamic ? replaceCaptures(m_string, captures, false) : m_string;
if (text.midRef(offset, pattern.size()).compare(pattern, m_caseSensitivity) == 0)
if (QStringView(text).mid(offset, pattern.size()).compare(pattern, m_caseSensitivity) == 0)
return offset + pattern.size();
return offset;
}
@@ -637,7 +638,7 @@ MatchResult WordDetect::doMatch(const QString &text, int offset, const QStringLi
if (offset > 0 && !isWordDelimiter(text.at(offset - 1)) && !isWordDelimiter(text.at(offset)))
return offset;
if (text.midRef(offset, m_word.size()).compare(m_word, m_caseSensitivity) != 0)
if (QStringView(text).mid(offset, m_word.size()).compare(m_word, m_caseSensitivity) != 0)
return offset;
if (text.size() == offset + m_word.size() || isWordDelimiter(text.at(offset + m_word.size())) || isWordDelimiter(text.at(offset + m_word.size() - 1)))

View File

@@ -100,7 +100,7 @@ public:
virtual MatchResult doMatch(const QString &text, int offset, const QStringList &captures) const = 0;
static Rule::Ptr create(const QStringRef &name);
static Rule::Ptr create(const QStringView &name);
protected:
virtual bool doLoad(QXmlStreamReader &reader);
@@ -121,7 +121,7 @@ private:
bool m_lookAhead = false;
// cache for DefinitionData::wordDelimiters, is accessed VERY often
QStringRef m_wordDelimiter;
QStringView m_wordDelimiter;
protected:
bool m_dynamic = false;

View File

@@ -32,7 +32,7 @@ namespace KSyntaxHighlighting
namespace Xml
{
/** Parse a xs:boolean attribute. */
inline bool attrToBool(const QStringRef &str)
inline bool attrToBool(const QStringView &str)
{
return str == QLatin1String("1") || str.compare(QLatin1String("true"), Qt::CaseInsensitive) == 0;
}