diff --git a/src/lib/htmlhighlighter.cpp b/src/lib/htmlhighlighter.cpp --- a/src/lib/htmlhighlighter.cpp +++ b/src/lib/htmlhighlighter.cpp @@ -1,5 +1,6 @@ /* Copyright (C) 2016 Volker Krause + Copyright (C) 2018 Christoph Cullmann Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -32,6 +33,7 @@ #include #include #include +#include using namespace KSyntaxHighlighting; @@ -114,25 +116,32 @@ if (length == 0) return; - if (!format.isDefaultTextStyle(theme())) { + // collect potential output, cheaper than thinking about "is there any?" + QVarLengthArray formatOutput; + if (format.hasTextColor(theme())) + formatOutput << QStringLiteral("color:") << format.textColor(theme()).name() << QStringLiteral(";"); + if (format.hasBackgroundColor(theme())) + formatOutput << QStringLiteral("background-color:") << format.backgroundColor(theme()).name() << QStringLiteral(";"); + if (format.isBold(theme())) + formatOutput << QStringLiteral("font-weight:bold;"); + if (format.isItalic(theme())) + formatOutput << QStringLiteral("font-style:italic;"); + if (format.isUnderline(theme())) + formatOutput << QStringLiteral("text-decoration:underline;"); + if (format.isStrikeThrough(theme())) + formatOutput << QStringLiteral("text-decoration:line-through;"); + + if (!formatOutput.isEmpty()) { *d->out << "out << "color:" << format.textColor(theme()).name() << ";"; - if (format.hasBackgroundColor(theme())) - *d->out << "background-color:" << format.backgroundColor(theme()).name() << ";"; - if (format.isBold(theme())) - *d->out << "font-weight:bold;"; - if (format.isItalic(theme())) - *d->out << "font-style:italic;"; - if (format.isUnderline(theme())) - *d->out << "text-decoration:underline;"; - if (format.isStrikeThrough(theme())) - *d->out << "text-decoration:line-through;"; + for (const auto &out : qAsConst(formatOutput)) { + *d->out << out; + } *d->out << "\">"; } *d->out << d->currentLine.mid(offset, length).toHtmlEscaped(); - if (!format.isDefaultTextStyle(theme())) + if (!formatOutput.isEmpty()) { *d->out << ""; + } } diff --git a/src/lib/rule.cpp b/src/lib/rule.cpp --- a/src/lib/rule.cpp +++ b/src/lib/rule.cpp @@ -1,5 +1,6 @@ /* Copyright (C) 2016 Volker Krause + Copyright (C) 2018 Christoph Cullmann Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -99,6 +100,9 @@ void Rule::setDefinition(const Definition &def) { m_def = def; + + // cache for DefinitionData::wordDelimiters, is accessed VERY often + m_wordDelimiter = &DefinitionData::get(m_def.definition())->wordDelimiters; } QString Rule::attribute() const @@ -273,8 +277,8 @@ bool Rule::isWordDelimiter(QChar c) const { - auto defData = DefinitionData::get(m_def.definition()); - return defData->isWordDelimiter(c); + // perf tells contains is MUCH faster than binary search here, very short array + return m_wordDelimiter.contains(c); } @@ -569,7 +573,9 @@ if (m_keywordList.contains(text.midRef(offset, newOffset - offset))) return newOffset; } - return offset; + + // we don't match, but we can skip until newOffset as we can't start a keyword in-between + return MatchResult(offset, newOffset); } diff --git a/src/lib/rule_p.h b/src/lib/rule_p.h --- a/src/lib/rule_p.h +++ b/src/lib/rule_p.h @@ -88,6 +88,9 @@ bool m_firstNonSpace = false; bool m_lookAhead = false; bool m_dynamic = false; + + // cache for DefinitionData::wordDelimiters, is accessed VERY often + QStringRef m_wordDelimiter; };