Commit 6d153f05 authored by heretik's avatar heretik

make usage of std::regex instead of QRegularExpression beauce of valgrind errors

parent 2c835b4c
......@@ -63,13 +63,13 @@ SyntaxHighlighter::SyntaxHighlighter( QTextDocument *parent )
auto format_keyword = QTextCharFormat{};
format_keyword.setForeground( Qt::darkMagenta );
format_keyword.setFontWeight( QFont::Bold );
regex_and_formats_.emplace_back( QRegularExpression{ QString::fromStdString( os.str() ) }, format_keyword );
regex_and_formats_.emplace_back( std::regex{ os.str() }, format_keyword );
// other regexs
for( const auto &[ regex, color ] : regex_and_colors ) {
auto format = QTextCharFormat{};
format.setForeground( color );
regex_and_formats_.emplace_back( QRegularExpression{ regex }, format );
regex_and_formats_.emplace_back( std::regex{ regex }, format );
}
}
......@@ -87,13 +87,14 @@ void SyntaxHighlighter::highlightBlock( const QString &text )
}
// regex
const auto &text_str = text.toStdString();
for( const auto &[ regex, format ] : regex_and_formats_ ) {
auto merged_format = format;
merged_format.merge( format_to_merge );
auto matchIterator = regex.globalMatch( text );
while( matchIterator.hasNext() ) {
auto match = matchIterator.next();
setFormat( match.capturedStart(), match.capturedLength(), merged_format );
auto begin_it = std::sregex_iterator{ text_str.begin(), text_str.end(), regex };
for( auto it = begin_it; it != std::sregex_iterator{}; ++it ) {
auto match = *it;
setFormat( static_cast<int>( match.position() ), static_cast<int>( match.length() ), merged_format );
}
}
......@@ -104,22 +105,32 @@ void SyntaxHighlighter::highlightBlock( const QString &text )
setCurrentBlockState( end_line_is_not_in_comment );
auto start_index = 0;
if( previousBlockState() != end_line_is_in_comment ) {
start_index = text.indexOf( regex_comment_start_ );
auto match = std::smatch{};
if( std::regex_search( text_str.begin(), text_str.end(), match, regex_comment_start_ ) ) {
start_index = static_cast<int>( match.position() );
} else {
start_index = -1;
}
}
auto format = format_multiline_comment_;
format.merge( format_to_merge );
while( start_index >= 0 ) {
auto match = QRegularExpressionMatch{ regex_comment_end_.match( text, start_index ) };
auto end_index = match.capturedStart();
auto comment_length = 0;
if( end_index == -1 ) {
auto match = std::smatch{};
auto is_matching = std::regex_search( text_str.begin() + start_index, text_str.end(), match, regex_comment_end_ );
auto comment_length = 0L;
if( not is_matching ) {
setCurrentBlockState( end_line_is_in_comment );
comment_length = text.length() - start_index;
comment_length = static_cast<long>( text_str.length() - static_cast<unsigned>( start_index ) );
} else {
auto end_index = start_index + match.position();
comment_length = end_index - start_index + match.length();
}
setFormat( start_index, static_cast<int>( comment_length ), format );
if( std::regex_search( text_str.begin() + start_index + comment_length, text_str.end(), match, regex_comment_start_ ) ) {
start_index = static_cast<int>( start_index + comment_length + match.position() );
} else {
comment_length = end_index - start_index + match.capturedLength();
start_index = -1;
}
setFormat( start_index, comment_length, format );
start_index = text.indexOf( regex_comment_start_, start_index + comment_length );
}
}
......@@ -2,8 +2,8 @@
#ifndef SYNTAX_HIGHLIGHTER_HPP
#define SYNTAX_HIGHLIGHTER_HPP
#include <QRegularExpression>
#include <QSyntaxHighlighter>
#include <regex>
#include <vector>
class SyntaxHighlighter
......@@ -21,9 +21,9 @@ private:
QTextCharFormat format_error_;
QTextCharFormat format_warning_;
QTextCharFormat format_multiline_comment_;
QRegularExpression regex_comment_start_;
QRegularExpression regex_comment_end_;
std::vector<std::pair<QRegularExpression, QTextCharFormat>> regex_and_formats_;
std::regex regex_comment_start_;
std::regex regex_comment_end_;
std::vector<std::pair<std::regex, QTextCharFormat>> regex_and_formats_;
};
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment