23
23
*/
24
24
25
25
#include " BasicXMLSyntaxHighlighter.h"
26
+ #include < QRegularExpressionMatchIterator>
26
27
27
- BasicXMLSyntaxHighlighter::BasicXMLSyntaxHighlighter (QObject * parent) :
28
- QSyntaxHighlighter(parent)
28
+ BasicXMLSyntaxHighlighter::BasicXMLSyntaxHighlighter (QObject *parent) : QSyntaxHighlighter(parent)
29
29
{
30
30
setRegexes ();
31
31
setFormats ();
32
32
}
33
33
34
- BasicXMLSyntaxHighlighter::BasicXMLSyntaxHighlighter (QTextDocument * parent) :
35
- QSyntaxHighlighter(parent)
34
+ BasicXMLSyntaxHighlighter::BasicXMLSyntaxHighlighter (QTextDocument *parent) : QSyntaxHighlighter(parent)
36
35
{
37
36
setRegexes ();
38
37
setFormats ();
39
38
}
40
39
41
- BasicXMLSyntaxHighlighter::BasicXMLSyntaxHighlighter (QTextEdit * parent) :
42
- QSyntaxHighlighter(parent)
40
+ BasicXMLSyntaxHighlighter::BasicXMLSyntaxHighlighter (QTextEdit *parent) : QSyntaxHighlighter(parent)
43
41
{
44
42
setRegexes ();
45
43
setFormats ();
46
44
}
47
45
48
- void BasicXMLSyntaxHighlighter::highlightBlock (const QString & text)
46
+ void BasicXMLSyntaxHighlighter::highlightBlock (const QString &text)
49
47
{
50
48
// Special treatment for xml element regex as we use captured text to emulate lookbehind
51
- int xmlElementIndex = m_xmlElementRegex.indexIn (text);
52
- while (xmlElementIndex >= 0 )
53
- {
54
- int matchedPos = m_xmlElementRegex.pos (1 );
55
- int matchedLength = m_xmlElementRegex.cap (1 ).length ();
56
- setFormat (matchedPos, matchedLength, m_xmlElementFormat);
57
-
58
- xmlElementIndex = m_xmlElementRegex.indexIn (text, matchedPos + matchedLength);
49
+ auto matchIt = m_xmlElementRegex.globalMatch (text);
50
+
51
+ while (matchIt.hasNext ()) {
52
+ auto match = matchIt.next ();
53
+ setFormat (match.capturedStart (), match.capturedLength (), m_xmlElementFormat);
59
54
}
60
55
61
56
// Highlight xml keywords *after* xml elements to fix any occasional / captured into the enclosing element
62
- typedef QList<QRegExp>::const_iterator Iter;
63
- Iter xmlKeywordRegexesEnd = m_xmlKeywordRegexes.end ();
64
- for (Iter it = m_xmlKeywordRegexes.begin (); it != xmlKeywordRegexesEnd; ++it) {
65
- const QRegExp & regex = *it;
57
+ for (auto it = m_xmlKeywordRegexes.begin (); it != m_xmlKeywordRegexes.end (); ++it) {
58
+ const QRegularExpression ®ex = *it;
66
59
highlightByRegex (m_xmlKeywordFormat, regex, text);
67
60
}
68
61
@@ -71,17 +64,14 @@ void BasicXMLSyntaxHighlighter::highlightBlock(const QString & text)
71
64
highlightByRegex (m_xmlValueFormat, m_xmlValueRegex, text);
72
65
}
73
66
74
- void BasicXMLSyntaxHighlighter::highlightByRegex (const QTextCharFormat & format,
75
- const QRegExp & regex, const QString & text)
67
+ void BasicXMLSyntaxHighlighter::highlightByRegex (const QTextCharFormat &format, const QRegularExpression ®ex ,
68
+ const QString &text)
76
69
{
77
- int index = regex.indexIn (text);
78
-
79
- while (index >= 0 )
80
- {
81
- int matchedLength = regex.matchedLength ();
82
- setFormat (index, matchedLength, format);
70
+ auto matchIt = regex.globalMatch (text);
83
71
84
- index = regex.indexIn (text, index + matchedLength);
72
+ while (matchIt.hasNext ()) {
73
+ auto match = matchIt.next ();
74
+ setFormat (match.capturedStart (), match.capturedLength (), format);
85
75
}
86
76
}
87
77
@@ -92,9 +82,9 @@ void BasicXMLSyntaxHighlighter::setRegexes()
92
82
m_xmlValueRegex.setPattern (" \" [^\\ n\" ]+\" (?=[?\\ s/>])" );
93
83
m_xmlCommentRegex.setPattern (" <!--[^\\ n]*-->" );
94
84
95
- m_xmlKeywordRegexes = QList<QRegExp>() << QRegExp ( " < \\ ? " ) << QRegExp ( " /> " )
96
- << QRegExp ( " >" ) << QRegExp ( " < " ) << QRegExp (" </ " )
97
- << QRegExp (" \\ ?>" );
85
+ m_xmlKeywordRegexes = QList<QRegularExpression>( )
86
+ << QRegularExpression ( " < \\ ? " ) << QRegularExpression ( " / >" ) << QRegularExpression ( " > " ) << QRegularExpression (" <" )
87
+ << QRegularExpression ( " </ " ) << QRegularExpression (" \\ ?>" );
98
88
}
99
89
100
90
void BasicXMLSyntaxHighlighter::setFormats ()
0 commit comments