Skip to content

Commit

Permalink
Merge pull request #1618 from Waqar144/develop
Browse files Browse the repository at this point in the history
Optimize CodeToHTMLConverter
  • Loading branch information
pbek authored Feb 7, 2020
2 parents fb8faf4 + a55af40 commit e5b73fe
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 70 deletions.
90 changes: 51 additions & 39 deletions src/helpers/codetohtmlconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ CodeToHtmlConverter::CodeToHtmlConverter(const QStringRef input, const QString &
if (_langStringToEnum.isEmpty())
initCodeLangs();

currentLang = _langStringToEnum.value(_lang);
_currentLang = _langStringToEnum.value(_lang);
}

void CodeToHtmlConverter::initCodeLangs() const Q_DECL_NOTHROW
void CodeToHtmlConverter::initCodeLangs() Q_DECL_NOTHROW
{
CodeToHtmlConverter::_langStringToEnum =
if (!_langStringToEnum.isEmpty())
return;
_langStringToEnum =
QHash<QString, CodeToHtmlConverter::Lang> {
{QStringLiteral("bash"), CodeToHtmlConverter::CodeBash},
{QStringLiteral("c"), CodeToHtmlConverter::CodeC},
Expand Down Expand Up @@ -68,7 +70,7 @@ QString CodeToHtmlConverter::process() const
QChar comment;
QString output = QLatin1String("");

switch(currentLang) {
switch(_currentLang) {
case CodeCpp:
loadCppData(types, keywords, builtin, literals, others);
break;
Expand Down Expand Up @@ -247,7 +249,7 @@ int CodeToHtmlConverter::highlightNumericLit(QString &output, int i) const
isPreAllowed = true;
break;
case ':':
if (currentLang == CodeCSS) {
if (_currentLang == CodeCSS) {
isPreAllowed = true;
}
break;
Expand Down Expand Up @@ -304,13 +306,13 @@ int CodeToHtmlConverter::highlightNumericLit(QString &output, int i) const
isPostAllowed = true;
break;
case 'p':
if (currentLang == CodeCSS && _input.at(i+2) == QLatin1Char('x'))
if (_currentLang == CodeCSS && _input.at(i+2) == QLatin1Char('x'))
if ( (i+3 < _input.length()) &&
(_input.at(i+3) == QLatin1Char(';') || _input.at(i+3) == QLatin1Char('\n')) )
isPostAllowed = true;
break;
case 'e':
if (currentLang == CodeCSS && _input.at(i+2) == QLatin1Char('m'))
if (_currentLang == CodeCSS && _input.at(i+2) == QLatin1Char('m'))
if ( (i+3 < _input.length()) &&
(_input.at(i+3) == QLatin1Char(';') || _input.at(i+3) == QLatin1Char('\n')) )
isPostAllowed = true;
Expand Down Expand Up @@ -469,9 +471,9 @@ int CodeToHtmlConverter::highlightWord(int i, const LangData &data, QString &out
return i;
// check if we are at the beginning OR if this is the start of a word
// AND the current char is present in the data structure
if ( ( i == 0 || !_input.at(i-1).isLetter()) && data.contains(_input.at(i).toLatin1())) {
if (( i == 0 || !_input.at(i-1).isLetter()) && data.contains(_input.at(i).toLatin1())) {
const auto wordList = data.values(_input.at(i).toLatin1());
for(const auto &word : wordList) {
for (const auto &word : wordList) {
if (word == _input.mid(i, word.size())) {
//check if we are at the end of text OR if we have a complete word
if ( i + word.size() == _input.length() || !_input.at(i + word.size()).isLetter()) {
Expand All @@ -484,29 +486,6 @@ int CodeToHtmlConverter::highlightWord(int i, const LangData &data, QString &out
return i;
}

QString CodeToHtmlConverter::escape(QChar c) const
{
switch (c.toLatin1()) {
case '\'':
return QStringLiteral("&#39;");
case '"':
return QStringLiteral("&quot;");
case '&':
return QStringLiteral("&amp;");
case '<':
return QStringLiteral("&lt;");
case '>':
return QStringLiteral("&gt;");
case '/':
return QStringLiteral("&#47;");
case '~':
return QStringLiteral("&#126;");
case '`':
return QStringLiteral("&#96;");
}
return c;
}

QString CodeToHtmlConverter::xmlHighlighter() const {
if (_input.isEmpty()) return QLatin1String("");
const auto textLen = _input.length();
Expand Down Expand Up @@ -686,7 +665,7 @@ QString CodeToHtmlConverter::ymlHighlighter() const {
} else if (_input.at(i) == QLatin1Char('#')) {
i = highlightComment(output, i);
} else {
int colon = _input.indexOf(QLatin1Char(':'), i);
const int colon = _input.indexOf(QLatin1Char(':'), i);
if (colon > 0) {
//move i to the beginning of the word
int cursor = i;
Expand All @@ -698,9 +677,9 @@ QString CodeToHtmlConverter::ymlHighlighter() const {

output += setFormat(_input.mid(i, colon - i), Format::Keyword);
i = colon;
int endLine = _input.indexOf(QLatin1Char('\n'), i);
const int endLine = _input.indexOf(QLatin1Char('\n'), i);
if (endLine > 0) {
QStringRef line = _input.mid(i, endLine - i);
const QStringRef line = _input.mid(i, endLine - i);
if (line.contains(QLatin1Char('#'))) {
int hashPos = _input.indexOf(QLatin1Char('#'), i);
//first add everything till the # into output
Expand Down Expand Up @@ -734,8 +713,8 @@ QString CodeToHtmlConverter::iniHighlighter() const {

for (int i = 0; i < textLen; ++i) {
//start of a [section]
if (_input.at(i) == QChar('[')) {
int sectionEnd = _input.indexOf(QChar(']'), i);
if (_input.at(i) == QLatin1Char('[')) {
int sectionEnd = _input.indexOf(QLatin1Char(']'), i);
if (sectionEnd == -1)
sectionEnd = _input.indexOf(QLatin1Char('\n'), i);
else
Expand Down Expand Up @@ -772,7 +751,30 @@ QString CodeToHtmlConverter::iniHighlighter() const {
return output;
}

QString CodeToHtmlConverter::escapeString(const QStringRef s) const
QString CodeToHtmlConverter::escape(QChar c)
{
switch (c.toLatin1()) {
case '\'':
return QStringLiteral("&#39;");
case '"':
return QStringLiteral("&quot;");
case '&':
return QStringLiteral("&amp;");
case '<':
return QStringLiteral("&lt;");
case '>':
return QStringLiteral("&gt;");
case '/':
return QStringLiteral("&#47;");
case '~':
return QStringLiteral("&#126;");
case '`':
return QStringLiteral("&#96;");
}
return c;
}

QString CodeToHtmlConverter::escapeString(const QStringRef s)
{
QString ret = QLatin1String("");
ret.reserve(s.length());
Expand All @@ -782,7 +784,17 @@ QString CodeToHtmlConverter::escapeString(const QStringRef s) const
return ret;
}

QString CodeToHtmlConverter::setFormat(const QStringRef str, CodeToHtmlConverter::Format format) const

static const QString keywordTagBegin = QStringLiteral("<span class=\"code-keyword\">");
static const QString typeTagBegin = QStringLiteral("<span class=\"code-type\">");
static const QString literalTagBegin = QStringLiteral("<span class=\"code-literal\">");
static const QString commentTagBegin = QStringLiteral("<span class=\"code-comment\">");
static const QString builtinTagBegin = QStringLiteral("<span class=\"code-builtin\">");
static const QString otherTagBegin = QStringLiteral("<span class=\"code-other\">");
static const QString stringTagBegin = QStringLiteral("<span class=\"code-string\">");
static const QString spanEnd = QStringLiteral("</span>");

QString CodeToHtmlConverter::setFormat(const QStringRef str, CodeToHtmlConverter::Format format)
{
switch(format) {
case Type:
Expand Down
50 changes: 19 additions & 31 deletions src/helpers/codetohtmlconverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,56 +47,44 @@ class CodeToHtmlConverter
};
public:
CodeToHtmlConverter(const QStringRef input, const QString &lang) Q_DECL_NOTHROW;
QString process() const;
Q_REQUIRED_RESULT QString process() const;

private:
const QStringRef _input;
const QString _lang;
Lang currentLang;
Lang _currentLang;

QString escape(QChar c) const;
QString escapeString(const QStringRef s) const;
QString setFormat(const QStringRef str, Format format) const;
void initCodeLangs() const Q_DECL_NOTHROW;
Q_REQUIRED_RESULT static QString escape(QChar c);
Q_REQUIRED_RESULT static QString escapeString(const QStringRef s);
Q_REQUIRED_RESULT static QString setFormat(const QStringRef str, Format format);
static void initCodeLangs() Q_DECL_NOTHROW;

int highlightNumericLit(QString &output, int i) const;
int highlightStringLiterals(QChar strType, QString &output, int i) const;
int highlightComment(QString &output, int i, bool isSingleLine = true) const;
int highlightWord(int i, const LangData &data,
QString &output, Format f) const;
QString xmlHighlighter() const;
QString cssHighlighter(const LangData &types,
const LangData &keywords) const;
QString ymlHighlighter() const;
QString iniHighlighter() const;
Q_REQUIRED_RESULT int highlightNumericLit(QString &output, int i) const;
Q_REQUIRED_RESULT int highlightStringLiterals(QChar strType, QString &output, int i) const;
Q_REQUIRED_RESULT int highlightComment(QString &output, int i, bool isSingleLine = true) const;
Q_REQUIRED_RESULT int highlightWord(int i, const LangData &data, QString &output, Format f) const;
Q_REQUIRED_RESULT QString xmlHighlighter() const;
Q_REQUIRED_RESULT QString cssHighlighter(const LangData &types, const LangData &keywords) const;
Q_REQUIRED_RESULT QString ymlHighlighter() const;
Q_REQUIRED_RESULT QString iniHighlighter() const;

/**
* @brief returns true if c is octal
*/
static inline bool isOctal(const char c) {
Q_REQUIRED_RESULT static inline bool isOctal(const char c) {
return (c >= '0' && c <= '7');
}

/**
* @brief returns true if c is hex
*/
static inline bool isHex(const char c) {
return (
(c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'f') ||
(c >= 'A' && c <= 'F'));
Q_REQUIRED_RESULT static inline bool isHex(const char c) {
return ((c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'f') ||
(c >= 'A' && c <= 'F'));
}

static QHash<QString, Lang> _langStringToEnum;

const QString keywordTagBegin = QStringLiteral("<span class=\"code-keyword\">");
const QString typeTagBegin = QStringLiteral("<span class=\"code-type\">");
const QString literalTagBegin = QStringLiteral("<span class=\"code-literal\">");
const QString commentTagBegin = QStringLiteral("<span class=\"code-comment\">");
const QString builtinTagBegin = QStringLiteral("<span class=\"code-builtin\">");
const QString otherTagBegin = QStringLiteral("<span class=\"code-other\">");
const QString stringTagBegin = QStringLiteral("<span class=\"code-string\">");
const QString spanEnd = QStringLiteral("</span>");
};

#endif // CODETOHTMLCONVERTER_H

0 comments on commit e5b73fe

Please sign in to comment.